2017-06-21 125 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