Xunit.DependencyInjection
9.7.1
dotnet add package Xunit.DependencyInjection --version 9.7.1
NuGet\Install-Package Xunit.DependencyInjection -Version 9.7.1
<PackageReference Include="Xunit.DependencyInjection" Version="9.7.1" />
paket add Xunit.DependencyInjection --version 9.7.1
#r "nuget: Xunit.DependencyInjection, 9.7.1"
// Install Xunit.DependencyInjection as a Cake Addin #addin nuget:?package=Xunit.DependencyInjection&version=9.7.1 // Install Xunit.DependencyInjection as a Cake Tool #tool nuget:?package=Xunit.DependencyInjection&version=9.7.1
Use Microsoft.Extensions.DependencyInjection
to resolve xUnit test cases
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 toIHostedService
.
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 ofProgram.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]) { }
}
Configure
methodAnything defined in
ConfigureServices
, can be specified in theConfigure
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:
A.B.C.Startup
A.B.Startup
A.Startup
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.
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 to use xunit 2.8.0+ and without setting parallelAlgorithm
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.
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 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
.NET Framework | net472 is compatible. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETFramework 4.7.2
- Microsoft.Extensions.Hosting (>= 8.0.1)
- xunit.extensibility.execution (>= 2.4.2 && < 3.0.0)
-
.NETStandard 2.1
- Microsoft.Extensions.Hosting (>= 8.0.1)
- xunit.extensibility.execution (>= 2.4.2 && < 3.0.0)
NuGet packages (13)
Showing the top 5 NuGet packages that depend on Xunit.DependencyInjection:
Package | Downloads |
---|---|
Xunit.DependencyInjection.Logging
Support Microsoft.Extensions.Logging to ITestOutputHelper. public void Configure(IServiceProvider provider) { XunitTestOutputLoggerProvider.Register(provider); } |
|
Rystem.Test.XUnit
Rystem is a open-source framework to improve the System namespace in .Net |
|
Xunit.DependencyInjection.SkippableFact
Support Xunit.SkippableFact. public void ConfigureServices(IServiceCollection services) { services.AddSkippableFactSupport(); } |
|
Xunit.DependencyInjection.Demystifier
Use Ben.Demystifier Formate Exception. public void ConfigureServices(IServiceCollection services) { services.UseDemystifyExceptionFilter(); } |
|
Xunit.DependencyInjection.xRetry
Support xRetry. public void ConfigureServices(IServiceCollection services) { services.AddXRetrySupport(); } |
GitHub repositories (18)
Showing the top 5 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
RAG architecture: index and query any data using LLM and natural language, track sources, show citations, asynchronous memory patterns.
|
|
dotnetcore/sharding-core
high performance lightweight solution for efcore sharding table and sharding database support read-write-separation .一款ef-core下高性能、轻量级针对分表分库读写分离的解决方案,具有零依赖、零学习成本、零业务代码入侵
|
|
Nexus-Mods/NexusMods.App
Home of the development of the Nexus Mods App
|
Version | Downloads | Last updated |
---|---|---|
9.7.1 | 4,149 | 12/20/2024 |
9.7.0 | 109,573 | 11/21/2024 |
9.6.0 | 162,266 | 10/30/2024 |
9.5.0 | 63,070 | 10/11/2024 |
9.4.0 | 355,105 | 9/15/2024 |
9.3.1 | 63,980 | 8/30/2024 |
9.3.0 | 1,609,288 | 5/14/2024 |
9.2.1 | 85,631 | 4/24/2024 |
9.1.0 | 133,396 | 4/3/2024 |
9.0.1 | 55,966 | 3/18/2024 |
8.9.1 | 594,339 | 12/10/2023 |
8.9.0 | 408,960 | 9/19/2023 |
8.8.2 | 129,488 | 8/23/2023 |
8.8.1 | 9,757 | 8/22/2023 |
8.7.2 | 174,122 | 7/16/2023 |
8.7.1 | 924,602 | 3/11/2023 |
8.7.0 | 331,272 | 1/4/2023 |
8.6.1 | 572,193 | 10/22/2022 |
8.6.0 | 55,309 | 10/11/2022 |
8.5.0 | 1,089,311 | 4/17/2022 |
8.4.1 | 188,595 | 3/24/2022 |
8.4.0 | 20,482 | 3/17/2022 |
8.3.0 | 566,880 | 11/25/2021 |
8.2.0 | 245,409 | 11/12/2021 |
8.1.0 | 51,771 | 10/22/2021 |
8.0.0 | 89,610 | 10/21/2021 |
7.7.0 | 147,181 | 10/14/2021 |
7.6.0 | 222,564 | 9/2/2021 |
7.5.1 | 178,511 | 8/31/2021 |
7.4.0 | 94,865 | 8/14/2021 |
7.3.0 | 289,575 | 7/9/2021 |
7.2.0 | 358,638 | 4/19/2021 |
7.1.0 | 412,061 | 11/7/2020 |
Use Microsoft.Extensions.DependencyInjection to inject xunit testclass. If you want write Microsoft.Extensions.Logging to ITestOutputHelper, please install Xunit.DependencyInjection.Logging.
Release notes:
9.7: Fix #133 #134, IHost.StopAsync might not have been invoked.
9.6: Support required property on test class.
9.5: Add support for BuildHostApplicationBuilder, fixing TheoryData of T evaluation.
9.4: Add ITestClassOrderer, Support registration ITestCollectionOrderer and ITestCaseOrderer.
9.3: Support xunit 2.8.0.
9.2: Add BeforeAfterTest.
9.1: Support [FromKeyedService].
9.0: Upgrade TargetFramework version and reference package version.
8.9: Extends the test framework to execute tests in parallel.
8.8: Added support `BuildHost` method in startup.
8.7: Allow missing default startup in some case.
8.6: Support any awaitable type of async test method.
8.5: Change analyzer category.
8.4: Fix #63: allow modify default service from DI container.
8.3: Add ActivitySource(name: Xunit.DependencyInjection) and ValueTask/ValueTask<> support.
8.2: Fix #60: support IAsyncDisposable.
8.1: Startup allow static method or class (like Asp.Net Core startup).
8.0: New feature: Support multiple startup.