Marco Eckstein
Marco Eckstein

Reputation: 4758

How can I combine IApplyToTest with TestFixtureSource to get descendant tests?

I have a custom attribute:

[AttributeUsage(AttributeTargets.Class)]
sealed class MyAttribute : NUnitAttribute, IApplyToTest
{
    // ...

    public void ApplyToTest(Test test)
    {
        // I need to get all descendant tests of parameter 'test' here.
    }

    // ...
}

If I combine it with a TestFixture attribute like this, things work as expected:

[TestFixture(1)]
[TestFixture(2)]
[MyAttribute]
class MyTests
{
    // ...
}

ApplyToTest is then called with a TestFixture, which has a non-empty test.Tests property which I can use to recursively get all descendant tests.

However, if I use TestFixtureSource, this approach breaks down:

[TestFixtureSource(nameof(GetTestFixtureSource))]
[MyAttribute]
class MyTests
{
    static IEnumerable<int> GetTestFixtureSource() => new List<int>() { 1, 2 };

    // ...
}

Now, ApplyToTest is called with a ParameterizedFixtureSuite which has an empty test.Tests property.

How can I solve this problem?

Upvotes: 1

Views: 43

Answers (1)

Marco Eckstein
Marco Eckstein

Reputation: 4758

So I've found somewhat of a solution, or at least a workaround.

The attribute needs to be changed like this:

[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class)]
sealed class MyAttribute : NUnitAttribute, IApplyToTest
{
    // ...
}

Instead of a test class, it needs to be applied to the assembly:

[assembly: MyAttribute]

Now, ApplyToTest is called with a TestAssembly, which has a non-empty test.Tests property which I can use to recursively get all tests.

Upvotes: 0

Related Questions