Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Frameworks and Page Objects

Test Runners and Fixtures: pytest, JUnit and TestNG

Run Appium tests with pytest, JUnit 5 or TestNG: driver fixtures, session scope versus test scope, markers, parametrization, and clean teardown.

Appium itself is only a server that executes commands. It has no idea what a test is, how to report a failure or when to start and stop a session. That job belongs to a test runner: pytest or unittest in Python, JUnit 5 or TestNG in Java, Mocha, Jest or WebdriverIO in JavaScript. The runner finds tests, runs setup and teardown code, collects results and reports them to your CI system.

This lesson focuses on the most important piece of glue: the driver fixture that creates a session before a test and always quits it afterward. Real Appium sessions cannot be created here, so the Appium code is read-only, and the runnable examples use stand-in drivers to show lifecycle and scope.

The driver fixture in pytest

A fixture is a function that prepares something a test needs. Put it in conftest.py so every test file can use it:

Python
# conftest.py
import pytest
from appium import webdriver
from appium.options.android import UiAutomator2Options


@pytest.fixture
def driver():
    options = UiAutomator2Options()
    options.platform_name = "Android"
    options.app = "/builds/app-debug.apk"
    options.auto_grant_permissions = True

    drv = webdriver.Remote("http://127.0.0.1:4723", options=options)
    yield drv          # the test runs here
    drv.quit()         # always runs, even when the test failed

A test asks for it by naming a parameter:

Python
def test_home_title(driver):
    assert driver.find_element("accessibility id", "home-title").is_displayed()

The code after yield is the teardown. Forgetting quit() leaves sessions open on the server or, worse, on a paid cloud device, and the farm keeps billing until the idle timeout.

Stand-in: fixture lifecycle

This runnable example reproduces the same order of events with a context manager so you can see setup and teardown run even when the test raises an error.

Python Setup and teardown order (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fixture scope: speed versus isolation

By default a pytest fixture runs once per test: a new session for every test. That is the safest choice because tests cannot affect each other, but session startup takes several seconds (more on iOS).

Python
@pytest.fixture(scope="session")
def driver():
    ...

A session-scoped driver starts once for the whole run, which is fast but leaves state behind. A middle path is a session-scoped driver plus a function-scoped reset that restarts the app:

Python
@pytest.fixture(scope="session")
def raw_driver():
    drv = create_driver()
    yield drv
    drv.quit()

@pytest.fixture
def driver(raw_driver):
    raw_driver.terminate_app(APP_ID)
    raw_driver.activate_app(APP_ID)
    yield raw_driver

Choose per project: isolation first, then optimize the slowest suites. Never share one device between parallel test workers.

Scope Session per Speed Isolation
function test slowest best
module / class file / class medium medium
session whole run fastest tests share state

Markers and parametrization

Use markers to tag tests and run subsets:

Python
import pytest

@pytest.mark.smoke
def test_app_launches(driver): ...

@pytest.mark.parametrize("email,password,message", [
    ("", "secret", "Email is required"),
    ("[email protected]", "", "Password is required"),
    ("[email protected]", "wrong", "Wrong email or password"),
])
def test_login_errors(driver, email, password, message):
    ...
Terminal
pytest -m smoke
pytest -k login --maxfail=3
pytest --platform ios     # a custom option defined in conftest.py

A custom command-line option lets one suite run on both platforms:

Python
def pytest_addoption(parser):
    parser.addoption("--platform", default="android", choices=["android", "ios"])

Stand-in: a tiny test runner

The next stand-in is a miniature runner with markers and parametrization. It shows what pytest does for you: collect, filter, call and report.

Python A tiny runner with markers (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Java: JUnit 5 and TestNG

The same ideas exist in Java. JUnit 5 uses @BeforeEach and @AfterEach; TestNG uses @BeforeMethod and @AfterMethod and supports parallel execution and data providers natively:

Java
class LoginTest {
    private AndroidDriver driver;

    @BeforeEach
    void start() throws Exception {
        UiAutomator2Options options = new UiAutomator2Options()
            .setApp("/builds/app-debug.apk")
            .setAutoGrantPermissions(true);
        driver = new AndroidDriver(new URL("http://127.0.0.1:4723"), options);
    }

    @AfterEach
    void stop() {
        if (driver != null) driver.quit();
    }

    @Test
    void homeTitleIsShown() {
        assertTrue(driver.findElement(AppiumBy.accessibilityId("home-title")).isDisplayed());
    }
}

Note the null check in stop(): if session creation failed, driver is null, and an unguarded quit() would hide the real error behind a NullPointerException.

Failure evidence

A failing mobile test is hard to debug from a stack trace alone. Add a hook that saves a screenshot and the page source on failure. In pytest this uses a pytest_runtest_makereport hook; the next lesson on reporting shows the pattern. At minimum, give each session a name that appears in the Appium and cloud logs, so you can match a failure to a recording.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why put drv.quit() after yield in a pytest fixture?

    Code after yield is teardown and runs whether the test passed or failed.
  2. 2. What is the main trade-off of a session-scoped driver?

    You skip repeated startup, but one test can leave state behind that breaks the next.
  3. 3. Which tool lets you run only the tests tagged as quick checks?

    Markers (tags) select subsets of tests.
  4. 4. Why guard driver.quit() with a null check in Java teardown?

    A failed setup leaves driver null; a NullPointerException in teardown masks the real cause.

Summary

  • The runner starts and stops sessions; Appium only executes commands.
  • A fixture with yield creates the driver and always quits it.
  • Function scope isolates tests; session scope is faster but shares state, so add a reset step.
  • Markers and parametrization select and multiply tests; a custom option can choose the platform.
  • JUnit and TestNG offer the same lifecycle hooks; guard teardown against a missing driver.

Next: Test Hooks and Backend Mocking for Stable Mobile Tests