NUnit用于可移植类库测试

时间:2021-11-08 15:40:44

Trying to run tests that are written in regular PCL library using NUNit. Its just an empty project, with single test that does nothing.

尝试运行使用NUNit在常规PCL库中编写的测试。它只是一个空项目,只有一个测试什么都不做。

NUnit console runner shows message

NUnit console runner显示消息

The NUnit 3.0 driver does not support the portable version of NUnit. Use a platform specific runner.

NUnit 3.0驱动不支持NUnit的便携版本。使用特定平台的运行程序。

and Resharper runner is crashing with message

Resharper runner则在发送消息

Unit Test Runner failed to run tests

单元测试运行失败

and a stack trace:

堆栈跟踪:

at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
   at System.Activator.CreateInstance(String assemblyString, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo, StackCrawlMark& stackMark)
   at System.Activator.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityInfo)
   at System.AppDomain.CreateInstance(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   at System.AppDomain.CreateInstanceAndUnwrap(String assemblyName, String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, Evidence securityAttributes)
   at NUnit.Engine.Drivers.NUnit3FrameworkDriver.Load(String testAssemblyPath, IDictionary`2 settings)
   at NUnit.Engine.Runners.DirectTestRunner.LoadPackage()
   at NUnit.Engine.Runners.AbstractTestRunner.Load()
   at NUnit.Engine.Runners.MasterTestRunner.LoadPackage()
   at NUnit.Engine.Runners.AbstractTestRunner.EnsurePackageIsLoaded()
   at NUnit.Engine.Runners.MasterTestRunner.NUnit.Engine.ITestRunner.Explore(TestFilter filter)
   at JetBrains.ReSharper.UnitTestRunner.nUnit30.BuiltInNUnitRunner.<>c__DisplayClass1.<RunTests>b__0() in c:\Build Agent\work\10282fe47e6c6213\Psi.Features\UnitTesting\nUnit\Runner30\Src\BuiltInNUnitRunner.cs:line 79
   at JetBrains.ReSharper.UnitTestRunner.nUnit30.BuiltInNUnitRunner.WithExtensiveErrorHandling(IRemoteTaskServer server, Action action) in c:\Build Agent\work\10282fe47e6c6213\Psi.Features\UnitTesting\nUnit\Runner30\Src\BuiltInNUnitRunner.cs:line 623

According to this comment PCL should work, but it does not. If we are using PCL without Xamarin what are the options to execute tests?

根据这条评论,PCL应该可以工作,但它不能。如果我们使用PCL而不使用Xamarin,那么执行测试的选项是什么?

1 个解决方案

#1


6  

To run portable tests, you need to use NUnitLite to create a self-executing test runner. Basically, you create a .NET 4.5.x console application, reference NUnitLite and your test assembly. You then run the console application to run your tests. See Testing .NET Core using NUnit 3 for the steps except that it uses .NET Core instead of .NET 4.5 to run the tests.

要运行可移植测试,您需要使用NUnitLite创建一个自动执行的测试运行器。基本上,你创建了。net 4.5。控制台应用程序,参考NUnitLite和您的测试程序集。然后运行控制台应用程序来运行测试。有关步骤,请参见使用NUnit 3测试。net Core,但是它使用。net Core而不是。net 4.5来运行测试。

If the assembly you are testing is PCL but has .NET 4.5 as a target, an easier approach is to create your test assembly as .NET 4.5.x and reference the PCL assembly under test. This will cause your tests to reference the full version of the NUnit framework and allow you to run your tests in Visual Studio or with Resharper. Just because the code you are working on is PCL, it doesn't mean your tests need to be PCL.

如果您正在测试的程序集是PCL,但是以。net 4.5为目标,那么更简单的方法是将您的测试程序集创建为。net 4.5。x和参考测试中的PCL组件。这将导致您的测试引用NUnit框架的完整版本,并允许您在Visual Studio或Resharper环境中运行测试。仅仅因为您正在处理的代码是PCL,并不意味着您的测试需要是PCL。

#1


6  

To run portable tests, you need to use NUnitLite to create a self-executing test runner. Basically, you create a .NET 4.5.x console application, reference NUnitLite and your test assembly. You then run the console application to run your tests. See Testing .NET Core using NUnit 3 for the steps except that it uses .NET Core instead of .NET 4.5 to run the tests.

要运行可移植测试,您需要使用NUnitLite创建一个自动执行的测试运行器。基本上,你创建了。net 4.5。控制台应用程序,参考NUnitLite和您的测试程序集。然后运行控制台应用程序来运行测试。有关步骤,请参见使用NUnit 3测试。net Core,但是它使用。net Core而不是。net 4.5来运行测试。

If the assembly you are testing is PCL but has .NET 4.5 as a target, an easier approach is to create your test assembly as .NET 4.5.x and reference the PCL assembly under test. This will cause your tests to reference the full version of the NUnit framework and allow you to run your tests in Visual Studio or with Resharper. Just because the code you are working on is PCL, it doesn't mean your tests need to be PCL.

如果您正在测试的程序集是PCL,但是以。net 4.5为目标,那么更简单的方法是将您的测试程序集创建为。net 4.5。x和参考测试中的PCL组件。这将导致您的测试引用NUnit框架的完整版本,并允许您在Visual Studio或Resharper环境中运行测试。仅仅因为您正在处理的代码是PCL,并不意味着您的测试需要是PCL。