我们正在运行每晚构建,最后使用MsTest框架运行所有UnitTest.
我们必须有100%的通过率,所以如果一个失败,其他人就没有意义; 因此我们希望在第一次失败的测试中停止执行.
反正我们能做到吗?
您真的确定要丢弃测试结果吗?
假设某人今天心情不好,并在您的代码中引入了多个错误 A、B 和 C。您可能只在星期一才发现问题 A,因此您修复了该问题,而直到星期二您才知道问题 B,然后直到星期三才修复问题 C。但由于您错过了半周的测试覆盖范围,周一引入的错误直到引入几天后才会被发现,而且修复它们的成本更高,而且需要更长的时间。
如果在失败后继续运行测试不需要花费太多成本,那么这些信息不是很有用吗?
也就是说,在测试库中进行修复并不是那么困难。让所有可能的测试失败代码路径设置一个静态变量StopAssert.Failed = true;(可能通过包装调用Assert并捕获 )AssertFailedExceptions。然后只需[TestInitialize()]向每个测试类添加方法即可在失败后停止每个测试!
public class StopAssert
{
public static bool Failed { get; private set; }
public static void AreEqual<T>(T expected, T actual)
{
try
{
Assert.AreEqual(expected, actual);
}
catch
{
Failed = true;
throw;
}
}
// ...skipping lots of methods. I don't think inheritance can make this easy, but reflection might?
public static void IsFalse(bool condition)
{
try
{
Assert.IsFalse(condition);
}
catch
{
Failed = true;
throw;
}
}
}
[TestClass]
public class UnitTest1
{
[TestInitialize()]
public void Initialize()
{
StopAssert.IsFalse(StopAssert.Failed);
}
[TestMethod]
public void TestMethodPasses()
{
StopAssert.AreEqual(2, 1 + 1);
}
[TestMethod]
public void TestMethodFails()
{
StopAssert.AreEqual(0, 1 + 1);
}
[TestMethod]
public void TestMethodPasses2()
{
StopAssert.AreEqual(2, 1 + 1);
}
}
[TestClass]
public class UnitTest2
{
[TestInitialize()]
public void Initialize()
{
StopAssert.IsFalse(StopAssert.Failed);
}
[TestMethod]
public void TestMethodPasses()
{
StopAssert.AreEqual(2, 1 + 1);
}
[TestMethod]
public void TestMethodFails()
{
StopAssert.AreEqual(0, 1 + 1);
}
[TestMethod]
public void TestMethodPasses2()
{
StopAssert.AreEqual(2, 1 + 1);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1530 次 |
| 最近记录: |