Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reqnroll.Microsoft.Extensions.DependencyInjection : not possible to get a IReqnrollOutputHelper in the [ScenarioDependencies] #317

Closed
StefH opened this issue Nov 7, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@StefH
Copy link

StefH commented Nov 7, 2024

Reqnroll Version

2.1.1

Which test runner are you using?

xUnit

Test Runner Version Number

xunit.runner.visualstudio 2.8.2

.NET Implementation

.NET 8.0

Test Execution Method

Visual Studio Test Explorer

Content of reqnroll.json configuration file

No response

Issue Description

I want to access the IReqnrollOutputHelper in the [ScenarioDependencies] method in order to register my custom ILogger implementation.

Steps to Reproduce

I followed this #247

When I try this code:

public static bool wasExecuted = false;
[BeforeScenario]
public static void ConfigureServices(ITestRunContext testRunContext, ITestThreadContext testThreadContext)
{
  if (wasExecuted) return;
  wasExecuted = true;

  var globalContainer = testRunContext.TestRunContainer; // this is the real global container used by all parallel threads
  var testThreadContainer = testThreadContext.TestThreadContainer; // this is what you have used in your hook
  //do service regs
}

I get error like:
System.InvalidOperationException : No service for type 'Reqnroll.ITestThreadContext' has been registered.


When I use this code:

[Binding]
public class Startup
{
    private static IReqnrollOutputHelper? _outputHelper;

    protected Startup()
    {
    }

    [BeforeScenario]
    public static void Before(TestThreadContext testThreadContext)
    {
        if (_outputHelper != null)
        {
            return;
        }

        _outputHelper = testThreadContext.TestThreadContainer.Resolve<IReqnrollOutputHelper>();
    }

    [ScenarioDependencies]
    public static IServiceCollection CreateServices()
    {
        return new ServiceCollection()
            .AddLogging(builder => builder
                .AddProvider(new ReqnrollLoggerProvider(_outputHelper!)))
            .AddTransient<Calculator>();
    }
}

When using BeforeFeature or BeforeScenario, the CreateServices is called first, after that the Before method is called, so that does not work.

If I use BeforeTestRun, I get error:

System.Collections.Generic.KeyNotFoundException : The given key 'Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope' was not present in the dictionary.

The only code which does work is like:

[BeforeTestRun]
public static void BeforeTestRun(ITestRunnerManager testRunnerManager)
{
    if (_outputHelper != null)
    {
        return;
    }

    var runner = testRunnerManager.GetTestRunner();
    var context = runner.TestThreadContext;
    _outputHelper = context.TestThreadContainer.Resolve<IReqnrollOutputHelper>();
    testRunnerManager.ReleaseTestThreadContext(context);
}

Although I'm not sure this is the correct solution....

Link to Repro Project

StefH/Stef.Extensions.SpecFlow.Logging#1

@gasparnagy
Copy link
Contributor

See the comment relevant for this issue from #247 here: #247 (comment)

Also important to note that the workaround provided above will only work for non-parallel execution.

@gasparnagy
Copy link
Contributor

Closing this ticket, but please reopen if there is anything new on it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants