Quick Links

Unit Test a Shared Project

Hi have a shared project which contains no platform-specific code. I created a standard Visual Studio Unit Test project (not an Android or iOS Unit Test project) with the intention of unit testing some of the classes in the shared project. I made my test project reference the shared project, but when it builds I get build errors. That is because a few of the source files reference some assemblies such as Xamarin.Forms for referencing primitives such as Size. I don't seem to have a way to reference these Xamarin assemblies in my standard test project.

Generally, what is the technique I should be using to test my shared project without having to make a platform specific test project?

0

Best Answer

I put out a few months ago a nuget library 'SpecFlow.Xamarin.Forms' that facilitates unit testing in Xamarin.forms in the console runner (ie for visual studio, team city, etc). It makes it easy to test your ViewModels / Commands/ Internal Navigation. In order to keep the test as close to the truth as possible, Xamarin.Forms.Init() is still called by the testing framework, so until we can flag that on/off in the Xamarin.Forms framework, you'll have to catch it.
Now, this was meant to be used with PCL projects, not shared.
But you should be able to maybe create a pcl or .net standard project that references your shared project, then follow the steps on the link below to test that assembly.

You can find the step by step tutorial here.

5

Answers

I put out a few months ago a nuget library 'SpecFlow.Xamarin.Forms' that facilitates unit testing in Xamarin.forms in the console runner (ie for visual studio, team city, etc). It makes it easy to test your ViewModels / Commands/ Internal Navigation. In order to keep the test as close to the truth as possible, Xamarin.Forms.Init() is still called by the testing framework, so until we can flag that on/off in the Xamarin.Forms framework, you'll have to catch it.
Now, this was meant to be used with PCL projects, not shared.
But you should be able to maybe create a pcl or .net standard project that references your shared project, then follow the steps on the link below to test that assembly.