Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit for Portable Class Library tests

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 console runner shows message

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

and Resharper runner is crashing with message

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?

like image 288
Sly Avatar asked May 18 '16 12:05

Sly


People also ask

What type of testing does NUnit support?

NUnit is an open-source unit testing framework for the . NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.

Is NUnit a test runner?

The nunit.exe program is a graphical runner. It shows the tests in an explorer-like browser window and provides a visual indication of the success or failure of the tests.

Why NUnit is used?

The NUnit Framework caters to a range of attributes that are used during unit tests. They are used to define Test -Fixtures, Test methods, ExpectedException, and Ignore methods.


1 Answers

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.

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.

like image 51
Rob Prouse Avatar answered Oct 07 '22 19:10

Rob Prouse