Use AutoFac to resolve xUnit test cases.
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;
}
MIT