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

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

Open
Piotrreek opened this issue Oct 16, 2024 · 1 comment
Open
Labels

Comments

@Piotrreek
Copy link

Piotrreek commented Oct 16, 2024

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();
    }
} 
@Piotrreek Piotrreek added the Bug label Oct 16, 2024
@DeLaphante
Copy link

Try updating to .net 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants