c# - 取消测试(带有警告)

标签 c# visual-studio unit-testing nunit mstest

我的项目有一些单元测试。它们都依赖于一些我无法控制的外部服务。该服务经常宕机(回想一下 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.

有用的链接: -What is the Inconclusive Test Condition?

关于c# - 取消测试(带有警告),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11140697/

相关文章:

iphone - Snow Leopard 中的 Xcode 3.2 挂起运行单元测试

c# - ASP.Net C# - 将变量传递给 MySQL 查询

C# 轻量级方法来存储具有快速访问的大位矩阵

c# - 在 ASP .NET CORE 中更新 Web 应用程序替换文件或删除文件,更新时如何在应用程序中保留文件

visual-studio - NEON 指令在 Windows CE 7 上引发异常

c - Vala 的单元测试框架

c# - 测试派生类时如何模拟基本方法

c# - Web 服务返回具有空字段的对象

c# - 导航到另一帧后如何停止 MediaPlayerElement 播放音频?

c# - VS intellisense 如何知道匿名类在编译时的属性?