Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using NUnit with PCL

I seem to be having all kinds of problems trying to get NUint to play nice with a PCL library. My PCL library targets .NET 4.5, Silverlight 4 and higher, Windows Phone 7.5, Windows Store, Mono for Android and MonoTouch (the last two with Xamarin). However, when I try to add NUnit to the project via NuGet it looks like it gets added (nunit.framework is added to the references) but the NUnit namespace isn't available. If I try and add a using NUnit.Framework, Visual studio underlines it and if I try and build I get an error about the NUnit namespace can't be found along with this warning:

Warning 2   The primary reference "nunit.framework" could not be resolved because it has an indirect dependency on the framework assembly "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" which could not be resolved in the currently targeted framework. ".NETPortable,Version=v4.0,Profile=Profile104". To resolve this problem, either remove the reference "nunit.framework" or retarget your application to a framework version which contains "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

Can NUnit work with PCLs? The mvvm cross tutorials certainly seem to manage it.

I tried creating a separate project for testing (a regular class library) and I can add NUnit to that, but then I can't seem to add a reference to my PCL library (the one I want to test), so that doesn't help either.

Any ideas?

like image 498
Matt Burland Avatar asked Jun 14 '13 15:06

Matt Burland


People also ask

Can NUnit be used for test driven development?

NUnit is a developing, open-source framework designed for writing and running tests in Microsoft . NET programming languages. NUnit, like JUnit, is a part of test-driven development (TDD), which is a piece of a bigger programming design paradigm known as Extreme Programming (in XP).

Does NUnit support .NET framework?

NUnit is an open-source unit testing framework for the . NET Framework and Mono.

Is NUnit compatible with .NET core?

The current version of NUnit is 3. It can be installed either from NuGet packages or from . NET Core CLI tool.


1 Answers

This argument has flaws but I will make it anyway...

Go ahead and write and run your unit tests in a .net 4.5 library and reference your PCL library from there. The goal of the unit test it to test the logic. Whilst it is true that some things may be different for the different PCL 'target versions' (methods not implemented etc), I suspect that testing your code this way will cover 99% of cases.

Besides, if you want a 'true' test of the logic, you need to get your unit tests running on the devices that you want to test...and then each version of the OS...and potentially different devices with different CPU's etc. How far do you want to go?

You mentioned that you couldn't add a reference to your PCL from the 'regular' test library...what sort of error do you get? I have been using this approach to test my PCL logic and it works like a charm, perhaps it is not perfect but for the 1% of cases where things are different on the device and things fail...I have an exception harness that logs to a server :-)

like image 129
wallismark Avatar answered Oct 12 '22 23:10

wallismark