Search code examples
c#unit-testingxamarinxamarin-studio

How can I create and run tests on PCL code in Xamarin?


This seems relatively simple but I'm not able to find a direct step by step guide anywhere. I have a simple PCL class I've created that is platform independent and I'd like to write a unit test for it. Right now I'm stuck with my test code in an iOS view controller and can only run it by running the app in the simulator. (Xamarin's documentation isn't that helpful, as they focus on view testing.)

I've added a new project to my solution by using the .NET -> Unit Library Project template. There's a default test in there called Test where I added a failure:

using NUnit.Framework;
using System;

namespace Tests
{
    [TestFixture ()]
    public class Test
    {
        [Test ()]
        public void TestCase()
        {
            Console.WriteLine ("this is a test");
            Assert.Fail ("this should fail");
        }
    }
}

I can't for the life of me figure out how to run this test from within the Xamarin IDE. I'd assume I could right-click on either my new Test project and run the whole thing or right-click on this specific class and run it directly. When I right-click on the Test project, I see the following options:

right-click options on test project

My Test project structure looks like:

directory structure for test project

If there's no other option, I would accept an answer that tells me how to run this on the command line.

FWIW, I'm on a Mac running Xamarin Studio 5.9.7 (build 9).


Solution

  • View -> Pads -> Unit Tests will bring up the unit testing window: enter image description here

    You can run your tests there by clicking Run All or by right clicking on a test suite then selecting Run Test, Debug Test etc:

    enter image description here

    enter image description here