【问题标题】:How do I Skip xUnit.net test based on certain bool condition in test class?如何根据测试类中的某些布尔条件跳过 xUnit.net 测试?
【发布时间】:2022-05-19 10:30:07
【问题描述】:

我需要根据类中的某些布尔条件跳过测试方法。 可能吗?如何实现?我曾尝试扩展 FactAttribute,但无法获取 Test 类的实例。

我的代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace XUnitTestProject1
{
    public class MyTestClass
    {
        bool SomeCondition;
        public MyTestClass()
        {
            SomeCondition = false;
        }

        [Fact] //I WANT TO SKIP THIS TEST AS SOMECONDITON == FALSE
        void MyTestMethod()
        {

        }

    }
}

【问题讨论】:

  • 不要让逻辑跳过一个测试,而是创建两个测试,将它们放入不同的特征中。所有测试运行器都支持基于 trait 运行/跳过测试。
  • :) 或者你总是可以在测试中做if (i_want_to_skip_this_test == true) return;

标签: xunit


【解决方案1】:

你可以这样做:


public class MyTestClass
{
    private const string SomeCondition = "false"

    [Fact(Skip=SomeCondition)]
    void MyTestMethod()
    {

    }
}

【讨论】:

    猜你喜欢
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    相关资源
    最近更新 更多