2017-06-21 27 views
2

已經創建了一個從IgnoreAttribute擴展的ProdIgnoreAttribute。我已經將此屬性分配給了某些我想在DEV/QA中運行但不在PROD中執行的測試。當CurrentEnv爲Prod時,如何忽略C#中的測試

在這種情況下不會調用ApplyToTest(測試測試)方法。如何解決這個問題?

public class ProdIgnoreAttribute : IgnoreAttribute 
{ 
private string IgnoreReason { get; } 

public ProdIgnoreAttribute(string reason) : base(reason) 
{ 
    IgnoreReason = reason; 
} 

public new void ApplyToTest(Test test) 
{ 
    if (test.RunState == RunState.NotRunnable) 
     return; 

    if (StaticInfoHelper.VrCurrentEnv == (int)RunEnv.PROD) 
    { 
     test.RunState = RunState.Ignored; 
     test.Properties.Set("_SKIPREASON", (object)IgnoreReason); 
    } 
    else 
    { 
     base.ApplyToTest(test); 
    } 
} 

}

回答

0

如何延伸,而不是屬性IgnoreAttribute?

public class ProdIgnoreAttribute : Attribute, ITestAction 
{ 
    public void BeforeTest(TestDetails details) 
    { 
    bool ignore = StaticInfoHelper.VrCurrentEnv == (int)RunEnv.PROD; 
    if (ignore) 
     Assert.Ignore("Test ignored during Prod runs"); 
    } 

    //stub out rest of interface 
} 

如果你想自定義忽略消息,你可以把它接受一個字符串ProdIgnoreAttribute構造。然後,您可以在測試中使用該屬性,例如:[ProdIgnore(「因爲xyz」而被忽略)]

+0

感謝您的回覆。我調整了該類以擴展和實現:NUnitAttribute,IApplyToTest,然後使用overriden方法: ApplyToTest(測試測試)並忽略了prod中的測試。如果(StaticInfoHelper.VrCurrentEnv ==(int)RunEnv.PROD) { test.RunState = RunState.Ignored; test.Properties.Set(「_ SKIPREASON」,ProdIgnoreReason); } else test.RunState = RunState.Runnable; } – ranp