c# - 选择性忽略 NUnit 测试

标签 c# nunit

我想忽略某些基于我在 TestFixtureSetUp 期间从配置文件中提取的数据的测试。有没有办法忽略基于参数运行测试?

[TestFixture]
public class MessagesTests
{
    private bool isPaidAccount;

    [TestFixtureSetUp]
    public void Init () {
        isPaidAccount = ConfigurationManager.AppSettings["IsPaidAccount"] == "True";
    }

    [Test]
    //this test should run only if `isPaidAccount` is true
    public void Message_Without_Template_Is_Sent()
    {
         //this tests an actual web api call.
    }

}

如果我们测试的账户是付费账户,测试应该可以正常运行,否则,该方法将抛出异常。

属性 [Ignore(ReallyIgnore = isPaidAccount )] 会有扩展吗?或者我应该在方法中编写它并为例如运行 2 个单独的测试用例。

    public void Message_Without_Template_Is_Sent()
    {
         if(isPaidAccount)
         {
              //test for return value here
         }
         else
         {
              //test for exception here
         }
    }

最佳答案

您可以像 Matthew 所说的那样使用 Assert.Ignore()。如果您想对结果进行不同的分类,您也可以使用 Assert.Inconclusive()

这个问题/答案有点相似:Programmatically skip an nunit test

关于c# - 选择性忽略 NUnit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18391248/

相关文章:

database - EntityFramework 单元测试示例

c# - 在 Roslyn 的 .net core 中动态选择引用

c# - Nhibernate 项目转化为类属性

java - UDP 传输后将字节编码数据转换回 int32 的问题

visual-studio - 如何从NuGet使用nUnit.Runners

c# - 一段代码在控制台应用程序中工作,但在 nunit 测试中不起作用

c# - 从派生类访问 protected 方法

c# - 在给定 HWND 的情况下如何获取窗口的子窗口?

macos - 如何使用 MStests 和 NUnit 的通用测试工具并在 Mono 中运行它们?

c# - 单元测试和检查私有(private)变量值