![]() |
VOOZH | about |
dotnet add package Xunit.DependencyInjection --version 11.3.0
NuGet\Install-Package Xunit.DependencyInjection -Version 11.3.0
<PackageReference Include="Xunit.DependencyInjection" Version="11.3.0" />
<PackageVersion Include="Xunit.DependencyInjection" Version="11.3.0" />Directory.Packages.props
<PackageReference Include="Xunit.DependencyInjection" />Project file
paket add Xunit.DependencyInjection --version 11.3.0
#r "nuget: Xunit.DependencyInjection, 11.3.0"
#:package Xunit.DependencyInjection@11.3.0
#addin nuget:?package=Xunit.DependencyInjection&version=11.3.0Install as a Cake Addin
#tool nuget:?package=Xunit.DependencyInjection&version=11.3.0Install as a Cake Tool
Microsoft.Extensions.DependencyInjection to resolve xUnit test casesXUnit v2 users: please use v2 branch.
Xunit.DependencyInjection.SkippableFact are obsoleted on xunit.v3.
👁 Xunit.DependencyInjection NuGet
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.DependencyInjectionis 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 toIHostedService.
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>());
}
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 ofProgram.cs;Detail see Xunit.DependencyInjection.Test.AspNetCore
Startup limitationStartup supports two configuration styles, and the Configure method is supported by both.
CreateHostApplicationBuilder method
NOTE: If this method signature is not found, the host is built by simply calling
Host.CreateEmptyApplicationBuilder(new() { ApplicationName = assemblyName.Name });.
public HostApplicationBuilder CreateHostApplicationBuilder([AssemblyName assemblyName]) { }
ConfigureHostApplicationBuilder method (distinguish by this method)
public void ConfigureHostApplicationBuilder(IHostApplicationBuilder hostApplicationBuilder) { }
BuildHostApplicationBuilder method
NOTE: If this method signature is not found, the host is built by simply calling
hostApplicationBuilder.Build();.
public IHost BuildHostApplicationBuilder(HostApplicationBuilder hostApplicationBuilder)
{
return hostApplicationBuilder.Build();
}
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(); }
}
Startup?Declare [Startup] on test class
public class TestClass1
{
public class Startup
{
public void ConfigureServices(IServiceCollection services) { }
}
If the class type full name is "A.B.C.TestClass", find Startup in the following order:
A.B.C.StartupA.B.StartupA.StartupStartupDefault 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 |
By default, xUnit runs all test cases in a test class synchronously. This package can extend the test framework to execute tests in parallel.
If you register a custom
ITestCollectionOrderer, test collections will be run in the specified order, which may be slower than running without the customITestCollectionOrderer.
<Project>
<PropertyGroup>
<ParallelizationMode></ParallelizationMode>
</PropertyGroup>
</Project>
This package has two policies to run test cases in parallel.
Enhance or true
Respect xunit parallelization behavior.
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 without setting parallelAlgorithm
<Project>
<PropertyGroup>
<EnableXunitDependencyInjectionDefaultTestFrameworkAttribute>false</EnableXunitDependencyInjectionDefaultTestFrameworkAttribute>
</PropertyGroup>
</Project>
internal class DependencyClass : IDependency
{
private readonly ITestOutputHelperAccessor _testOutputHelperAccessor;
public DependencyClass(ITestOutputHelperAccessor testOutputHelperAccessor)
{
_testOutputHelperAccessor = testOutputHelperAccessor;
}
}
Microsoft.Extensions.Logging to ITestOutputHelperAdd 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());
}
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;
}
}
IConfiguration?public class Startup
{
public void ConfigureHost(IHostBuilder hostBuilder) => hostBuilder
.ConfigureHostConfiguration(builder => { })
.ConfigureAppConfiguration((context, builder) => { });
}
Use [MethodData]
TracerProviderBuilder builder;
builder.AddSource("Xunit.DependencyInjection");
Inherit BeforeAfterTest and register as BeforeAfterTest service.
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 | net8.0 net8.0 is compatible. 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 Framework | net472 net472 is compatible. net48 net48 was computed. net481 net481 was computed. |
Showing the top 5 NuGet packages that depend on Xunit.DependencyInjection:
| Package | Downloads |
|---|---|
|
Rystem.Test.XUnit
Rystem is a open-source framework to improve the System namespace in .Net |
|
|
Xunit.DependencyInjection.Logging
Support Microsoft.Extensions.Logging to ITestOutputHelper. public void Configure(IServiceProvider provider) { XunitTestOutputLoggerProvider.Register(provider); } |
|
|
Xunit.DependencyInjection.SkippableFact
Support Xunit.SkippableFact. public void ConfigureServices(IServiceCollection services) { services.AddSkippableFactSupport(); } |
|
|
Xunit.DependencyInjection.xRetry
Support xRetry. public void ConfigureServices(IServiceCollection services) { services.AddXRetrySupport(); } |
|
|
Xunit.DependencyInjection.Demystifier
Use Ben.Demystifier Formate Exception. public void ConfigureServices(IServiceCollection services) { services.UseDemystifyExceptionFilter(); } |
Showing the top 19 popular GitHub repositories that depend on Xunit.DependencyInjection:
| Repository | Stars |
|---|---|
|
dotnetcore/Util
Util是一个.Net平台下的应用框架,旨在提升中小团队的开发能力,由工具类、分层架构基类、Ui组件,配套代码生成模板,权限等组成。
|
|
|
chromelyapps/Chromely
Build Cross Platform HTML Desktop Apps on .NET using native GUI, HTML5, JavaScript, CSS, Owin, AspNetCore (MVC, RazorPages, Blazor)
|
|
|
microsoft/kernel-memory
Research project. A Memory solution for users, teams, and applications.
|
|
|
Nexus-Mods/NexusMods.App
Home of the development of the Nexus Mods App
|
|
|
dotnetcore/sharding-core
high performance lightweight solution for efcore sharding table and sharding database support read-write-separation .一款ef-core下高性能、轻量级针对分表分库读写分离的解决方案,具有零依赖、零学习成本、零业务代码入侵
|
|
|
wabbajack-tools/wabbajack
An automated Modlist installer for various games.
|
|
|
bing-framework/Bing.NetCore
Bing是基于 .net core 3.1 的框架,旨在提升团队的开发输出能力,由常用公共操作类(工具类、帮助类)、分层架构基类,第三方组件封装,第三方业务接口封装等组成。
|
|
|
kendryte/nncase
Open deep learning compiler stack for Kendryte AI accelerators ✨
|
|
|
luoyunchong/lin-cms-dotnetcore
😃A simple and practical CMS implemented by .NET + FreeSql;前后端分离、Docker部署、OAtuh2授权登录、自动化部署DevOps、自动同步至Gitee、代码生成器、仿掘金专栏
|
|
|
kodlamaio-projects/nArchitecture
Inspired by Clean Architecture, nArchitecture is a monolith project which uses advanced techniques.
|
|
|
ShokoAnime/ShokoServer
Repository for Shoko Server.
|
|
|
sethreno/schemazen
Script and create SQL Server objects quickly
|
|
|
grate-devs/grate
grate - the SQL scripts migration runner
|
|
|
BestOwl/MyPhone
Connect your mobile devices (Android/iOS/WindowsPhone) to PC
|
|
|
WeihanLi/WeihanLi.Npoi
NPOI Extensions, excel/csv importer/exporter for IEnumerable<T>/DataTable, fluentapi(great flexibility)/attribute configuration
|
|
|
Viincenttt/MollieApi
This project allows you to easily add the Mollie payment provider to your application.
|
|
|
2sic/2sxc
DNN + 2sxc = #DNNCMS - This tool helps web designers and developers prepare great looking content in DNN (DotNetNuke). It's like mixing DNN with Umbraco and Drupal :)
|
|
|
sa-es-ir/DotNet.RateLimit
A Distributed RateLimit for Controller-Actions and Minimal API.
|
|
|
dr-marek-jaskula/DomainDrivenDesignUniversity
This project was made for tutorial purpose - to clearly present the domain driven design concept.
|
| Version | Downloads | Last Updated |
|---|---|---|
| 11.3.0 | 10,451 | 6/9/2026 |
| 11.2.1 | 199,527 | 3/18/2026 |
| 11.2.0 | 485,716 | 2/27/2026 |
| 11.1.1 | 297,754 | 12/30/2025 |
| 11.1.0 | 309,803 | 11/11/2025 |
| 11.0.0 | 149,252 | 9/21/2025 |
| 10.8.0 | 12,579 | 9/21/2025 |
| 10.7.0 | 21,368 | 9/3/2025 |
| 10.6.0 | 119,126 | 7/16/2025 |
| 10.5.0 | 27,055 | 6/16/2025 |
| 10.4.2 | 805,142 | 5/18/2025 |
| 10.4.1 | 23,749 | 5/7/2025 |
| 10.4.0 | 343,371 | 4/9/2025 |
| 10.3.0 | 140,571 | 3/10/2025 |
| 10.2.1 | 94,972 | 2/27/2025 |
| 10.2.0 | 128,742 | 2/9/2025 |
| 9.9.2 | 31,535 | 3/18/2026 |
| 9.9.1 | 328,898 | 5/7/2025 |
| 9.9.0 | 279,344 | 2/9/2025 |
Use Microsoft.Extensions.DependencyInjection to inject xunit testclass. If you want write Microsoft.Extensions.Logging to ITestOutputHelper, please install Xunit.DependencyInjection.Logging.
Release notes:
11.3: Support FsCheck.Xunit.v3.
11.2: Update xunit.v3 to 3.2.2, Move HostManager.Start/Stop to AssemblyRunner.
11.1: Update xunit.v3 to 3.2.0.
11.0: C# 14, Downgrade Microsoft.Extensions.Hosting to 8.0.
10.8: Add CreateHostApplicationBuilder method.
10.7: Update xunit.v3 to 3.0.1, does not set ApplicationName if it is already configured.
10.6: Update xunit.v3 to 3.0.0.
10.5: Improve compatibility with top level statements.
10.4: Fix #146.
10.3: Update xunit.v3 to 2.0.0.
10.2: Fix some parallelization problem.
10.1: Allow the default startup to be missing anywhere.
10.0: Upgrade xunit to v3.