提前致谢!
我们使用 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],但我必须不断更新打开/关闭功能的管道,这是我不想做的。
任何帮助或建议都会很棒!
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()
{
//...
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)