Skip to content

Tests from generic base classes are not executed correctly #2602

@samtrion

Description

@samtrion

It seems that TUnit does not discover (dependson chain) and execute tests defined in generic base classes, even when using [InheritsTests] and [DependsOn] appropriately across multiple levels of inheritance.

This issue can be reproduced using the following sample code:

public abstract class FirstClassBase
{
    [Test]
    public Task Test001() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test001))]
    public Task Test002() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test002))]
    public Task Test003() => Task.Delay(50);
}

[InheritsTests]
[DependsOn(typeof(FirstClassBase))]
public abstract class SecondClassBase<TContent, TValue> : FirstClassBase
    where TContent : class
{
    [Test]
    public Task Test100() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test100))]
    public Task Test101() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test101))]
    public Task Test102() => Task.Delay(50);
}

[InheritsTests]
[DependsOn(typeof(SecondClassBase<>))]
public abstract class ThirdClassBase<TContent, TValue> : SecondClassBase<TContent, TValue>
    where TContent : class
{
    [Test]
    public Task Test200() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test200))]
    public Task Test201() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test201))]
    public Task Test202() => Task.Delay(50);
}

[InheritsTests]
[DependsOn(typeof(ThirdClassBase<>))]
public class ComplexGenericDependsOn : ThirdClassBase<string, int>
{
    [Test]
    public Task Test300() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test300))]
    public Task Test301() => Task.Delay(50);

    [Test]
    [DependsOn(nameof(Test301))]
    public Task Test302() => Task.Delay(50);
}

Expected behavior:

All tests from FirstClassBase, SecondClassBase, ThirdClassBase, and ComplexGenericDependsOn should be discovered and executed in the correct dependency order.

Actual behavior:

Tests from all levels of the class hierarchy are discovered. However, during execution, the tests from the generic base classes and the concrete class fail with the message:

No tests found for DependsOn({dependsOnAttribute}) - If using Inheritance remember to use an [InheritsTests] attribute
This happens despite the presence of [InheritsTests] on all relevant classes.`

Additional context:

The issue appears to stem from a limitation in how [DependsOn] processes generic base classes, especially across multiple levels of abstraction. When removing the generic parameters or flattening the hierarchy, the tests are discovered as expected.

Let me know if further diagnostics or a minimal repro repo would help!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions