Skip to content

[Bug]: Virtual hook methods are called for base classes in inherited tests #5450

@Lexy2

Description

@Lexy2

Description

Related to #5428.

If I have to test the same logic for different cultures and inherit the test class, hooks are still executed twice.

Expected Behavior

Overridden test hook is executed once

Actual Behavior

Overridden test hook is executed twice.

Steps to Reproduce

public class BaseTestClass
{
    [Before(Test)]
    public virtual async Task SetupAsync()
    {
        Console.WriteLine("Base setup");
    }
}

public abstract class TestClass : BaseTestClass
{
    [Before(Test)]
    public override async Task SetupAsync()
    {
        await base.SetupAsync();
        Console.WriteLine("Complemented setup");
    }
}

[Culture("en-US")]
[InheritsTests]
public TestClass_enUS : TestClass {}

[Culture("de-DE")]
[InheritsTests]
public TestClass_deDE : TestClass {}

TUnit Version

1.29.0

.NET Version

.NET 10

Operating System

Windows

IDE / Test Runner

dotnet CLI (dotnet test / dotnet run)

Error Output / Stack Trace

Base setup
Complemented setup
Base setup
Complemented setup

Additional Context

A workaround is to remove the [Before(Test)] attribute on the abstract class.

IDE-Specific Issue?

  • I've confirmed this issue occurs when running via dotnet test or dotnet run, not just in my IDE

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions