⚠️ This repo is not maintained anymore, please use SpecFlow.DependencyInjection ⚠️
AdCodicem.SpecFlow.MicrosoftDependencyInjection is a SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies. It's based on Gáspár Nagy's SpecFlow.Autofac plugin.
Currently supports :
- SpecFlow 3.7 or above
- Microsoft.Extensions.DependencyInjection 2.1 or above
License: Apache
I created this plugin for Specflow because most of my projects use the plain Microsoft.Extensions.DependencyInjection for injecting dependency. If you've already familiar why ASP.Net Core dependency injection, you won't be lost with this plugin 😉.
Install plugin from NuGet into your SpecFlow project.
PM> Install-Package AdCodicem.SpecFlow.MicrosoftDependencyInjection
Create a non-static class somewhere in the SpecFlow project implementing the IServicesConfigurator
interface.
Configure the dependencies within the method.
You don't have to register the step definition classes as it's already done by this plugin.
A typical dependency configuration class probably looks like this :
public class Startup : IServicesConfigurator
{
/// <inheritdoc />
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<ICalculator, Calculator>();
}
}
Since v1.1.0 you can instruct the service collection to use the dependencies registered automatically by Specflow.
public class Startup : IServicesConfigurator
{
/// <inheritdoc />
public void ConfigureServices(IServiceCollection services)
{
services
// ITestOutputHelper is automatically registered by Specflow when using xUnit runner
.AddDelegated<ITestOutputHelper>()
// Calculator can use ITestOutputHelper as it's registered above to delegate the resolution to the original container
.AddTransient<ICalculator, Calculator>();
}
}
To register a delegated service, simply add the following call in configuration pipeline: AddDelegated<TService>()
.
The service resolution is delegated to the original container used by Specflow.
- Fix: Support for Specflow v3.7+
- Fix: Added Intellisense documentation
- Feature: Allow the delegation of the resolution of some services to the original Specflow dependency injection.
The delegated services must be registered like this:
services.AddDelegated<ITestOutputHelper>()
.
- Fix: Registered services are now disposed if they're disposable.
- Initial release.
- Increased minimal
Microsoft.Extensions.DependencyInjection
version to 2.1.0 to preventMissingMethodException
issue when using an higher version of the NuGet.