Creating a Xamarin Forms App Part 7 : Unit Testing

  • Part 1 : Introduction
  • Part 2 : Getting Started
  • Part 3 : How to use Xamarin Forms with Visual Studio without the Business Edition
  • Part 4 : Application Resources
  • Part 5 : Dependency Injection
  • Part 6 : View Model First Navigation
  • Part 7 : Unit Testing
  • Part 8 : Consuming a RESTful Web Service
  • Part 9 : Working with Alerts and Dialogs
  • Part 10 : Designing and Developing the User Interface
  • Part 11 : Updating to Xamarin Forms 1.3
  • Part 12 : Extending the User Interface

I’ve covered quite a lot so far in this series but one thing that I’ve left out up until now is unit testing. I didn’t want unit tests to distract from the subjects I’ve covered so far. I now need to address this as I believe unit tests are a very important part of the development process.

I am also taking this opportunity to refactor my solution a little. I wasn’t happy with the name I’d given for my app and the resulting solution and projects. I realised that I actually want to develop more than just a Mountain Forecast App. I want the app to include historical weather observations and current weather conditions as well as a forecast. I also want it to include other information relating to the weather conditions for each mountain area. The app is actually all about Mountain Weather so I have renamed it Mountain Weather. This required renaming all the projects, namespaces and the solution together with some classes and some project settings. The updated solution can be found at the end of this post.

So on to unit testing. Xamarin Studio comes with an NUnit test project template which adds the NUnit reference. Select Add > New Project, then select NUnit Library Project from the NUnit folder. I have added NUnit test projects for both my Core project and my Shared PCL project.

Screen Shot 2014-11-22 at 10.49.23

Screen Shot 2014-11-22 at 10.51.02

Note that I have named these the same as the projects they are testing, suffixed with .Tests. In each project I have added test fixtures for all my classes. I name these the same as the class they are testing, suffixed with Fixture. e.g. MountainAreaViewModelFixture.

Xamarin Studio allows you to run your tests directly from a test fixture by clicking on the indicator in the margin and choosing Run or Debug.

Screen Shot 2014-11-22 at 21.09.19

Or we can run them from the Test Pad by choosing Select in Test Pad or from the Menu select View > Pads > Unit Tests.

Screen Shot 2014-11-22 at 21.15.06

The Test Pad displays all the Tests in the solution where you can run selected or all tests.

Screen Shot 2014-11-22 at 21.12.43

The Results of your tests are then displayed in the Test Results Pad.

Screen Shot 2014-11-22 at 21.13.07

I am also using Moq to mock dependencies which I easily added using NuGet. I highly recommend you use this or something similar like Rhino Mocks as it makes creating mocks very easy.

I have attempted to cover as much code as possible but unfortunately there is no code coverage statistics available for Xamarin Studio. I might try running code coverage in Visual Studio at some point. I’m not going to go through any Tests in this post as they are self-explanatory.

 

8 thoughts on “Creating a Xamarin Forms App Part 7 : Unit Testing

  1. Great blog series Jonathan. Aside from being a very gifted developer you also have a knack for creating an interesting and informative blog.

    Like

  2. A few tests fail (eg. ApplicationResourceExtensionFixture) with an exception that Xamarin.Forms.Init must be called prior to running this test … how to solve that ? I cannot call Xamarin.Forms.init from the test itself …

    Great series of blogs otherwise !! I’m reading them back anf forward and have learned so much

    Like

    1. Hi,

      I’m sure I didn’t used to be necessary to call Init for this. Perhaps this has changed.
      I think I actually deleted this test as I found it a bit worthless. Did you resolve this?

      Like

  3. Hello Jonathan, thanks for this nice tutorial.
    I have an issue while adding Moq in xamarin forms project. Could not install package ‘Moq 4.2.1510.2205’. You are trying to install this package into a project that targets ‘Xamarin.iOS,Version=v1.0’, but the package does not contain any assembly references or content files that are compatible with that framework.

    Did you tried Moq package ?

    Like

  4. Moq is really amazing, you can fake even dialogs results (even async)

    I was facing the issue above, so I created a ModuleTest to manager my fake classes.

    Like

Leave a comment