我的项目有一些单元测试。它们都依赖于一些我无法控制的外部服务。该服务经常宕机(回想一下 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);
}
最佳答案
您可以使用 Assert.Inconclusive("message")或Assert.Ignore()实用方法:
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.
关于c# - 取消测试(带有警告),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11140697/