Add Unit test to your Xamarin.Forms projects.
As developers in general, there is always a concern of creating high quality code. You may ask yourself. What is high quality code?
In my case as part of a small team of mobile developers creating a medical enterprise application, writing Unit test for our features is a key fact of creating high quality code.
Why is that? Well having unit test in your project may minimize the chances of breaking important functionality while doing changes or refactoring a specific piece of code.
This is going to be a step by step guide on how I implement Unit tests in my current Xamarin.Forms enterprise application (It may have things to improve! so let me know any comments):
- For the purpose of this tutorial I am using Visual Studio for Mac. The Unit Test project can be added to any type of Xamarin.Forms project Shared/PCL/.NetStandard. We are also going to use NUnit
1.- To your solution add a new NUnit Library Project:
For default it will add you a "Test" class with a blank TestCase:
Open the Test pad on Visual Studio (For mac: View -> Test), Expand the UnitTest project until you see your TestCases, double click on it to run it and... voilà!!
Congratulations your first simple test case is ready!!
2.- Add a reference to your PCL/Shared/.NET Standard project on your Unit Test library project, which shuold be:
YourUnitTestProject/References -> Edit References...
3.- We begin the fun part... Add the Xamarin.Forms and Xamarin.Forms Mocks NuGet package to your NUnit Test project:
Now you are going to be able to mock the Xamarin.Forms initialization along side with the most common functionality of the library. (More info: here)
4.-Most important- Here is where building and creating a project correctly following design and architectural patterns is very important! We are going to follow the Dependency Injection pattern to Mock the
DependencyService and mock as many functionality as we need on our complex or simple View Models.
4.1 Add the
DependenyServiceStub class to your NUnit test project, that is going to serve as a wrapper to register mocks to services of your Xamarin.Forms DependencyServices.
4.2 Inside your PCL/Shared/.Net Standard project add
DependencyServiceWrapper class and
4.3 We will need all the previos steps to try a more complex unit test that depends from the "Dependency Service" to function, so let's imaging this scenario in our View Model:
4.4 Create the interface that is going to be used on the server side class:
In order to Unit test this, we need to modify our View Model in order to "mock" the Xamarin.Forms dependency service so using our previous steps we must modify it:
So this is the thing! all your View models need to have this two types of constructors in order to be unit testable (I haven't found another solution to this, so improvements are very much appreciated). So let's modify our unit test case:
5.- Add Moq NuGet package to your Unit test project:
5.1- Modify the simple test case that we currently have so it is based on our new View Model:
As you can see, now we can assert the response from our simple (fake) web service with a mock in our Unit test, and you can start to go nuts by creating as many complex or simple scenarios for you view models in your unit tests.
Run the unit test on the test pad and there you go! Unit test on your Xamarin.Forms project.
As always the source code of this tutorial is available at:
Please let me know any comments or questions.