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.25k stars 754 forks source link

Specflow xUnit runner not working when running test without debugger #2769

Open Piotrreek opened 1 month ago

Piotrreek commented 1 month ago

SpecFlow Version

3.9.74

Which test runner are you using?

xUnit

Test Runner Version Number

3.9.74

.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

When I create class with [BeforeTestRun] and [AfterTestRun] where I register my api factory (which is WebApplicationFactory) then tests are failing when I run them without debugger. When I run tests from explorer by clicking Debug Tests then everything works fine. Below is the stack trace. I am using Specflow.xUnit in this case. When I simply changed NuGet to Specflow.NUnit then tests are running correctly both with and without debugger.

With xUnit it is also worth to mention, that with [BeforeFeature("featureName)] everything works fine also.

image

Steps to Reproduce

Create class, that has some properties that should be initialized in BeforeTestRun attribute. Try to use this property in step definitions class when running tests with Specflow xUnit runner without debugger. Similar message should appear:

TechTalk.SpecFlow.BindingException Error calling binding method 'Products.Tests.Acceptance:Products.Tests.Integration.Steps.CreateProductStepDefinitions.WhenICreateTheProductWithNameQuantityPriceDescription(String, Int32, Decimal, String)': Value cannot be null. (Parameter 'client')

Link to Repro Project

https://github.com/Piotrreek/Advanced-Programming-Techniques/tree/main/Products/tests/Products.Tests.Acceptance

This is the code of my hooks class and part of step definition class:


[Binding]
public class CreateProductHooks
{
    [BeforeTestRun]
    //[BeforeFeature("CreateProduct")]
    public static async Task RegisterServices(IObjectContainer objectContainer)
    {
        var factory = new ApiFactory();
        await factory.InitializeAsync();
        objectContainer.RegisterInstanceAs(factory);
    }

    [AfterScenario]
    public static async Task CleanDatabase(IObjectContainer objectContainer)
    {
        var factory = objectContainer.Resolve<ApiFactory>();
        await factory.ResetDatabaseAsync();
    }

    [AfterTestRun]
   // [AfterFeature("CreateProduct")]
    public static async Task DisposeAsync(IObjectContainer objectContainer)
    {
        var factory = objectContainer.Resolve<ApiFactory>();
        await factory.DisposeAsync();
    }
}

public class CreateProductStepDefinitions
{
    private readonly HttpClient _client;

    private static string _responseString = string.Empty;

    public CreateProductStepDefinitions(ApiFactory apiFactory)
    {
        _client = apiFactory.Client;
    }

[When("I create the product with name '(.*)', quantity '(.*)', price '(.*)', description '(.*)'")]
    public async Task WhenICreateTheProductWithNameQuantityPriceDescription(string name, int quantity,
        decimal price,
        string description)
    {
        var createProductRequest = new CreateProductRequest(name, quantity, price, description);
        var response = await _client.PostAsJsonAsync("products", createProductRequest);
        _responseString = await response.Content.ReadAsStringAsync();
    }
} 
DeLaphante commented 2 weeks ago

Try updating to .net 8

Romfos commented 1 week ago

Specflow is not longer under development, please check https://github.com/reqnroll/Reqnroll