xunit.ioc.autofac
xunit.ioc.autofac copied to clipboard
XUnit2 Test Framework implementation with Autofac
xUnit Autofac

Use AutoFac to resolve xUnit test cases.
No longer maintained
This project is no longer maintained. We suggest that you migrate your existing solutions to use Xunit.DependencyInjection instead. For example:
-
Replace
ConfigureTestFrameworkwithStartup, and make sure that you specify theConfigureHostmethod andUseServiceProviderFactoryto specify that you want to use Autofac. Remove theTestFrameworkattribute.diff --git a/ConfigureTestFramework.cs b/ConfigureTestFramework.cs index 0c2c4ca..204e781 100644 --- a/ConfigureTestFramework.cs +++ b/ConfigureTestFramework.cs @@ -1,27 +1,28 @@ -[assembly: TestFramework("Your.Test.Project.ConfigureTestFramework", "AssemblyName")] - namespace Your.Test.Project { - public class ConfigureTestFramework : AutofacTestFramework + public class Startup { - private const string TestSuffixConvention = "Tests"; + public void Configure(ILoggerFactory loggerFactory, XunitTestOutputLoggerProvider loggerProvider) + { + loggerFactory.AddProvider(loggerProvider); + } - public ConfigureTestFramework(IMessageSink diagnosticMessageSink) - : base(diagnosticMessageSink) + public void ConfigureHost(IHostBuilder hostBuilder) { - var builder = new ContainerBuilder(); - builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly()) - .Where(t => t.Name.EndsWith(TestSuffixConvention)); + hostBuilder + .UseServiceProviderFactory(new AutofacServiceProviderFactory()) + .ConfigureContainer<ContainerBuilder>(RegisterDependencies); + } - builder.Register(context => new TestOutputHelper()) - .AsSelf() - .As<ITestOutputHelper>() - .InstancePerLifetimeScope(); + public void ConfigureServices(IServiceCollection serviceCollection) + { + serviceCollection.AddTransient<XunitTestOutputLoggerProvider>(); + } + private void RegisterDependencies(ContainerBuilder builder) + { // configure your container // e.g. builder.RegisterModule<TestOverrideModule>(); - - Container = builder.Build(); } } } -
Remove
UseAutofacTestFrameworkfrom all of your test classes.diff --git a/MyAwesomeTests.cs b/MyAwesomeTests.cs index 3a6ffb2..638506d 100644 --- a/MyAwesomeTests.cs +++ b/MyAwesomeTests.cs @@ -1,4 +1,3 @@ -[UseAutofacTestFramework] public class MyAwesomeTests { public MyAwesomeTests()
How to use
Install the Nuget package.
Install-Package xunit2.ioc.autofac
In your testing project, add the following framework
[assembly: TestFramework("Your.Test.Project.ConfigureTestFramework", "AssemblyName")]
namespace Your.Test.Project
{
public class ConfigureTestFramework : AutofacTestFramework
{
private const string TestSuffixConvention = "Tests";
public ConfigureTestFramework(IMessageSink diagnosticMessageSink)
: base(diagnosticMessageSink)
{
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly())
.Where(t => t.Name.EndsWith(TestSuffixConvention));
builder.Register(context => new TestOutputHelper())
.AsSelf()
.As<ITestOutputHelper>()
.InstancePerLifetimeScope();
// configure your container
// e.g. builder.RegisterModule<TestOverrideModule>();
Container = builder.Build();
}
}
}
Example test class.
[UseAutofacTestFramework]
public class MyAwesomeTests
{
public MyAwesomeTests()
{
}
public MyAwesomeTests(ITestOutputHelper outputHelper)
{
_outputHelper = outputHelper;
}
[Fact]
public void AssertThatWeDoStuff()
{
_outputHelper.WriteLine("Hello");
}
private readonly ITestOutputHelper _outputHelper;
}
License
MIT