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

use*_*226 5 c# unit-testing mstest featuretoggle

提前致谢!

我们有一些使用 selenium web 驱动程序的自动化测试,它们很棒,并且提供了一个非常好的回归包。

问题是现在我们的代码中有功能切换。所以我需要说忽略这些测试,除非该功能切换被打开/关闭。我找不到任何真正在 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
   }
}
Run Code Online (Sandbox Code Playgroud)

我有些需要挂钩到 MSTest 管道并说明是否存在此属性并且 MyFeature1 的逻辑已关闭,然后不要运行此测试 - 查看动态添加 [Ignore] 但没有运气。

这是通过 VSTS 运行的,我可以使用 [TestCategories] 但我必须不断更新我不想打开/关闭的功能的管道。

任何帮助或建议都会很棒!

Dav*_*e M 5

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

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 } };
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

public static class Configuration
{
    public static bool IsMyFeature1Enabled => false;
}
Run Code Online (Sandbox Code Playgroud)

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

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

[TestClass]
public class MyTests
{
    [TestMethodForConfig("MyFeature1")]
    public void MyTest()
    {
        //...
    }
}
Run Code Online (Sandbox Code Playgroud)