Is it possible to use Dependency Injection with xUnit?

Yes it’s possible with Xunit.DependencyInjection

Install-Package Xunit.DependencyInjection

and you can inject your services

[assembly: TestFramework("Your.Test.Project.Startup", "AssemblyName")]
    
namespace Your.Test.Project
{
    public class Startup : DependencyInjectionTestFramework
    {
        public Startup(IMessageSink messageSink) : base(messageSink) { }
    
        protected override void ConfigureServices(IServiceCollection services)
        {
            services.AddTransient<IDependency, DependencyClass>();
        }
    }
}

https://github.com/pengweiqhca/Xunit.DependencyInjection

Leave a Comment