SpecFlowOSS / SpecFlow

#1 .NET BDD Framework. SpecFlow automates your testing & works with your existing code. Find Bugs before they happen. Behavior Driven Development helps developers, testers, and business representatives to get a better understanding of their collaboration
https://www.specflow.org/
Other
2.22k stars 751 forks source link

Autofac dependencies are not resolved in BeforeTestRun when the dependencies are registered as Global Dependencies #2734

Open manikandantk88 opened 5 months ago

manikandantk88 commented 5 months ago

SpecFlow Version

3.9.0

Which test runner are you using?

xUnit

Test Runner Version Number

2.5.1

.NET Implementation

.NET 6.0

Project Format of the SpecFlow project

Classic project format using <PackageReference> tags

.feature.cs files are generated using

SpecFlow.Tools.MsBuild.Generation NuGet package

Test Execution Method

Visual Studio Test Explorer

SpecFlow Section in app.config or content of specflow.json

No response

Issue Description

Hi, I am using Autofac plugin for dependency injection. I am trying to register dependencies in a static method tagged with GlobalDependencies by following the Autofac Document

The registered dependencies are not getting resolved in BeforeTestRun

Steps to Reproduce

public sealed class GlobalHooks
{
    [GlobalDependencies]
    public static void CreateGlobalContainer(ContainerBuilder containerBuilder)
    {
        DependencyInjector.RegisterGlobalDependencies(containerBuilder);
        Log.Logger = new LoggerConfiguration()
            .WriteTo.Console()
            .CreateLogger();
    }
    [BeforeTestRun]
    public static void InitializeTestRun(ITestLogger testLogger)
    {
        testLogger.Log("Log is from before test run");
    }
}
public static class DependencyInjector
{
    public static void RegisterGlobalDependencies(ContainerBuilder containerBuilder)
    {
        var services = RegisterServicesWithSerivceCollection();
        containerBuilder.Populate(services);
    }

    private static IServiceCollection RegisterServicesWithSerivceCollection()
    {
        var services = new ServiceCollection();
        services.AddTransient<ITestLogger, TestLogger>();
        return services;
    }
}

Link to Repro Project

No response

rbeattie-ssi commented 2 months ago

Is this a sequence of events problem for you (GlobalDependenies runs but after BeforeTestRun) or GlobalDependencies doesn't run at all? For me I don't see GlobalDependencies fire at all while ScenarioDependencies fires as expected.