Skip to content

XUnit2 Test Framework implementation with Autofac

License

Notifications You must be signed in to change notification settings

ze1tgeist/xunit.ioc.autofac

 
 

Repository files navigation

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

About

XUnit2 Test Framework implementation with Autofac

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%