6

提前致谢!

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

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

理想情况下,我不想在功能测试的顶部使用“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 的逻辑已关闭然后不运行此测试 - 看着动态添加 [Ignore] 但没有运气。

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

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

4

2 回答 2

6

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 } };
        }
    }
}

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

[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()
    {
        //...
    }
}
于 2018-11-17T20:09:49.253 回答
0

根据我对此的阅读,您可能需要使用Assert.Inconclusive

于 2018-11-16T14:13:59.853 回答