如何停止MsTest在首次失败时测试执行?
问题描述:
我们正在运行夜间构建,最后运行我们的所有UnitTest使用MsTest框架。如何停止MsTest在首次失败时测试执行?
我们必须有100%的合格率,所以如果一个失败了,没有意义的运行其他人;因此我们希望在第一次失败的测试中停止执行。
反正我们可以做到吗?
答
你确定你想扔掉测试结果吗?
说别人有糟糕的一天,并将多个错误A,B和C引入您的代码中。你可能只会在周一发现A,所以你解决了这个问题,直到星期二你才知道问题B,然后到星期三你才解决C.但是,由于您错过了半个星期的测试覆盖范围,因此周一发布的错误直到推出后几天才被发现,而且它更昂贵并且需要更长的时间才能修复它们。
如果在失败后继续运行测试并不花费太多,那么这些信息是否有用?
也就是说,在测试库中一起修复修复并不会那么困难。有测试失败的所有可能的codepaths设置一个静态变量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);
}
}
+0
任何人都有办法做到这一点与配置设置?我们有很多代码需要修改:( – Steve 2016-02-19 18:06:48
我有同样的问题,任何人? – Zambonilli 2013-06-26 19:04:02