问题
I am trying to create a unit test project for a Xamarin Mac Class Library project.
.NET NUnit Library Project it doesn't work because I can't add a reference to my Xamarin Mac Class Library so I tried creating a new Xamarin Mac Class Library project and added NUnit Nuget package to it.
<package id="NUnit" version="3.5.0" targetFramework="xamarinmac20" />
For the first part it worked because the tests are visible in Test Manager but unfortunately every time I try to run the unit tests I receive "Load failed".
One or more errors occurred.
Stack trace:
at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00014] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2157
at System.Threading.Tasks.Task`1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x00034] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:562
at System.Threading.Tasks.Task`1[TResult].get_Result () [0x00000] in /private/tmp/source-mono-4.6.0-c8sr0/bockbuild-mono-4.6.0-branch-c8sr0/profiles/mono-mac-xamarin/build-root/mono-x86/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:532
at MonoDevelop.UnitTesting.NUnit.NUnitAssemblyTestSuite.RunUnitTest (MonoDevelop.UnitTesting.UnitTest test, System.String suiteName, System.String pathName, System.String testName, MonoDevelop.UnitTesting.TestContext testContext) [0x0014a] in /Users/builder/data/lanes/3816/fa52f026/source/monodevelop/main/src/addins/MonoDevelop.UnitTesting.NUnit/MonoDevelop.UnitTesting.NUnit/NUnitAssemblyTestSuite.cs:424
回答1:
Xamarin.Mac requires significant hand holding to place native support libraries in the right place / load everything up, and then assurance that you call NSApplication.Init only one time, no matter the number of tests, and so on.
Getting nunit to do that is non-trivial. I suggest trying https://github.com/mono/guiunit.
There are two ways to use guiunit:
- The traditional way where you package your assemblies along with guiunit in a bundle and run them: https://developer.xamarin.com/samples/mac/guiunit-example/
- The less standard way where you create a XM application and include guiunit directly: https://github.com/xamarin/xamarin-macios/blob/master/tests/common/mac/MacTestMain.cs#L16
The traditional way is easier to integrate into existing nunit workflows, while the newer less standard way is easier to debug.
回答2:
I ran into the same issue and ended up in changing the class library's Target Framework from Xamarin.Mac Mobile Framework to Xamarin.Mac .NET 4.5 Framework.
I don't know if this is an option for you.
来源:https://stackoverflow.com/questions/40217848/xamarin-mac-unit-test-project