VOOZH about

URL: https://www.nuget.org/packages/Xunit.DependencyInjection.Demystifier/10.0.0

⇱ NuGet Gallery | Xunit.DependencyInjection.Demystifier 10.0.0




Xunit.DependencyInjection.Demystifier 10.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xunit.DependencyInjection.Demystifier --version 10.0.0
 
 
NuGet\Install-Package Xunit.DependencyInjection.Demystifier -Version 10.0.0
 
 
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xunit.DependencyInjection.Demystifier" Version="10.0.0" />
 
 
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Xunit.DependencyInjection.Demystifier" Version="10.0.0" />
 
Directory.Packages.props
<PackageReference Include="Xunit.DependencyInjection.Demystifier" />
 
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Xunit.DependencyInjection.Demystifier --version 10.0.0
 
 
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Xunit.DependencyInjection.Demystifier, 10.0.0"
 
 
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Xunit.DependencyInjection.Demystifier@10.0.0
 
 
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Xunit.DependencyInjection.Demystifier&version=10.0.0
 
Install as a Cake Addin
#tool nuget:?package=Xunit.DependencyInjection.Demystifier&version=10.0.0
 
Install as a Cake Tool
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Use Microsoft.Extensions.DependencyInjection to resolve xUnit test cases

XUnit v2 users: please use v2 branch, Xunit.DependencyInjection.StaFact and Xunit.DependencyInjection.xRetry depend on upstream packages, waiting for update.

How to use

Install the Nuget package.

dotnet add package Xunit.DependencyInjection

In your testing project, add the following framework

namespace Your.Test.Project
{
 public class Startup
 {
 public void ConfigureServices(IServiceCollection services)
 {
 services.AddTransient<IDependency, DependencyClass>();
 }
 }
}

Example test class.

public interface IDependency
{
 int Value { get; }
}

internal class DependencyClass : IDependency
{
 public int Value => 1;
}

public class MyAwesomeTests
{
 private readonly IDependency _d;

 public MyAwesomeTests(IDependency d) => _d = d;

 [Fact]
 public void AssertThatWeDoStuff()
 {
 Assert.Equal(1, _d.Value);
 }
}

Xunit.DependencyInjection is built into the generic host and fully supports its lifecycle, allowing you to use all features supported by the generic host, including but not limited to IHostedService.

Integration asp.net core TestHost(3.0+)

Asp.Net Core Startup

dotnet add package Microsoft.AspNetCore.TestHost
public class Startup
{
 public void ConfigureHost(IHostBuilder hostBuilder) => hostBuilder
 .ConfigureWebHost[Defaults](webHostBuilder => webHostBuilder
 .UseTestServer(options => options.PreserveExecutionContext = true)
 .UseStartup<AspNetCoreStartup>());
}

MinimalApi

If you use MinimalApi rather than asp.net core Startup class.

Add package reference for Xunit.DependencyInjection.AspNetCoreTesting

dotnet add package Xunit.DependencyInjection.AspNetCoreTesting
public class Startup
{
 public IHostBuilder CreateHostBuilder() => MinimalApiHostBuilderFactory.GetHostBuilder<Program>();
}

Maybe your asp.net core project should InternalsVisibleTo or add public partial class Program {} in the end of Program.cs;

Detail see Xunit.DependencyInjection.Test.AspNetCore

Startup limitation

  • CreateHostBuilder method
public class Startup
{
 public IHostBuilder CreateHostBuilder([AssemblyName assemblyName]) { }
}
  • ConfigureHost method
public class Startup
{
 public void ConfigureHost(IHostBuilder hostBuilder) { }
}
  • ConfigureServices method
public class Startup
{
 public void ConfigureServices(IServiceCollection services[, HostBuilderContext context]) { }
}
  • BuildHost method

    NOTE: If this method signature is not found, the host is built by simply calling hostBuilder.Build();.

public class Startup
{
 public IHost BuildHost([IHostBuilder hostBuilder]) { return hostBuilder.Build(); }
}
  • Configure method

    Anything defined in ConfigureServices, can be specified in the Configure method signature. These services are injected if they're available.

How to find Startup?

1. Specific startup

Declare [Startup] on test class

2. Nested startup

public class TestClass1
{
 public class Startup
 {
 public void ConfigureServices(IServiceCollection services) { }
 }

3. Closest startup

If the class type full name is "A.B.C.TestClass", find Startup in the following order:

  1. A.B.C.Startup
  2. A.B.Startup
  3. A.Startup
  4. Startup

4. Default startup

Default startup is required before 8.7.0, is optional in some case after 8.7.0.

If is required, please add a startup class in your test project.

Default is find Your.Test.Project.Startup, Your.Test.Project.

If you want to use a special Startup, you can define XunitStartupAssembly and XunitStartupFullName in the PropertyGroup section

<Project>
 <PropertyGroup>
 <XunitStartupAssembly>Abc</XunitStartupAssembly>
 <XunitStartupFullName>Xyz</XunitStartupFullName>
 </PropertyGroup>
</Project>
XunitStartupAssembly XunitStartupFullName Startup
Your.Test.Project.Startup, Your.Test.Project
Abc Abc.Startup, Abc
Xyz Xyz, Your.Test.Project
Abc Xyz Xyz, Abc

Parallel

By default, xUnit runs all test cases in a test class synchronously. This package can extend the test framework to execute tests in parallel.

<Project>

 <PropertyGroup>
 <ParallelizationMode></ParallelizationMode>
 </PropertyGroup>

</Project>

This package has two policies to run test cases in parallel.

  1. Enhance or true

    Respect xunit parallelization behavior.

  2. Force

    Ignore xunit parallelization behavior and force running tests in parallel.

If [Collection](if ParallelizationMode is not Force), [CollectionDefinition(DisableParallelization = true)], [DisableParallelization] declared on the test class, the test class will run sequentially. If [DisableParallelization], [MemberData(DisableDiscoveryEnumeration = true)] declared on the test method, the test method will run sequentially.

It is recommended to use xunit 2.8.0+ and without setting parallelAlgorithm

Thanks Meziantou.Xunit.ParallelTestFramework

How to disable Xunit.DependencyInjection

<Project>
 <PropertyGroup>
 <EnableXunitDependencyInjectionDefaultTestFrameworkAttribute>false</EnableXunitDependencyInjectionDefaultTestFrameworkAttribute>
 </PropertyGroup>
</Project>

How to inject ITestOutputHelper

internal class DependencyClass : IDependency
{
 private readonly ITestOutputHelperAccessor _testOutputHelperAccessor;

 public DependencyClass(ITestOutputHelperAccessor testOutputHelperAccessor)
 {
 _testOutputHelperAccessor = testOutputHelperAccessor;
 }
}

Write Microsoft.Extensions.Logging to ITestOutputHelper

Add package reference for Xunit.DependencyInjection.Logging

dotnet add package Xunit.DependencyInjection.Logging

The call chain must be from the test case. If not, this feature will not work.

public class Startup
{
 public void ConfigureServices(IServiceCollection services) => services
 .AddLogging(lb => lb.AddXunitOutput());
}

How to inject IConfiguration or IHostEnvironment into Startup?

public class Startup
{
 public void ConfigureHost(IHostBuilder hostBuilder) => hostBuilder
 .ConfigureServices((context, services) => { context.XXXX });
}

or

public class Startup
{
 public void ConfigureServices(IServiceCollection services, HostBuilderContext context)
 {
 context.XXXX;
 }
}

How to configure IConfiguration?

public class Startup
{
 public void ConfigureHost(IHostBuilder hostBuilder) => hostBuilder
 .ConfigureHostConfiguration(builder => { })
 .ConfigureAppConfiguration((context, builder) => { });
}

[MemberData] how to inject?

Use [MethodData]

Integrate OpenTelemetry

TracerProviderBuilder builder;

builder.AddSource("Xunit.DependencyInjection");

Do something before and after test case

Inherit BeforeAfterTest and register as BeforeAfterTest service.

See demo.

Initialize some data on startup.

If it is synchronous initialization, you can use the Configure method. If it is asynchronous initialization, you should use IHostedService.

Product Versions Compatible and additional computed target framework versions.
.NET net5.0 net5.0 was computed.  net5.0-windows net5.0-windows was computed.  net6.0 net6.0 was computed.  net6.0-android net6.0-android was computed.  net6.0-ios net6.0-ios was computed.  net6.0-maccatalyst net6.0-maccatalyst was computed.  net6.0-macos net6.0-macos was computed.  net6.0-tvos net6.0-tvos was computed.  net6.0-windows net6.0-windows was computed.  net7.0 net7.0 was computed.  net7.0-android net7.0-android was computed.  net7.0-ios net7.0-ios was computed.  net7.0-maccatalyst net7.0-maccatalyst was computed.  net7.0-macos net7.0-macos was computed.  net7.0-tvos net7.0-tvos was computed.  net7.0-windows net7.0-windows was computed.  net8.0 net8.0 was computed.  net8.0-android net8.0-android was computed.  net8.0-browser net8.0-browser was computed.  net8.0-ios net8.0-ios was computed.  net8.0-maccatalyst net8.0-maccatalyst was computed.  net8.0-macos net8.0-macos was computed.  net8.0-tvos net8.0-tvos was computed.  net8.0-windows net8.0-windows was computed.  net9.0 net9.0 was computed.  net9.0-android net9.0-android was computed.  net9.0-browser net9.0-browser was computed.  net9.0-ios net9.0-ios was computed.  net9.0-maccatalyst net9.0-maccatalyst was computed.  net9.0-macos net9.0-macos was computed.  net9.0-tvos net9.0-tvos was computed.  net9.0-windows net9.0-windows was computed.  net10.0 net10.0 was computed.  net10.0-android net10.0-android was computed.  net10.0-browser net10.0-browser was computed.  net10.0-ios net10.0-ios was computed.  net10.0-maccatalyst net10.0-maccatalyst was computed.  net10.0-macos net10.0-macos was computed.  net10.0-tvos net10.0-tvos was computed.  net10.0-windows net10.0-windows was computed. 
.NET Core netcoreapp3.0 netcoreapp3.0 was computed.  netcoreapp3.1 netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 netstandard2.1 is compatible. 
.NET Framework net472 net472 is compatible.  net48 net48 was computed.  net481 net481 was computed. 
MonoAndroid monoandroid monoandroid was computed. 
MonoMac monomac monomac was computed. 
MonoTouch monotouch monotouch was computed. 
Tizen tizen60 tizen60 was computed. 
Xamarin.iOS xamarinios xamarinios was computed. 
Xamarin.Mac xamarinmac xamarinmac was computed. 
Xamarin.TVOS xamarintvos xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
11.0.0 5,247 9/21/2025
10.0.0 10,301 1/18/2025
9.0.0 17,130 3/18/2024
8.0.1 33,297 3/24/2022
8.0.0 1,039 10/21/2021
7.5.1 15,400 8/31/2021

Use Ben.Demystifier Formate Exception.

public void ConfigureServices(IServiceCollection services)
{
   services.UseDemystifyExceptionFilter();
}