Skip to content

SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.

License

Notifications You must be signed in to change notification settings

304NotModified/SpecFlow.DependencyInjection

 
 

Repository files navigation

SpecFlow.DependencyInjection

GitHub License GitHub Issues Azure Build NuGet Package

SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.

Currently supports:

Based on https://github.com/gasparnagy/SpecFlow.Autofac (now part of SpecFlow).

Usage

Install plugin from NuGet into your SpecFlow project.

PM> Install-Package SolidToken.SpecFlow.DependencyInjection

Create a static method somewhere in the SpecFlow project (recommended to put it into the Support folder) that returns an Microsoft.Extensions.DependencyInjection IServiceCollection and tag it with the [ScenarioDependencies] attribute. Configure your dependencies for the scenario execution within the method. You also have to register the step definition classes, that you can do by either registering all classes marked with the [Binding] attribute:

foreach (var type in typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
{
    services.AddSingleton(type);
}

A typical dependency builder method probably looks like this:

[ScenarioDependencies]
public static IServiceCollection CreateServices()
{
    var services = new ServiceCollection();
    
    // TODO: add customizations, stubs required for testing

    foreach (var type in typeof(TestDependencies).Assembly.GetTypes().Where(t => Attribute.IsDefined(t, typeof(BindingAttribute))))
    {
        services.AddSingleton(type);
    }

    return services;
}

Refer to SpecFlow.DependencyInjection.Tests for a typical implementation.

About

SpecFlow plugin that enables to use Microsoft.Extensions.DependencyInjection for resolving test dependencies.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 90.3%
  • Gherkin 9.7%