Creating a test class

in Japanese(日本語)

A test scenario is a Swift file that describes a @TestClass with one or more @Test methods. This page covers where to put the file and the minimal shape it needs.

Where the file goes

Place .swift files under TestProjects/<project>/scenarios/ (any subfolder is fine, e.g. scenarios/Login/). Running swift build (or any fleetest run) auto-discovers new files — there is no registration step.

Minimal example

import FTDSL

@TestClass                                      // target app is resolved from the run profile
class LoginTest {

    @Test("Can log in with a valid email and password")
    func S0010() {
        scenario {
            scene(1, "Log in") {
                condition {
                    launchApp()
                }.action {
                    type("#email", "test@example.com")
                    type("#password", account("[user1].password"))
                    tap("#login_btn||Log In")
                }.expectation {
                    exist("#welcome_text||Welcome")
                }
            }
        }
    }
}

Account values live in dataset/accounts.json — see Test data and accounts.

beforeEach / afterEach

@TestClass
class LoginTest {
    func beforeEach() {
        irregularHandler("#promo_modal", dismiss: "#btn_promo_close")
    }
    func afterEach() {
        // cleanup that must always run
    }

    @Test("...")
    func S0010() { scenario { /* ... */ } }
}

beforeEach() and afterEach() run automatically around every @Test in the class. afterEach() still runs even after a failure — it is the place to put cleanup that must not be skipped when a scenario is aborted mid-way.

func beforeAll() (optional) runs once per run, per class (in a run that spans machines, once per machine), before any scenario of that class on any device — for per-class preparation (for example creating the test data of that class; values it writes with writeRunMemo are shared, see memo). In a run that spans machines, another machine's beforeAll() can overwrite a value this one wrote, so pass a value made by the app or by one machine from beforeAll() only in a class with @RunOnSingleMachine. Preparation that must happen once for the whole run goes in beforeRun(). Other devices that pick up a scenario of that class wait until it finishes. If it fails, the remaining scenarios of that class in the run are recorded as failed without running.

func afterAll() (optional) runs once per run, per class (in a run that spans machines, once per machine), after every device has finished the run, in a process of its own — for run-wide cleanup such as deleting the test data beforeAll() created. It runs whether or not beforeAll() passed and can read the run memo. It does not run in an interrupted run.

func setUpDevice() (optional) runs at most once per run, per device, per class, before beforeEach() of the first scenario of that class that lands on the device — for preparation that should not repeat for every test but is needed on each device (for example switching the app's language). Each scenario is its own process, so instance properties set there are visible only to that first scenario; setUpDevice() cannot write the run memo (it can only read it); pass values by writing them with writeRunMemo in beforeAll() of a class with @RunOnSingleMachine, or in beforeRun() for a run-wide value (see memo).

func tearDownDevice() (optional) runs once, after the device has finished all of its scenarios in the run, if at least one scenario of that class ran on the device — for cleaning up what setUpDevice() prepared (it runs even if setUpDevice() failed). It does not run on a device that dropped out mid-run or when the run is interrupted. Its result is written to the execution log and a report, but it does not count toward the test results.

Run-wide preparation and cleanup (@RunHooks / beforeRun / afterRun)

beforeAll() runs once per class (once per machine in a run that spans machines). When you need something done once for the whole run, before any scenario of any machine and after all of them (for example resetting a server that every machine shares, or writing a value that every test reads), put it in a class marked @RunHooks.

import FTDSL   // required: without it, a class in a separate file fails with `unknown attribute 'RunHooks'`

@RunHooks
class RunWide {
    func beforeRun() {
        let response = httpRequest("https://test-server.example.com/login")
        response.status.thisIs(200)
        writeRunMemo("token", response.text)
    }

    func afterRun() {
        // reads the final run memo; it cannot write it
        let response = httpRequest("https://test-server.example.com/cleanup?token=\(readRunMemo("token"))")
        response.status.thisIs(200)
    }
}

Which one to use:

beforeRun() / afterRun() beforeAll() / afterAll() setUpDevice() / tearDownDevice() run-setup.sh / run-teardown.sh
How often once per run per project once per run per class (per machine in a multi-machine run) once per run per device per class once per machine
Runs on the machine where you started fleetest (the runner machine for run --runner) each machine that runs a scenario of the class the device each machine
Device commands no yes yes no (a script)
compose.yaml servers no yes yes before / after they start (see services)
Use it for a server shared by every machine; values for the run memo per-class preparation that needs the app or this machine's servers per-device preparation (language, permissions) per-machine preparation

Running a class in one place or in order (@RunOnSingleMachine / @RunOnSingleDevice / @RunInOrder)

By default the tests of a class are spread over the devices of the run (which may span several machines), run in parallel, in no particular order. If the tests are independent, that is the fastest and you add nothing. Only when you really need it, put exactly one of the following on the class (on a line of its own, apart from @TestClass). The reason string (for example @RunInOrder("the order flow")) can be left out in all three.

@RunInOrder("the order flow")
@TestClass
class OrderFlow {
    @Test("add to the cart")
    @Order(10)
    func S0010() { scenario { /* ... */ } }

    @Test("place the order")
    @Order(20)
    func S0020() { scenario { /* ... */ } }

    @Test("it shows in the order history")
    @Order(30)
    func S0030() { scenario { /* ... */ } }
}
@RunOnSingleMachine @RunOnSingleDevice @RunInOrder
Runs on one machine one device one device
Inside the class parallel (over that machine's devices) one at a time, no other class in between one at a time, no other class in between
Order not defined not defined (tests cannot rely on each other) the @Order order (guaranteed)
After a failure continues continues the rest is not run and is recorded as failed
--failed only what failed only what failed from the first test of the class

The flow in this example is normally folded into one @Test. Split it only when it is too long and you want to see a failure in the middle separately.

@Order(n) goes on a @Test method. In a @RunInOrder class it is required on every @Test, with an integer written directly (gaps such as 10, 20, 30 are fine; constant names and expressions are not accepted); a value cannot repeat within a class. Using it in a class without @RunInOrder is a compile error.

You cannot write which machine or device the class is kept on: it is kept where the first test of the class was handed out. In a run that is divided over several machines, one line in the execution log says where. If the device of a device-bound class (@RunOnSingleDevice / @RunInOrder) drops out, fleetest waits for it to come back and reruns the interrupted test; if it cannot come back, the remaining tests are recorded as failed instead of moving to another device.

What is an error

With --broadcast, and in a run that runs the same thing on several machines (--fleet without --split; see Remote runs), the attribute has no effect (the class runs on several devices or machines). --broadcast prints one line saying so; --fleet does not. See Where to put test data and Memo (the run memo is shared across machines, except in --fleet without --split).

Marking work in progress or retired tests

Both can be attached to the class or to an individual @Test method.

Notes