Nunit ExpectedException 是否正常工作? [英] Is Nunit ExpectedException Working Correctly?
问题描述
在设置一些命令行脚本来测试我正在处理的项目的 NUnit 测试套件时,我早些时候开始遇到问题.
我注意到在 Visual Studio
或 Xamarin
中运行测试时,结果是我所期望的,但是当使用 nunit 从命令行运行时-console
(mac v2.4.8
) 它不会通过某些测试.
所有在控制台中失败的测试都使用 [ExpectedException]
属性(包括类型和一般).当改为使用 Assert.Throws<>
时,它在 IDE 和命令行中都能正常工作.
这是 Nunit 或我拥有的特定版本/平台的错误吗?
这是 unit-console
2.4.8 中的一个错误,与一位评论者建议的 VS 测试运行程序无关.我在 cmd-line 上使用 3.x 来解决它,方法是在我将它添加到我的 Makefile 中时执行本地 nuget 安装,并且可以执行make test".
通过 2.4.8 进行测试(并暴露错误/问题):
<块引用>nunit-console -nologo -labels except.dll
***** except.Test.ExpectedException***** except.Test.ExpectedNotSystemException***** except.Test.ExpectedNotTypeOfSystemException***** except.Test.NotExpectedException测试运行:4,失败:1,未运行:0,时间:0.106 秒测试用例失败:1)except.Test.NotExpectedException:System.Exception:Stackoverflow在/Users/sushi/code/XamTests/except/except/Test.cs:33 中的 except.Test.NotExpectedException () [0x00006]at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)在 System.Reflection.MonoMethod.Invoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfoculture) [0x00038] 在/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~~~错误和失败1)失败:except.Test一项或多项子测试有错误2)失败:except.Test.ExpectedNotSystemException抛出了意外的异常类型3)失败:except.Test.ExpectedNotTypeOfSystemException抛出了意外的异常类型4) 错误:except.Test.NotExpectedException试运行总结总体结果:失败测试运行:4,通过:1,错误:1,失败:2,不确定:0未运行:0,无效:0,忽略:0,显式:0,跳过:0开始时间:2016-01-26 23:09:56Z结束时间:2016-01-26 23:09:56Z持续时间:0.117 秒
测试用例:
使用NUnit.Framework;使用系统;命名空间除外{[测试夹具()]公开课测试{[测试 ()][预期异常]公共无效预期异常(){抛出新异常(Stackoverflow");}[测试 ()][ExpectedException("System.DivideByZeroException")]public void ExpectedNotSystemException(){抛出新异常(Stackoverflow");}[测试 ()][ExpectedException(typeof(DivideByZeroException))]public void ExpectedNotTypeOfSystemException (){抛出新异常(Stackoverflow");}[测试 ()]public void NotExpectedException(){抛出新异常(Stackoverflow");}}}
I started having problems earlier when setting up some command line scripts to test my NUnit suite of tests for a project I am working on.
I noticed that when running the tests in Visual Studio
or Xamarin
that the results are what I expected, but when running from the command line with the nunit-console
(mac v2.4.8
) it doesn't fail some of the tests.
All the of the tests that are failing that don't fail in the console use the [ExpectedException]
attribute (both with a type and in general). When changing to use Assert.Throws<>
it works correctly in both the IDEs and the command line.
Is this a bug with Nunit or the particular version/platform I have?
It is a bug in unit-console
2.4.8 and has nothing to due with VS test runner as one commenter suggested. I use 3.x on the cmd-line to work around it by doing a local nuget install as I add this into my Makefile and can do a 'make test'.
Testing via 2.4.8 (and exposing the bug/issue):
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
Local 3.0.1 install and test (works correctly):
nuget install 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
Test Case:
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 ExpectedException 是否正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!