Running Tests in the IDE

PDF for offline use

Let us know how you feel about this

Translation Quality


0/250

last updated: 2017-04

This guide will discuss how to run UITests in either Xamarin Studio or Visual Studio.

Overview

During the development of an app, or after making changes to an existing app, developers like to have rapid feedback that everything is working. Xamarin.UITests, written using the NUnit testing framework, can be run from within Xamarin Studio during development and provide developers with confirmation that the application is functioning as expected. Xamarin Studio has built in support for the NUnit framework and can run Xamarin.UITests and display the test results. During the development of an app, or after making changes to an existing app, developers like to have rapid feedback that everything is working. Xamarin.UITests, written using the NUnit testing framework, can be run from within Visual Studio during development and provide developers with confirmation that the application is functioning as expected.

Visual Studio requires a third party plug-in, the NUnit Test Adapter for Visual Studio, before it is possible to run Xamarin.UITests from within the IDE. The Xamarin templates for Visual Studio will install the NUnit Test Adapter.

ℹ️ Do not use the Nunit3TestAdapter for running Xamarin.UITests.

This guide will discuss how to run Xamarin.UITests in Xamarin Studio or Visual Studio and view the test results.

Requirements

This guide assumes that you already have a solution for a mobile application that includes a UITest project.

Xamarin Test Cloud and Xamarin.UITest only support Android and iOS applications. The Windows Phone and Windows versions of Xamarin.Forms apps cannot be tested in Test Cloud.

It is important to understand the concepts described in the Introduction to Xamarin Test Cloud guide.

It is assumed that the most recent stable version of the Xamarin.UITest NuGet Package is installed in the UITest project. It is assume that iOS projects have the most recent version of the Xamarin Test Cloud Agent installed.

In order to run Xamarin.UITests with Xamarin Studio, the following dependencies must be met:
  • NUnit 2.6.x – Xamarin.UITest is not compatible with NUnit 3.x.
  • Android SDK – Only if testing Android apps.
  • Java Developers Kit – Only if testing Android apps.
  • Xcode Command Line Tools – Only for testing iOS apps.
In order to run Xamarin.UITests with Visual Studio, the following dependencies must be met:
  • NUnit 2.6.x – Xamarin.UITest is not compatible with NUnit 3.x.
  • A Test Runner for Visual Studio – A 3rd party test runner, such as the NUnit Test Adapter for NUnit 2 or Resharper from Jetbrains, is required for Visual Studio to be able to run the NUnit tests.
  • Android SDK – Only if testing Android apps. Windows requires that the ANDROID_HOME environment variable is set with the path to the Android SDK.
  • Java Developers Kit – Only if testing Android apps.
⚠️

It is not possible to run UITests for iOS apps in Visual Studio or on Windows.

Running Tests in the IDE

To run all of the unit tests in the solution, select Run > Run Unit Tests from the Xamarin Studio menu. This will cause the Unit Tests pad to appear. The Test Apps leaf of the tree shows the Xamarin.Android and the Xamarin.iOS projects along with the target of the test run:

The unit test pad

The target identifies the physical device/Android emulator/iOS simulator that the tests will be run on. To run the tests, click the Run All button in the Unit Tests pad. Xamarin Studio will perform the following steps:

  • Compile the solution.
  • If necessary, start up the emulator or simulator identified by the test target.
  • Deploy the app to the target and start the application.
  • Run all the UITests.
  • Collect the results and display them in either the Unit Test pad or the Test Results pad:

    Test results in the Unit Test Pad

    Test results in the Test Results Pad

To run a individual test or tests for a specific device, right click on the test and select Run Test from the context:

Running an individual test

Changing the Target

It is possible to select a different target to run the UITests on. Right click on the App Project under Test Apps in the Unit Tests pad, and select the new target for the test:

Selecting a new test target

In Visual Studio, it is possible for the NUnit Test Adapter to deploy and run the UITests on an Android device and to display the test results inside the IDE.

  • First, select the UITest project and add a reference to the Xamarin.Android project that is to be tested:

    Adding the reference to the project

    By doing this, UITest will automatically configure the IApp instance in the UITests to use the APK from the Xamarin.Android project that is referenced.

  • Next, open the Test Explorer; by select Test > Windows > Test Explorer from the Visual Studio menu:

    Opening the Test Explorer

  • Now it is necessary to specify the select the target device that the tests will run on. This can be any Android emulator or physical device:

    Selecting the target device

  • Finally, run the tests by selecting Test > Run > All Tests from the Visual Studio menu, or pressing Ctrl+R,A. This will cause Visual Studio deploy the APK, run the UITests, and display the output of the tests in the Test Explorer:

    Showing the test results

To run a individual test or tests for a specific device, right click on the test and select Run Selected Tests from the context menu:

Running an individual test

ℹ️

UITest will not be able to run Android tests if the device is locked. You must unlock the device before the test may be run.

When running tests locally, ensure that the compiled application targets a CPU architecture that is appropriate for the device running the tests:

TargetArchitecture
iOS Simulatorx86
iOS devicex86_64
Android DeviceTypically armeabi-v7a
Google EmulatorDepends on the Android Virtual Device

Running Android 6.0 Applications from the IDE

When the IDE installs an Android 6.0 app for the first time, it does not grant all permissions required by the application. The workaround for this is to use UITest to install and start the application:

ConfigureApp.Android.Debug().ApkFile(apkpath).StartApp ()

Do use ApkFile() and do not use PreferIdeSettings(). This will force UITest to install instead of the IDE.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.