gpt4 book ai didi

c# - 如何在 CurrentEnv 为 Prod 时忽略 C# 中的测试

转载 作者:太空宇宙 更新时间:2023-11-03 15:03:56 27 4
gpt4 key购买 nike

创建了一个从 IgnoreAttribute 扩展而来的 ProdIgnoreAttribute。我已将此属性分配给某些我想在 DEV/QA 中运行但不在 PROD 中运行的测试。

ApplyToTest(Test test) method is not being called in this case. How to resolve this?

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

最佳答案

如何扩展 Attribute 而不是 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("ignored because xyz")]

关于c# - 如何在 CurrentEnv 为 Prod 时忽略 C# 中的测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44684073/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com