Uses Microsoft.Extensions.DependencyInjection to resolve xUnit test cases.
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);
}
}
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>());
}
Detail see Xunit.DependencyInjection.Test.AspNetCore
- 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 method
Anything defined in ConfigureServices, can be specified in the Configure method signature. These services are injected if they're available.
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.Startup
- A.B.Startup
- A.Startup
- Startup
Default startup is required before 8.7.0, is optional in some case after 8.7.0.
If is required, plaese add a startup class in your test project.
Default is find Your.Test.Project.Startup, Your.Test.Project`.
If you want use a special Startup
, you can defined XunitStartupAssembly
and XunitStartupFullName
in 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 |
<Project>
<PropertyGroup>
<EnableXunitDependencyInjectionDefaultTestFrameworkAttribute>false</EnableXunitDependencyInjectionDefaultTestFrameworkAttribute>
</PropertyGroup>
</Project>
internal class DependencyClass : IDependency
{
private readonly ITestOutputHelperAccessor _testOutputHelperAccessor;
public DependencyClass(ITestOutputHelperAccessor testOutputHelperAccessor)
{
_testOutputHelperAccessor = testOutputHelperAccessor;
}
}
The call chain must from test case. If not, this feature will not work.
public class Startup
{
public void ConfigureServices(IServiceCollection services) => services.AddLogging(lb => lb.AddXunitOutput());
}
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;
}
}
public class Startup
{
public void ConfigureHost(IHostBuilder hostBuilder) =>
hostBuilder
.ConfigureHostConfiguration(builder => { })
.ConfigureAppConfiguration((context, builder) => { });
}
Use [MethodData]
TracerProviderBuilder builder;
builder.AddSource("Xunit.DependencyInjection");