c# - 如何在 MSTest 中强制将测试状态设置为“通过”?
问题描述
您能否建议如何在 MSTest 中强制将测试状态设置为“通过”?假设我重新运行了 2 次相同的测试——一次失败,第二次通过,但结果还是“失败”……我需要让它“通过”。这是重新运行测试的代码示例。但是如果第一次运行失败并且第二次运行通过,它仍然在最终输出中显示测试结果为“失败”
protected void BaseTestCleanup(TestContext testContext, UITestBase type)
{
if (testContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
if (!typeof(UnitTestAssertException).IsAssignableFrom(LastException.InnerException.GetType()))
{
var instanceType = type.GetType();
var testMethod = instanceType.GetMethod(testContext.TestName);
testMethod.Invoke(type, null);
}
}
}
解决方案
TestCleanup
方法来不及检查UnitTestOutcome
。如果出于某种原因您想运行两次测试,则必须在其中创建自己的TestMethodAttribute
覆盖Execute
方法。这是一个示例,您可以如何做到这一点:
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject1
{
public class MyTestMethodAttribute : TestMethodAttribute
{
public override TestResult[] Execute(ITestMethod testMethod)
{
TestResult[] results = base.Execute(testMethod);
bool runTestsAgain = false;
foreach (TestResult result in results)
{
if (result.Outcome == UnitTestOutcome.Failed)
{
result.Outcome = UnitTestOutcome.Passed;
runTestsAgain = true;
}
}
if (runTestsAgain)
{
// Run them again I guess...
}
return results;
}
}
[TestClass]
public class UnitTest1
{
[MyTestMethod]
public void TestMethod1()
{
Assert.IsTrue(false);
}
}
}
使用此解决方案,您的测试将始终是绿色的。
推荐阅读
- python - 如何使用 Flask 添加新的 html 项目
- javascript - 如何在不立即执行“onclick”功能的情况下在我的文本区域中有“必需”?
- node.js - 如何发布包含几个 Express 端点的单个节点文件
- android - 适用于 Android 的自定义条目渲染器中的清除按钮问题
- javascript - 如何将 CreateJS 导入 VueJS .vue 组件?
- reactjs - 不变量失败:您不应该使用
外面 - mfcc - Librosa MFCC 特征提取
- git - 为什么我的 github 帐户连接到命令行?
- python - 使用 Pandas 将单列字典(如文本)转换为以键为列名的多列
- reactjs - 使用 Jest 和 Enzyme 反应模拟提交表单功能