如何在运行时跳过单元测试?

How to skip a Unit Test at runtime?

提前致谢!

我们使用 selenium 网络驱动程序进行了一些自动化测试,这些测试非常棒,并且提供了非常好的回归包。

问题是现在我们的代码中有功能切换。所以我需要说忽略这些测试,除非该功能切换已打开/关闭。我找不到真正搜索的内容 Google。

理想情况下,我不希望在功能测试的顶部使用 'if' 语句,但看起来它将成为主要方式。我最初的想法是在何处创建自定义属性

public class IsFeatureFlagTurnedOn : Attribute
{
   public IsFeatureFlagTurnedOn(string featureToggleName)
   {
      FeatureToggleName = featureToggleName;
   }
   public string FeatureToggleName {get;}
}

public class MyTests 
{
   [TestMethod]
   [IsFeatureFlagTurnedOn("MyFeature1")]
   public void ItShould()
   {
      // only run if MyFeature1 is turned on
   }
}

我知道如何需要连接到 MSTest 管道并说明如果此属性存在并且 MyFeature1 的逻辑已关闭则不要 运行 此测试 - 查看动态添加 [忽略]但没有运气。

这是通过 VSTS 运行宁,我可以使用 [TestCategories],但我必须不断更新功能转向的管道 on/off,我不想这样做.

任何帮助或建议都将非常有用!

根据我对 this, you may need to use Assert.Inconclusive

的阅读

MSTest v2 现在有很多扩展点,您可以通过扩展 TestMethodAttribute 来实现。首先,我们添加两个属性参数,一个 string 用于 属性 名称,一个 Type 具有 属性。然后我们覆盖 Execute 方法并通过反射调用 属性 。如果结果是 true,我们将正常执行测试,否则我们将 return 一个“不确定”的测试结果。

public class TestMethodWithConditionAttribute : TestMethodAttribute
{
    public Type ConditionParentType { get; set; }
    public string ConditionPropertyName { get; set; }

    public TestMethodWithConditionAttribute(string conditionPropertyName, Type conditionParentType)
    {
        ConditionPropertyName = conditionPropertyName;
        ConditionParentType = conditionParentType;
    }

    public override TestResult[] Execute(ITestMethod testMethod)
    {
        if (ConditionParentType.GetProperty(ConditionPropertyName, BindingFlags.Static | BindingFlags.Public)?.GetValue(null) is bool condiiton && condiiton)
        {
            return base.Execute(testMethod);
        }
        else
        {
            return new TestResult[] { new TestResult {  Outcome = UnitTestOutcome.Inconclusive } };
        }
    }
}

现在我们可以像这样使用我们的新属性:

[TestClass]
public class MyTests
{
    [TestMethodWithCondition(nameof(Configuration.IsMyFeature1Enabled), typeof(Configuration))]
    public void MyTest()
    {
        //...
    }
}

public static class Configuration
{
    public static bool IsMyFeature1Enabled => false;
}

以上是一个非常通用的解决方案。您还可以根据您的特定用例对其进行更多自定义,以避免在属性声明中过于冗长:

public class TestMethodForConfigAttribute : TestMethodAttribute
{
    public string Name { get; set; }

    public TestMethodForConfigAttribute(string name)
    {
        Name = name;
    }

    public override TestResult[] Execute(ITestMethod testMethod)
    {
        if (IsConfigEnabled(Name))
        {
            return base.Execute(testMethod);
        }
        else
        {
            return new TestResult[] { new TestResult {  Outcome = UnitTestOutcome.Inconclusive } };
        }
    }

    public static bool IsConfigEnabled(string name)
    {
        //...
        return false;
    }
}

并像这样使用它:

[TestClass]
public class MyTests
{
    [TestMethodForConfig("MyFeature1")]
    public void MyTest()
    {
        //...
    }
}