簡體   English   中英

如何在運行時跳過單元測試?

[英]How to skip a Unit Test at runtime?

提前致謝!

我們使用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
   }
}

我提出了一些需要如何連接到MSTest管道並說是否存在此屬性並且MyFeature1的邏輯已關閉的方法,然后不要運行此測試-看過動態添加[忽略]但沒有運氣。

這是通過VSTS運行的,我可以使用[TestCategories],但是我必須不斷更新打開/關閉功能的管道,而這是我不想做的。

任何幫助或建議將是巨大的!

根據我的閱讀 ,您可能需要使用Assert.Inconclusive

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()
    {
        //...
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM