I have a test class with a constructor that needs an IService.
public class ConsumerTests { private readonly IService _service; public ConsumerTests(IService servie) { _service = service; } [Fact] public void Should_() { //use _service } }
I want to plugin my DI container of choice to build the test class.
Is this possible with xUnit?
The xUnit.net test runner that we've been using supports . NET Core 1.0 or later, . NET 5.0 or later, and . NET Framework 4.5.
xUnit.net offers several methods for sharing this setup and cleanup code, depending on the scope of things to be shared, as well as the expense associated with the setup and cleanup code.
Parallelism in Test FrameworksTests written in xUnit.net version 1 cannot be run in parallel against each other in the same assembly, though multiple test assemblies linked against v1 are still able to participate in the runner parallelism feature described in the next sub-section.
xUnit is the collective name for several unit testing frameworks that derive their structure and functionality from Smalltalk's SUnit. SUnit, designed by Kent Beck in 1998, was written in a highly structured object-oriented style, which lent easily to contemporary languages such as Java and C#.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With