Setting Up Xamarin (Android) Unit Tests in Visual Studio 2017

Setting Up Xamarin (Android) Unit Tests in Visual Studio 2017

5 April 2024 Stephan Petzl Leave a comment Tech-Help

Developing Xamarin Android applications in Visual Studio 2017 involves various types of testing to ensure the stability and quality of your application. Understanding how to implement and run unit tests is crucial for verifying the logic and functionality of your code. In this guide, we’ll walk you through the steps to set up and run unit tests for your Xamarin Android application.

Understanding Testing Levels

Before diving into the setup, it’s important to know the three basic levels of testing in the Xamarin environment:

  • Classic Unit Testing: This includes pure .Net/Mono code testing with frameworks like xUnit or NUnit. These tests are independent of Xamarin.Android or iOS and do not involve any platform frameworks.
  • On-Platform Testing: This involves testing platform features such as Networking, Bluetooth, GPS, etc., but excludes GUI related tests. Xamarin provides NUnitLite versions that run on Android and iOS, offering a device-specific UI to run these tests. They can reference NUnit tests written in PCL-based assemblies or platform-specific libraries.
  • UI Testing: This involves testing UI elements and their reactions to input events using tools like Calabash or Appium. Tests can be run on local devices or through services like Xamarin Test Cloud.

Setting Up Unit Tests

To set up unit tests in your Xamarin Android application, you can follow these steps:

  1. Create a new project in your solution of type “Unit Test App (Android)” or “Class Library (Android)” for NUnit-based tests.
  2. If you’re using a Class Library (Android), install the NUnit package via NuGet. Ensure you target the appropriate MonoAndroid version.
  3. Add your test classes and methods, annotated with the [TestFixture] and [Test] attributes respectively.

Running Unit Tests

To execute unit tests on your Xamarin Android application, you’ll need to deploy and run the tests on an emulator or a physical device:

  1. Set your Android project as the startup project in Visual Studio.
  2. Select the hardware or emulator you wish to use for executing the unit tests.
  3. Run the application. This will deploy the unit test project to the device.
  4. Once deployed, open the app, and tap on the project name. A list of available tests will appear.
  5. Execute and debug your tests by tapping on each one.

Note: NUnit version 3.x may not be supported for Xamarin UI testing, and you may need to use NUnit 2.6.x instead.


Setting up unit tests in Xamarin Android applications requires an understanding of the different testing levels and the appropriate setup for each. By following the steps outlined above, you can effectively implement and run unit tests to ensure your application’s logic is sound. Remember to deploy and run tests on an actual device or emulator to simulate real-world usage and identify any potential issues.

Like this article? there’s more where that came from!