【问题标题】:Expect exceptions in nUnit without the ExpectedException attribute在没有 ExpectedException 属性的情况下预期 nUnit 中的异常
【发布时间】:2023-03-12 06:05:01
【问题描述】:

我有带有多个参数的方法,只要任何参数为空,就通过抛出 ArgumentNullExceptions 和 ArgumentExceptions 来防止输入错误。

所以有两种明显的方法来测试这个:

  • 使用 [ExpectedException] 属性对每个参数进行一次测试
  • 使用多个 try{} catch 块对所有参数进行一次测试

try catch 是这样的:

try 
{
    controller.Foo(null, new SecondParameter());
    Assert.Fail("ArgumentNullException wasn't thrown");
} catch (ArgumentNullException)
{}

有一个小问题。如果测试通过,则永远不会调用 Assert.Fail,因此将突出显示为未覆盖的测试代码(由 NCover)。

我知道这实际上不是问题,因为这是我想要 100% 覆盖的业务代码,而不是测试代码。我仍然很好奇是否有一种方法可以将多个抛出异常的调用压缩到一个测试用例中而不会有死的 LoC?

【问题讨论】:

    标签: unit-testing exception nunit


    【解决方案1】:

    好吧,您可以通过提取实用程序方法将其减少到一个死线,例如

    public void ExpectException<T>(Action action) where T : Exception
    {
        try
        {
            action();
            Assert.Fail("Expected exception");
        }
        catch (T)
        {
            // Expected
        }
    }
    

    调用它:

    ExpectException<ArgumentNullException>
        (() => controller.Foo(null, new SecondParameter());
    

    (当然,您不需要将其包装在 IDE 中...... SO 上的行长很短。)

    【讨论】:

      【解决方案2】:

      来自release notes of NUnit 2.4.7 NUnit 现在在其扩展程序集中包含由 Andreas Schlapsi 编写的 RowTest 扩展。此扩展允许您编写带有参数的测试方法,并使用 RowAttribute 提供多组参数值。要使用 RowTest,您的测试必须引用 nunit.framework.extensions 程序集。

      它将 MbUnit 的 RowTest 功能添加到 NUnit。

      你可以这样写:

      [RowTest]
      [Row(1, 2, 3)]
      [Row(3, 4, 8, TestName="Special case")]
      [Row(10, 10, 0, TestName="ExceptionTest1"
          , ExpectedException=typeof(ArgumentException)
          , ExceptionMessage="x and y may not be equal.")]
      [Row(1, 1, 0, TestName="ExceptionTest2"
          , ExpectedException=typeof(ArgumentException)
          , ExceptionMessage="x and y may not be equal.")]
      public void AddTest(int x, int y, int expectedSum)
      {
        int sum = Sum(x, y);
        Assert.AreEqual(expectedSum, sum);
      }
      

      http://www.andreas-schlapsi.com/2008/03/31/nunit-247-includes-rowtest-extension/ 代码来自Google codeNunit RowTestExtension 的源代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-25
        • 2017-08-07
        • 2013-02-07
        • 1970-01-01
        • 2011-08-25
        • 2014-04-12
        • 1970-01-01
        • 2021-12-26
        相关资源
        最近更新 更多