Getting Started with Appium and NUnit framework

Appium is widely used for Mobile Automation Testing. Here’s your guide how you can run Appium tests with NUnit with example

guide-banner-qals-1x
Home Guide Getting Started with Appium and NUnit framework

Getting Started with Appium and NUnit framework

Appium with NUnit is a powerful combination for testing mobile applications on Android and iOS. While Appium provides the automation library, NUnit acts as the test runner, making it easy for .NET and C# developers to structure and execute mobile test cases efficiently. This guide explains how to set up, write, and run your first Appium NUnit test.

Overview

What Do You Need Before Running Appium with NUnit?

  • Visual Studio for Mac (or Windows) – to write and run your C# test scripts.
  • Appium server – for interacting with mobile apps on Android or iOS.
  • Android SDK and Virtual Devices – to create emulators for testing if physical devices aren’t available.
  • NUnit Framework – the test runner to manage setup, execution, and teardown of your test cases.

How to Set Up Appium and NUnit on Your System?

  • Install Visual Studio for Mac → Enable Android SDK and create a virtual device using Device Manager.
  • Set Up Appium Server → Enter host IP (127.0.0.1) and configure environment variables like ANDROID_HOME.
  • Create an NUnit Project → Add the required NuGet packages (Appium libraries, NUnit adapters).
  • Prepare the Test Structure → Define Config files and Helper Classes for reusable capabilities and environment details.

How to Write Your First Appium NUnit Test?

  • Setup: Defines desired capabilities and initializes the AndroidDriver.
  • Test: Performs an action (e.g., search feature validation in a demo app).
  • TearDown: Closes the driver to free resources.
    Tests can be executed directly in Visual Studio by selecting Run Test from the NUnit window.

Why Should You Test on Real Devices?

  • More Accurate Results: Emulators may not replicate real-world conditions like network drops or device-specific bugs.
  • Wide Device Coverage: Test across both older and latest devices.
  • BrowserStack Advantage: Run Appium NUnit tests on a real-device cloud, use parallel testing, and drastically reduce execution time.

This article covers what Appium with NUnit is, how to set it up, how to write your first test, and why running tests on real devices gives the most reliable results.

Prerequisites for running Appium Tests with NUnit

Before you start writing your first test in Appium with NUnit, you need below setup to be completed in our System (This article focuses on setting up tools in macOS and similar setup is also possible in Windows)

  1. Visual Studio for Mac
  2. Appium

Visual Studio for Mac setup

Once the installation of Visual studio for mac is completed, open the Visual studio for next setup

  1. Click on Tools > SDK Manager
    SDK Manager Menu selection
  2. In the Android Platform, Enable Android API and Android OS, this will download required images for running the Android Virtual Devices. Click on Apply Changes.
    SDK manager
  3. Next step is to create a Virtual Device. To do that, click on Tools > Device Manager
    Device Manager Menu selection
  4. Android Device Manager window will be displayed like below, you can now create a New Device, or manage Existing Devices.
    Android Device Manager Screen
  5. Upon clicking on the New Device button, you will see below the new Virtual device creation window. Using this you can provide device name and select its Configuration. After selecting configuration, click on Create and this will create a new Virtual device.
    Android Device Creation
  6. Once the device is created, it will list in the Android Device Manager window. You can start the Virtual device by clicking on Play button
    Android Device Manager
  7. Our new Android virtual device will be started and it looks like below

Android Emulator

Appium Setup

Once the installation is completed, on opening the Appium server, the screen will look like below.

Appium Server Before Start

You need to enter the host IP, so let’s enter 127.0.0.1 and before starting the server you need to ensure the ANDROID_HOME variable is defined with the proper path. Enter the android sdk path and click on Save and Restart

Appium Environment Variable Setup

You can now start our Appium server with host IP set as 127.0.0.1

Appium Server 2

Project setup for running Appium Tests with NUnit

Once you create the NUnit Test Project, you need to install below packages

Nuget Packages

Once these packages are added, you can start writing our tests and helpers.

Writing first Appium NUnit test

Using the NUnit framework for creating the test which will test the sample Android Application.

The Test class will have

  • Setup – To set the Desired capabilities and to create Android Driver
  • Test – To perform a Search feature validation in our Android Application
  • TearDown – To tear down the Android Driver

Our Project and Classes will look like below in the Solution

NUnitSolutionOverviewAs you can see there are: Test Class, Config, and the Helper Class, which will have the reusable static methods which will contains

  • Environment Details
  • Application Details (Whether we need to test IOS/Android application, Application name and path etc.)

Below is the code used in Helper Class

using System;

namespace AppiumDotNetSamples.Helper
{
public static class Env
{
public static String rootDirectory = System.IO.Path.GetFullPath($"{System.AppDomain.CurrentDomain.BaseDirectory.ToString()}/../../../..");

static public bool IsBrowserStack()
{
return Environment.GetEnvironmentVariable("BROWSERSTACK") != null;
}

static public Uri ServerUri()
{
String bsUserName = Environment.GetEnvironmentVariable("BROWSERSTACK_USERNAME");
String bsAccessKey = Environment.GetEnvironmentVariable("BROWSERSTACK_ACCESS_KEY");

return (bsUserName == null) || (bsAccessKey == null)
? new Uri("http://localhost:4723/wd/hub")
: new Uri($"https://cloudURL:80/wd/hub");
}

public static TimeSpan INIT_TIMEOUT_SEC = TimeSpan.FromSeconds(180);
public static TimeSpan IMPLICIT_TIMEOUT_SEC = TimeSpan.FromSeconds(10);
}

public static class App
{
static public String IOSApp()
{
return Env.IsBrowserStack() ? "http://appium.github.io/appium/assets/TestApp7.1.app.zip" : $"{Env.rootDirectory}/apps/TestApp.app.zip";
}

static public String IOSDeviceName()
{
return Environment.GetEnvironmentVariable("IOS_DEVICE_NAME") ?? "iPhone 6s";
}

static public String IOSPlatformVersion()
{
return Environment.GetEnvironmentVariable("IOS_PLATFORM_VERSION") ?? "11.4";
}

static public String AndroidApp()
{
Console.WriteLine(Env.rootDirectory);
return Env.IsBrowserStack() ? "http://appium.github.io/appium/assets/ApiDemos-debug.apk" : $"{Env.rootDirectory}/apps/ApiDemos-debug.apk";
}

static public String AndroidDeviceName()
{
return Environment.GetEnvironmentVariable("ANDROID_DEVICE_VERSION") ?? "Android";
}

static public String AndroidPlatformVersion()
{
return Environment.GetEnvironmentVariable("ANDROID_PLATFORM_VERSION") ?? "12.0";
}
}
}

Let’s see how our Setup/BeforeAll method looks like

[SetUp()]
public void BeforeAll()
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(MobileCapabilityType.BrowserName, "");
capabilities.SetCapability(MobileCapabilityType.PlatformName, App.AndroidDeviceName());
capabilities.SetCapability(MobileCapabilityType.PlatformVersion, App.AndroidPlatformVersion());
capabilities.SetCapability(MobileCapabilityType.AutomationName, "UIAutomator2");
capabilities.SetCapability(MobileCapabilityType.DeviceName, "Nexus");
capabilities.SetCapability("appActivity", ".app.SearchInvoke");
capabilities.SetCapability(MobileCapabilityType.App, App.AndroidApp());

driver = new AndroidDriver<AndroidElement>(Env.ServerUri(), capabilities, Env.INIT_TIMEOUT_SEC);
driver.Manage().Timeouts().ImplicitWait = Env.IMPLICIT_TIMEOUT_SEC;
}

As seen, all the Desired Capabilities are set and then passed to AndroidDriver for creating a new driver object.

AndroidDriver class constructor accepts three objects

  1. Server URI – Which will be http://localhost:4723/wd/hub when you run locally
  2. Desired Capabilities – Which specifies Mobile Capabilities
  3. Timeout for the commands

As explained earlier, we will be finding an element in the demo app and entering a text for search, and then will validate if the result contains searched text. For this the test class will look like below.

using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium;
using OpenQA.Selenium.Appium.Enums;
using OpenQA.Selenium.Appium.Android;
using OpenQA.Selenium.Remote;
using System;
using AppiumDotNetSamples.Helper;

namespace AppiumDotNetSamples
{
[TestFixture()]
public class AndroidBasicInteractionsTest
{
private AndroidDriver<AndroidElement> driver;

[SetUp()]
public void BeforeAll()
{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.SetCapability(MobileCapabilityType.BrowserName, "");
capabilities.SetCapability(MobileCapabilityType.PlatformName, App.AndroidDeviceName());
capabilities.SetCapability(MobileCapabilityType.PlatformVersion, App.AndroidPlatformVersion());
capabilities.SetCapability(MobileCapabilityType.AutomationName, "UIAutomator2");
capabilities.SetCapability(MobileCapabilityType.DeviceName, "Nexus");
capabilities.SetCapability("appActivity", ".app.SearchInvoke");
capabilities.SetCapability(MobileCapabilityType.App, App.AndroidApp());

driver = new AndroidDriver<AndroidElement>(Env.ServerUri(), capabilities, Env.INIT_TIMEOUT_SEC);
driver.Manage().Timeouts().ImplicitWait = Env.IMPLICIT_TIMEOUT_SEC;
}

[TearDown()]
public void AfterAll()
{
driver.Quit();
}

[Test()]
public void TestShouldSendKetsToSearchBoxThenCheckTheValue()
{
AndroidElement searchBoxElement = driver.FindElementById("txt_query_prefill");
searchBoxElement.SendKeys("Hello World!");

AndroidElement onSearchRequestButton = driver.FindElementById("btn_start_search");
onSearchRequestButton.Click();

AndroidElement seachText = driver.FindElementById("android:id/search_src_text");
Assert.AreEqual("Hello World!", seachText.Text);
}

}
}

To run this test, simply double click on the test name in Tests window or right click on test name and select Run

RunTest

If we execute our test, it will look like below in the Android Emulator

AppiumRun

Complete example repository of the code explained can be found here

Test on Real Devices

This article discussed, how to set up the tools required for running Appium tests using NUnit. Appium, an open source test automation framework is very powerful and can be used to test almost all workflows/use cases of Mobile Application across Platforms. Since Appium commands are like Selenium to find elements and interact with them, it will become familiar for a Selenium based tester to switch to Appium with less learning curve.

However, to get more accurate test results, it is suggested to test on real devices. BrowserStack’s real device cloud provides all the mobile devices both latest and older models in the platform.

Check out the Official Documentation to learn how to Run Appium Tests with NUnit using BrowserStack.

Using BrowserStack’s real devices you can also reduce your testing time drastically by leveraging Mobile Test Automation and running parallel tests.

Run Appium with NUnit Tests on BrowserStack

Tags
Appium Automation Testing Mobile App Testing

Get answers on our Discord Community

Join our Discord community to connect with others! Get your questions answered and stay informed.

Join Discord Community
Discord