/xunit.ioc.autofac

XUnit2 Test Framework implementation with Autofac

Primary LanguageC#MIT LicenseMIT

xUnit Autofac Build Master NuGet Version Join the chat at https://gitter.im/xunit-ioc-autofac/Lobby

Use AutoFac to resolve xUnit test cases.

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