reqnroll / Reqnroll

Open-source Cucumber-style BDD test automation framework for .NET.
https://reqnroll.net
BSD 3-Clause "New" or "Revised" License
302 stars 33 forks source link

Reqnroll.NUnit: ObjectContainer.BaseContainer property is null in latest version (2.0.2) #162

Closed mattrenshawrecorder closed 1 month ago

mattrenshawrecorder commented 1 month ago

Reqnroll Version

2.0.2

Which test runner are you using?

NUnit

Test Runner Version Number

NUnit3TestAdapater: 4.50, NUnit 3.13.2

.NET Implementation

.NET 8.0

Test Execution Method

Visual Studio Test Explorer

Content of reqnroll.json configuration file

N/A

Issue Description

When using the Reqnroll.NUnit v2.0x, i'm getting a NullReferenceException when trying to access the ObjectContainer.BaseContainer property in a BeforeTestRunHook:

[BeforeTestRun]
    public static void BeforeTestRun(ObjectContainer objectContainer)
    {
        var exitCode = Microsoft.Playwright.Program.Main(["install", "chromium"]);
        if (exitCode != 0)
        {
            throw new Exception($"Playwright exited with code {exitCode}");
        }

        var configuration = new ConfigurationBuilder()
            .AddJsonFile("appsettings.integration.json")
            .AddEnvironmentVariables()
            .Build()
            .Get<EnvironmentConfiguration>();

        objectContainer.BaseContainer.RegisterInstanceAs(configuration); // Exception thrown here during test execution, .BaseContainer is null
        objectContainer.BaseContainer.RegisterInstanceAs(new JsonSerializerOptions { WriteIndented = true });
    }

The same code works when downgrading to Reqnroll.NUnit 1.0.1

Steps to Reproduce

  1. Create a new Reqnroll project, using NUnit as the test runner
  2. Upgrade Reqnroll.NUnit to v2.0.x
  3. Add a BeforeTestRun hook, with a parameter of type ObjectContainer
  4. Add code within the hook to register an instance of a class using the BaseContainer property
  5. Run the default sample test that is created as part of the project

Expected: The test runs and passes

Actual: An exception is thrown when registering objects in the object container. Downgrading to v1.0.1 prevents this error from occurring. image

Link to Repro Project

https://github.com/mattrenshawrecorder/ObjectContainerIssueRecreation

gasparnagy commented 1 month ago

The requirement to use the BaseContainer was a workaround for the bug #58 that is fixed now. So you need to simply remove the BaseContainer, because the container you got is already the global (test run) container.