最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Is there a way to skip Xunit tests only in one derived class? - Stack Overflow

programmeradmin1浏览0评论

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.

发布评论

评论列表(0)

  1. 暂无评论