wil*_*lem 14 integration-testing nunit
我们使用NUnit来执行集成测试.这些测试非常耗时.通常,检测故障的唯一方法是超时.
我希望一旦检测到单个故障,测试就会停止执行.
有没有办法做到这一点?
小智 14
我正在使用NUnit 3,以下代码适用于我.
public class SomeTests {
private bool stop;
[SetUp]
public void SetUp()
{
if (stop)
{
Assert.Inconclusive("Previous test failed");
}
}
[TearDown]
public void TearDown()
{
if (TestContext.CurrentContext.Result.Outcome.Status == TestStatus.Failed)
{
stop = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
或者,您可以将其设为抽象类并从中派生出来.
Rag*_*ghu -1
这可能不是理想的解决方案,但它满足您的要求,即如果测试失败则忽略剩余的测试。
[TestFixture]
public class MyTests
{
[Test]
public void Test1()
{
Ascertain(() => Assert.AreEqual(0, 1));
}
[Test]
public void Test2()
{
Ascertain(() => Assert.AreEqual(1, 1));
}
private static void Ascertain( Action condition )
{
try
{
condition.Invoke();
}
catch (AssertionException ex)
{
Thread.CurrentThread.Abort();
}
}
}
Run Code Online (Sandbox Code Playgroud)
由于 TestFixtureAttribute 是可继承的,因此您可以创建一个基类,并在其上装饰此属性,并在其中包含 Ascertain protected Method 并从中派生所有 TestFixture 类。
唯一的缺点是,您必须重构所有现有的断言。
| 归档时间: |
|
| 查看次数: |
7686 次 |
| 最近记录: |