- 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.
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.
Or we can run them from the Test Pad by choosing Select in Test Pad or from the Menu select View > Pads > Unit Tests.
The Test Pad displays all the Tests in the solution where you can run selected or all tests.
The Results of your tests are then displayed in the Test Results Pad.
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.