How to run setup code only once in an xUnit.net test

Following the guidance in this xUnit discussion topic, it looks like you need to implement a constructor on the Fixture and also implement IDisposable. Here’s a complete sample that behaves the way you want: using System; using System.Diagnostics; using Xunit; using Xunit.Sdk; namespace xUnitSample { public class SomeFixture : IDisposable { public SomeFixture() { Console.WriteLine(“SomeFixture … Read more

Having an actual decimal value as parameter for an attribute (example xUnit.net’s [InlineData]

You should be able use the String value in the Attribute and set the Parameter type to Decimal, it get’s converted automatically by the Test Framework as far as I can tell. [Theory] [InlineData(“37.60”)] public void MyDecimalTest(Decimal number) { Assert.Equal(number, 37.60M); } If this doesn’t work then you can manually convert it by passing in … Read more

Which is better? Unit-test project per solution or per project?

Assemblies are a packaging/deployment concern, so we usually split them out because we don’t want to deploy them with our product. Whether you split them out per library or per solution there are merits to both. Ultimately, you want tests to be immediately available to all developers, so that developers know where to find them … Read more

xUnit doesn’t write message to the output pane

This solution works for me (Visual Studio 2017 and xUnit 2.2.0.3545). Try adding the below configuration in the App.Config file. (I don’t know why. It was just needed. If it doesn’t exist, just add a new one in your test project.) <?xml version=”1.0″ encoding=”utf-8″ ?> <configuration> <appSettings> <add key=”xunit.diagnosticMessages” value=”true”/> </appSettings> </configuration> The output information will be … Read more

Xunit 2.3.0 Unable to pass dates as inline params

You can make it explicit with MemberDataAttribute :- public static readonly object[][] CorrectData = { new object[] { “title 1”, “testing 1”, 1, “Educational”, new DateTime(2017,3,1), new DateTime(2018,12,31)}, new object[] { “title 2”, “testing 2”, 2, “Self Employment”, new DateTime(2017, 2, 1), new DateTime(2018, 2, 28)} }; [Theory, MemberData(nameof(CorrectData))] public async Task WhenPassingCorrectData_SuccessfullyCreate(string title, string … Read more

Is there an easy way in xunit.net to compare two collections without regarding the items’ order?

Brad Wilson from xunit.net told me in this Github Issue that one should use LINQ’s OrderBy operator and afterwards Assert.Equal to verify that two collections contain equal items without regarding their order. Of course, you would have to have a property on the corresponding item class that you can use for ordering in the first … Read more

Skipping a whole test class in xUnit.net

No – there is no such facility at present, and the last time it was requested it was considered too low value to add, One quick way of achieving the effect in xUnit is to comment out the public – private classes are not reflected over (obviously it won’t appear on the skip list that … Read more