2

我的项目有一些单元测试。它们都依赖于一些我无法控制的外部服务。该服务经常停机(回想一下 2008 年左右的 Twitter)。目前,如果服务关闭,则测试失败,并且无法构建持续集成服务器。我很反感这个。我想忽略依赖此设置的测试,打印警告。

[BeforeScenario]

try
{
    connect(server)
}
catch (Exception x)
{
    throw new Exception(String.Format("Failed to connect to server {0}", server), x);
}

我怎样才能做到这一点?我今天碰巧在使用 SpecFlow,但我也想知道如何从 NUnit 做到这一点。

以下是我目前拥有的。没关系。测试被忽略,但构建仍然失败。

[BeforeScenario]

try
{
    connect(server)
}
catch (Exception x)
{      
    throw new SpecFlowCancelTests(String.Format("Failed to connect to server {0}", server), x);
}
4

1 回答 1

3

You can use either Assert.Inconclusive("message") or Assert.Ignore() utility methods:

The Assert.Inconclusive method indicates that the test could not be completed with the data available. It should be used in situations where another run with different data might run to completion, with either a success or failure outcome.

The Assert.Ignore method provides you with the ability to dynamically cause a test or suite to be ignored at runtime. It may be called in a test, setup or fixture setup method. We recommend that you use this only in isolated cases. The category facility is provided for more extensive inclusion or exclusion of tests or you may elect to simply divide tests run on different occasions into different assemblies.

Useful links: - What is the Inconclusive Test Condition?

于 2012-06-21T14:50:12.577 回答