Xamarin - Documentation

iOS

change platform

  1. Xamarin
  2. iOS
  3. Tutorials
  4. Unit Testing

Unit Testing

This document describes how to create unit tests for your MonoTouch projects.   Unit testing with MonoTouch is done using the Touch.Unit framework that includes both an iOS test runner as well as a modified version of the NUnitLite framework that provides a familiar set of APIs for writing unit tests.

Setting up a Test Project

To setup a unit testing framework for your project, all you need to do is to add to your solution a project of type "MonoTouch Unit Tests Project".   Do this by right-clicking on your solution and selecting "Add/Add New Project".   From the list of projects select MonoTouch/MonoTouch Unit Tests Project:

Screen Shot 2012-02-07 at 9.39.48 PM.png

The above will create a basic project that contains a basic runner program and which references the new MonoTouch.NUnitLite assembly, your project will look like this:

Screen Shot 2012-02-07 at 9.40.46 PM.png

 

The AppDelegate.cs class contains the test runner, and it looks like this:

[Register ("AppDelegate")]
	public partial class AppDelegate : UIApplicationDelegate
	{
		UIWindow window;
		TouchRunner runner;

		public override bool FinishedLaunching (UIApplication app, NSDictionary options)
		{
			// create a new window instance based on the screen size
			window = new UIWindow (UIScreen.MainScreen.Bounds);
			runner = new TouchRunner (window);

			// register every tests included in the main application/assembly
			runner.Add (System.Reflection.Assembly.GetExecutingAssembly ());

			window.RootViewController = new UINavigationController (runner.GetViewController ());
			
			// make the window visible
			window.MakeKeyAndVisible ();
			
			return true;
		}
	}

Writing Some Tests

Now that you have the basic shell in place, you should write your first set of tests.    

Tests are written by creating classes that have the [TestFixture] attribute applied to them.   Inside each TestFixture class you should apply the [Test] attribute to every method that you want the test runner to invoke.   The actual test fixtures can live in any file in your Tests project.

To quickly get started select "Add/Add New File" and select in the MonoTouch group "UnitTests". This will add a skeleton file that contains one passing test, one failing test and one ignored tests, it looks like this:

using System;
using NUnit.Framework;

namespace Fixtures {
	
	[TestFixture]
	public class Tests {
		
		[Test]
		public void Pass ()
		{
			Assert.True (true);
		}

		[Test]
		public void Fail ()
		{
			Assert.False (true);
		}

		[Test]
		[Ignore ("another time")]
		public void Ignore ()
		{
			Assert.True (false);
		}
	}
}

Running Your Tests

To run this project inside your solution right click on it and select "Debug Item" or "Run Item".   

The test runner allows you to see which tests are registered and select individually which tests can be executed.

Screen Shot 2012-02-07 at 9.54.34 PM.pngScreen Shot 2012-02-07 at 9.57.08 PM.pngScreen Shot 2012-02-07 at 9.57.12 PM.png

You can run individual test fixtures by selecting the text fixture from the nested views, or you can run all of your tests with "Run Everything".   If you run the default test that is supposed to include one passing test, one failure and one ignored test.   This is what the report looks like, and you can drill down directly to the failing tests and find out more information about the failure:

Screen Shot 2012-02-07 at 10.00.33 PM.pngScreen Shot 2012-02-07 at 10.00.56 PM.png

You can also look at the Application Output window in MonoDevelop to see which tests are being executed and their current status.   

Writing New Tests

MonoTouch.NUnitLite is a modified version of the NUnitLite project.  NUnitLites is a lightweight testing framework for .NET, based on the ideas in NUnit and providing a subset of its features. It uses minimal resources and will run on resource-restricted platforms such as those used in embedded and mobile development.   You can browse NUnitLite APIs available to you in MonoTouch in our web site.   With the basic skeleton provided by the unit test template, your main entry point are the Assert class methods.  

In addition to the assert class methods, the unit testing functionality is split on the following namespaces that are part of MonoTouch.UnitLite:

The MonoTouch-specific unit test runner is documented here: