How to run unit tests for Xamarin.Mac class library?

I have Xamarin.Mac class library, which targets Xamarin.Mac Modern. To test it I created another Xamarin.Mac class library and added reference to nUnit 3.11.0 added several tests to it. Unit Tests are detected by Visual Studio for Mac, but when I try to run them, I get exceptions like the following:

One or more errors occurred.
Stack trace:
  at System.Threading.Tasks.Task.ThrowIfExceptional (System.Boolean includeTaskCanceledExceptions) [0x00013] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Task.cs:2166 
  at System.Threading.Tasks.Task`1[TResult].GetResultCore (System.Boolean waitCompletionNotification) [0x0002b] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/mcs/class/referencesource/mscorlib/system/threading/Tasks/Future.cs:562 
  at System.Threading.Tasks.Task`1[TResult].get_Result () [0x00000] in /Users/builder/jenkins/workspace/build-package-osx-mono/2018-02/external/bockbuild/builds/mono-x64/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) [0x001f6] in /Users/vsts/agent/2.141.1/work/1/s/monodevelop/main/src/addins/MonoDevelop.UnitTesting.NUnit/MonoDevelop.UnitTesting.NUnit/NUnitAssemblyTestSuite.cs:451 
  at MonoDevelop.UnitTesting.NUnit.NUnitAssemblyTestSuite.OnRun (MonoDevelop.UnitTesting.TestContext testContext) [0x00000] in /Users/vsts/agent/2.141.1/work/1/s/monodevelop/main/src/addins/MonoDevelop.UnitTesting.NUnit/MonoDevelop.UnitTesting.NUnit/NUnitAssemblyTestSuite.cs:364 
  at MonoDevelop.UnitTesting.UnitTest.Run (MonoDevelop.UnitTesting.TestContext testContext) [0x0001c] in /Users/vsts/agent/2.141.1/work/1/s/monodevelop/main/src/addins/MonoDevelop.UnitTesting/Services/UnitTest.cs:289 

I tried to simplify code and here are steps to reproduce the problem:
1. Create Xamarin.Mac Class Library (it must target Xamarin.Mac Modern)
2. Add reference to nUnit 3.11.0
3. Add the following code:


using System; using NUnit.Framework; namespace SampleLibrary { [TestFixture] public class SampleLibraryTests { [Test] public void Test() { var value = 1; Assert.AreEqual(value, 1); } } }
  1. Run test

So, my question is, how to run unit tests for Xamarin.Mac Class Library that targets Xamarin.Mac Mobile framework? Is there something I missed?.

Tagged:

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    So there are two things to consider:

    • Are you going to use any Cocoa specific APIs. Anything from AppKit or some of Foundation? If so, you must initialize the Cocoa one and only one time, correctly. This is a bit non-trivial, and I would strongly suggest you using https://github.com/garuma/GuiUnitNg as your runner in this case.

    • If you are a XM library but not using anything in Cocoa, I'd first suggest changing your library to a .net standard 2.0 library and then using the "standard" test runners.

    • If for some reason you aren't in either of those camps, please provide more detail on your use case. :)

Sign In or Register to comment.