admin管理员组

文章数量:1355612

I'm using a template method for tests with an abstract base class.

The abstract class is just a template with code that will be used in each derived class, and when some part of the code differs, I create an abstract method that is implemented in the derived class.

In this case, the derived classes do not have tests, only implementations of these abstract methods. You can only run tests from derived classes.

I would like to be able to enforce a skip only for one particular derived class. Is there a reasonable way to do this? Theoretically, I could insert the same tests in each derived class and skip only in one, but I don't want redundancy in the code and that is terrible solution.

Worth to mention that I'm not able to use Assert.Skip, since don't use xunit v3.

How my code works:

Example base class:

public abstract class BaseTestClass : TestBase
{
    public BaseTestClass(BaseFixture fixture, ITestOutputHelper TestOutputHelper) 
        : base(fixture, testOutputHelper) { }

    protected abstract string ExampleMethod();

    [Fact]
    public async Task ExampleTest()
    {
        //arrange

        var valueSameInAllFlows1 = 1;
        var valueSameInAllFlows2 = 2;

        var exampleStringDifferentInEachFlow = ExampleMethod();

        //act 
        //....

        //assert
        //....
    }
}

First example derived class:

[Collection(DerivedFixtureA.CollectionName)]
public sealed class DerivedClassA : BaseTestClass
{
    public DerivedClassA(DerivedFixtureA fixture, ITestOutputHelper testOutputHelper)
        : base(fixture, testOutputHelper) { }

    protected override string ExampleMethod()
    {
        return "derived A";
    }
}

Second example derived class:

[Collection(DerivedFixtureB.CollectionName)]
public sealed class DerivedClassB : BaseTestClass
{
    public DerivedClassB(DerivedFixtureB fixture, ITestOutputHelper testOutputHelper)
        : base(fixture, testOutputHelper) { }

    protected override string ExampleMethod()
    {
        return "derived B";
    }
}

Let's say I only want to skip test in DerivedClassB and I want them truly skipped.

Not just if(condidtion) return; approach.

How I'm supposed to do it?

Tried to create my own attribute, but I am not able to check the fixture from there.

本文标签: cIs there a way to skip Xunit tests only in one derived classStack Overflow