【问题标题】:Is Nunit ExpectedException Working Correctly?Nunit ExpectedException 工作正常吗?
【发布时间】:2016-01-26 16:41:26
【问题描述】:

我之前在设置一些命令行脚本来测试我正在处理的项目的 NUnit 测试套件时开始遇到问题。

我注意到在Visual StudioXamarin 中运行测试时,结果符合我的预期,但是当使用nunit-console (mac v2.4.8) 从命令行运行时,它不会失败一些的测试。

在控制台中未失败的所有测试都使用[ExpectedException] 属性(包括类型和一般情况)。当更改为使用 Assert.Throws<> 时,它在 IDE 和命令行中都能正常工作。

这是 Nunit 的错误还是我拥有的特定版本/平台的错误?

【问题讨论】:

  • 问题在于混合了两个测试框架——该属性是一个 NUnit 无法理解的 .NET 开箱即用的测试实用程序。使用内置的 VS 测试运行程序时,这些测试通过。但是,Nunit 测试运行器不会拾取属性的含义。 Assert.Throws 对这两个运行器都有效,因为 NUnit 提供了一个 VS 插件,可以增强 VS 测试运行器以与 NUnit 兼容。希望这会有所帮助。
  • 测试失败是因为没有抛出异常还是因为抛出了不同的异常?
  • 测试不会抛出。当属性存在时,他们全权通过。

标签: c# visual-studio xamarin nunit


【解决方案1】:

这是unit-console 2.4.8 中的一个错误,正如一位评论者所建议的那样,与 VS 测试运行程序无关。我在 cmd-line 上使用 3.x 来解决它,方法是执行本地 nuget 安装,因为我将它添加到我的 Makefile 中并且可以执行“make test”。

通过 2.4.8 进行测试(并暴露错误/问题):

nunit-console -nologo -labels except.dll

***** except.Test.ExpectedException
***** except.Test.ExpectedNotSystemException
***** except.Test.ExpectedNotTypeOfSystemException
***** except.Test.NotExpectedException

Tests run: 4, Failures: 1, Not run: 0, Time: 0.106 seconds

Test Case Failures:
1) except.Test.NotExpectedException : System.Exception : Stackoverflow
at except.Test.NotExpectedException () [0x00006] in /Users/sushi/code/XamTests/except/except/Test.cs:33
at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)
at System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in /private/tmp/source-mono-mac-4.2.0-branch-c6sr1/bockbuild-mono-4.2.0-branch/profiles/mono-mac-xamarin/build-root/mono-4.2.2/mcs/class/corlib/System.Reflection/MonoMethod.cs:295

本地 3.0.1 安装和测试(正常工作):

nuget 安装 nunit.runners

./NUnit.Console.3.0.1/tools/nunit3-console.exe except.dll

NUnit Console Runner 3.0.5813
~~~  
Errors and Failures

1) Failed : except.Test
One or more child tests had errors
2) Failed : except.Test.ExpectedNotSystemException
An unexpected exception type was thrown
3) Failed : except.Test.ExpectedNotTypeOfSystemException
An unexpected exception type was thrown
4) Error : except.Test.NotExpectedException

Test Run Summary
    Overall result: Failed
   Tests run: 4, Passed: 1, Errors: 1, Failures: 2, Inconclusive: 0
     Not run: 0, Invalid: 0, Ignored: 0, Explicit: 0, Skipped: 0
  Start time: 2016-01-26 23:09:56Z
    End time: 2016-01-26 23:09:56Z
    Duration: 0.117 seconds

测试用例:

using NUnit.Framework;
using System;

namespace except
{
    [TestFixture ()]
    public class Test
    {
        [Test ()]
        [ExpectedException]
        public void ExpectedException ()
        {
            throw new Exception ("Stackoverflow");
        }

        [Test ()]
        [ExpectedException("System.DivideByZeroException")]
        public void ExpectedNotSystemException ()
        {
            throw new Exception ("Stackoverflow");
        }

        [Test ()]
        [ExpectedException(typeof(DivideByZeroException))]
        public void ExpectedNotTypeOfSystemException ()
        {
            throw new Exception ("Stackoverflow");
        }

        [Test ()]
        public void NotExpectedException ()
        {
            throw new Exception ("Stackoverflow");
        }
    }
}

【讨论】:

  • NUnit 2.4.8 是古董!
  • 但这需要与 Mono 安装捆绑并在 Xamarin Studio 中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多