xUnit

xUnit.net is a free, open source, community-focused unit testing tool for .NET.

  • documentation @github 9
  • Comparing xUnit.net to other frameworks 1
  • Using xUnit with ASP.NET Core, 2 blog 2016
  • Unit testing C# in .NET Core using dotnet test and xUnit 3 mslearn
  • Getting Started with xUnit.net
    v3 .NET or .NET Framework with the .NET SDK command line 6
    v2 .NET Core with Visual Studio 5
    v2 .NET Framework with Visual Studio 4.
  • End to end testing angular js apps with XUnit and Protractor.Net 7 2016
  • Assert.ThrowsAsync 10
  • Creating parameterised tests in xUnit with InlineData, ClassData and MemberData 11, 2017
  • xUnit Theory: Working With InlineData, MemberData, ClassData 12, 2017
  • Introduction to integration testing with xUnit and TestServer in ASP.NET Core 13, 2016
  • Capturing Output 14
  • Shared Context between Tests 15
  • Creating strongly typed xUnit theory test data with TheoryData 16
  • TypedClassData Typed Class Data Attribute for xUnit Test Framework 17

Deadlock When Using Parallelization and Blocking on Async Code

VS runner hangs on Run All #611

  public static object ApiVversionControllerNameGet(this IGeneratedAPI operations, string version) { 
    return operations.ApiVversionGetAsync(version).GetAwaiter().GetResult(); 
  }

Resolution was to:

update test runner related packages in the HLD template
update test runner related packages in the HLD template

  <package id="xunit" version="2.3.1" targetFramework="net462" />
  <package id="xunit.abstractions" version="2.0.1" targetFramework="net462" />
  <package id="xunit.analyzers" version="0.7.0" targetFramework="net462" />
  <package id="xunit.assert" version="2.3.1" targetFramework="net462" />
  <package id="xunit.core" version="2.3.1" targetFramework="net462" />
  <package id="xunit.extensibility.core" version="2.3.1" targetFramework="net462" />
  <package id="xunit.extensibility.execution" version="2.3.1" targetFramework="net462" />
  <package id="xunit.runner.visualstudio" version="2.2.0" targetFramework="net462" developmentDependency="true" />

Configure xUnit to not participate in parallelization with other assemblies.
Tests in the same test collection will be run sequentially against each other, but tests in different test collections will be run in parallel against each other.

All parallelization within this test assembly is disabled.

Configure xUnit to not participate in parallelization with other assemblies.
Tests in the same test collection will be run sequentially against each other, but tests in different test collections will be run in parallel against each other.

All parallelization within this test assembly is disabled. 18

  <appSettings>
    <add key="xunit.appDomain" value="ifAvailable" />
    <add key="xunit.diagnosticMessages" value="true" />
    <add key="xunit.methodDisplay" value="classAndMethod" />
    <add key="xunit.parallelizeTestCollections" value="false" />
    <add key="xunit.parallelizeAssembly" value="false" />
    <add key="xunit.preEnumerateTheories" value="true" />
    <add key="xunit.shadowCopy" value="true" />
    <add key="longRunningTestSeconds" value="10" />
  </appSettings>

Classes with a sensible number of tests have been isolated into collections, collection is Xunit.CollectionAttribute:

    [Collection("Some Collection")]
    [Trait("Controller", "Controller Name")]
    [Trait("Client", "AutoRest")]
    public class SomeControllerTests {...}

For a .NET Core project, a xUnit configuration would have been done via a xunit.runner.json file in the test project root.

{
  "appDomain": "ifAvailable",
  "diagnosticMessages": true,
  "methodDisplay": "classAndMethod",
  "parallelizeTestCollections": true,
  "internalDiagnosticMessages": true,
  "parallelizeAssembly": true,
  "preEnumerateTheories": true,
  "shadowCopy": true
}

About xUnit.net

xUnit.net is a free, open-source, community-focused unit testing tool for the .NET Framework. Written by the original inventor of NUnit v2, xUnit.net is the latest technology for unit testing C#, F#, VB.NET, and other .NET languages. xUnit.net works with ReSharper, CodeRush, TestDriven.NET, and Xamarin 19.

What’s New in v3

What’s New in v3 24
xUnit.net v3 introduces major architectural changes including built-in support for the Microsoft Testing Platform (MTP), improved parallelization, and a new extensibility model. Key changes include:

  • Native support for Microsoft.Testing.Platform as an alternative to VSTest 20
  • TheoryData<T> for type-safe theory data 16
  • Built-in Roslyn analyzers for common mistakes and best practices 21
  • Handle cancellation via CancellationToken in test methods 22

Roslyn Analyzer Rules

xUnit.net ships with xunit.analyzers that provide compile-time checks for common testing mistakes. Rules cover proper use of Assert methods, theory data compatibility, test class lifecycle, and collection fixtures 21.

Theory

TheoryData{T} Type Safety

Typesafety in xUnit with TheoryData{T} 27
TheoryData<T> provides a strongly typed alternative to object[] for theory data. Instead of relying on MemberData returning IEnumerable<object[]>, use TheoryData<T1, T2> to get compile-time checking of parameter types 16.

Mixing Theory Data

Mixing theory data mechanisms, input and expected data 28
Theory data sources can be combined: InlineData for simple cases, MemberData for shared data sets, and ClassData for reusable data across test classes 11.

Clean Test Cases

Create clean test cases in xUnit with TheoryData 29
Keep theory test cases focused and self-documenting. Each InlineData entry should represent a distinct scenario with a clear expected outcome. Prefer TheoryData<T> with descriptive property names over raw object[] arrays 16.

Custom Theory Data Serialization

Custom Theory Data Serialization 25
Custom theory data types that do not implement IXunitSerializable will not display correctly in test explorers. Implement custom serialization for complex types used in theory data to ensure test names are meaningful and test cases are individually identifiable.

Code Coverage With MTP

Code Coverage with MTP 26
Microsoft Testing Platform enables integrated code coverage collection without external tooling. Use the --collect "Code Coverage" flag with dotnet test to generate coverage reports directly through MTP 20.

Microsoft Testing Platform (v3)

v3 introduces first-class support for Microsoft.Testing.Platform (MTP) as an alternative runner to VSTest. MTP provides faster test execution, better diagnostics, and a more extensible architecture. Configure via xunit.runner.json or MSBuild properties 20.

Miscellaneous

  • Unit testing C# with dotnet test and xUnit 3 mslearn
  • Handle cancellation in tests with xUnit.net v3 30: test methods can accept a CancellationToken parameter that signals when a test run is being cancelled, allowing graceful cleanup 22
< «