Cross platform unit tests with mock
Following my post on the framework SimpleStubs for UWP Unit tests, I’ll show how to do a cross platform unit test.
The idea is not to test the PCL in a “neutral” way , but test it on the 3 platforms supported by Xamarin (Android, iOS and UWP). All by reusing the same code for the tests, of course.
Creation of projects
For iOS and Android, it’s easy, simply create the basic Xamarin unit test project
For UWP, we will also create the basic unit test project, but we’ll turn it in unit test UWP Xamarin style.
Firstly, create the project
Then, remove all references to the Visual Studio test framework :
Because Xamarin use NUnit, and in order to use exactly the same source code, we’ll also use NUnit for UWP tests
Add now the Xamarin.NUnit and Xamarin Forms nugets
We create now the page that contains the tests
And, finally, we add these lines in the code-behind file of the application’s OnLaunch method, after “rootFrame.NavigationFailed += OnNavigationFailed;” :
Now let’s remove predefined tests that are in all projects.
And the projects are now ready!
Adding the tests
To begin, we will add a reference to the tested library and SimpleStubs nuget in each project:
Then, add a Shared project. It is in this project that we will write tests. Like this:
Add a reference to this project to all projects unit tests, and here we are!
That is how it look like when run on each platform:
The solution looks like this:
The code can be found here