Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Testing with pep-test

pep-test builds the [Test] functions of Pepper files and runs them, each in a child process of its own, reporting which pass. Writing tests is covered in Unit tests.

pepper test                          # every test under the current folder
pepper test src/words_test.pep       # the tests of one file
pepper test -f words src             # only tests whose name contains "words"
pepper test --list src               # list them without running them
pepper test --shard 2/4 src          # the second of four parts of them
pepper test -I . geometry/tests      # tests that import the module geometry
running 3 tests
test counts_the_words ... ok (4 ms)
test an_empty_string_has_no_words ... ok (3 ms)
test reading_past_the_last_word_panics ... ok (5 ms)
3 passed, 0 failed

pep-test is written in Pepper (tools/pep-test). Build it with the compiler and put it next to pepper and pep-build, so that pepper test finds it and it finds the compiler:

target/bin/pep-build tools/pep-test/main.pep -o target/bin/pep-test

Usage and options

pep-test [options] [path]...
OptionWhat it doesDefault
<path>A .pep file to test, or a folder to search for files holding [Test] functions; repeatablethe current folder
-f <text>, --filter <text>Run only the tests whose name contains the text; repeated, any of themevery test
-l, --listList the tests instead of running them
-j <count>, --jobs <count>How many tests run at onceone per logical CPU
--target <triple>Builds the test programs for this target and runs the tests there: in the iOS simulator for an iOS simulator triple, on an Android emulator or device for an Android one (see Testing on iOS and Android)this machine
--shard <i>/<n>Run only the ith of n parts of the tests (1 to n), so the n runs 1/n to n/n run each test onceevery test
-t <seconds>, --timeout <seconds>Fail a test still running after this long; 0 for no limit60
-I <dir>, --import-path <dir>A folder pep-build searches for imports; repeatable
-L <dir>, --library-path <dir>A folder the linker searches for libraries, for pep-build; repeatable
-D <option>, --define <option>A build option for pep-build, NAME or NAME=VALUE; repeatable
--build-environment <name>=<value>Sets an environment variable for each pep-build that builds a test program, and not for the tests, which run with pep-test's environment; repeatable. For example, --build-environment PEPC_NO_COMPILATION_UNITS=1 builds each test program as one, while tests that build programs of their own still use compilation units
--lockedPassed to pep-build: fail, rather than fetch, when the project's pep.lock is out of date (also PEPPER_LOCKED=1)
--offlinePassed to pep-build: fetch nothing over the network, building with what .pep/deps and the download cache hold (also PEPPER_OFFLINE=1)
--frozenBoth --locked and --offline
-F <names>, --features <names>Passed to pep-build: features of the project's package to turn on, comma-separated; repeatable (see Features)the default feature
--no-default-featuresPassed to pep-build: leave the project's package's default feature off
--allTest every member of the project's workspace, with their features unified; no <path> with it
-p <name>, --package <name>Test this workspace member, by package name; repeatable, the members' features then unified
-h, --helpPrints the usage and these options
-V, --versionPrints pep-test and its version

--locked, --offline and --frozen govern the fetch each pep-build does for a project (see pep-build); pep-build reads PEPPER_LOCKED and PEPPER_OFFLINE itself.

The exit status is 0 when every test program builds and every test passes, 1 when a test fails or a program does not build, and 2 for a usage error. A run in which no test runs, because --filter matches none or the --shard gets none, passes and exits with 0 (running 0 tests, then 0 passed, 0 failed, 5 filtered out), as cargo test does, so one filter can go to runs over several folders and those that hold none of its tests still pass. A misspelt filter therefore passes too; the summary shows it.

What it does

  1. Finds the files. A file named on the command line is tested as it is. In a folder, and the folders below it (except those whose names start with .), it takes each .pep file with a line that starts with [Test].
  2. Builds one test program per folder, of that folder's test files, with pep-build --test (found next to pep-test, else on the PATH), in parallel. Each file keeps the scope it has in any program, so test files in a module's folder see each other's names, and a helper two of them declare must be private (see Unit tests). A test build sees the project's [dev-dependencies] (see Projects: pep.toml). A program that does not build is reported with the compiler's errors, and the other folders' tests still run.
  3. Lists each program's tests with --list and keeps those that match --filter. With --shard I/N it keeps every Nth of those, starting with the Ith: tests next to each other in the list, such as one file's slow ones, land in different shards, and every test is in exactly one, as long as the N runs see the same files. With --list it prints them and stops: the test's name, or file::name when the tests come from more than one file.
  4. Runs each test as program --run FILE::NAME --timeout SECONDS, each in its own process, up to --jobs at once, and prints test NAME ... ok (TIME) or ... FAILED (TIME) as each ends (in order with --jobs 1). TIME is how long the test's process ran: whole milliseconds (12 ms), or seconds with two decimals from one second up (1.25 s). A test that passes after more than 60% of its timeout is followed by warning: test NAME took TIME, over 60% of its N s timeout: it will time out once its machine is busier. A test passes when its process exits with 0; a [ShouldPanic] test passes when its process panics with a message that contains the attribute's text.
  5. Reports the failures, each with what the test printed (the panic's message and location) and why it failed, then a summary: 2 passed, 1 failed, with the number of tests --filter left out, of tests left to other shards, and of test programs that did not build when there are any. Each shard builds every test program: only the tests' runs are split.

The test programs are built in a temporary folder that is removed at the end. A test runs in the current folder, with PEPPER_BACKTRACE=0 unless PEPPER_BACKTRACE is set, so a panic prints its message without a backtrace. Each test gets a temporary folder of its own (TMPDIR, and TMP and TEMP for Windows, name it), removed when the test ends, so what a test that panics or times out leaves there, such as a TempFolder whose destructor never ran, is removed too.

Testing on iOS and Android

With --target, each test program builds for that target (pep-build --target, which needs the platform's SDK; see iOS and Android), and every request to it, --list and each --run, goes to where that target runs. The report is the same as on this machine.

pepper test --target aarch64-apple-ios-simulator src   # on a Mac, in the booted simulator
pepper test --target x86_64-linux-android src          # on the running emulator
  • iOS simulator (aarch64-apple-ios-simulator, or x86_64-apple-ios-simulator on an Intel Mac): each program runs in a booted simulator with xcrun simctl spawn, from this Mac's file system, so its temporary folder is the one pep-test makes here. simctl starts it in another folder, so pep-test names the current one in PEPPER_TEST_DIRECTORY and the program goes back there first: data files beside a test are found as on the Mac. simctl passes no USER, and the simulator's user database lacks the Mac's user, so os.user_name() finds none. The simulator is the one whose UDID PEPPER_IOS_SIMULATOR holds, else the booted one (booted). Boot it first (xcrun simctl boot "iPhone 16"). A device triple (aarch64-apple-ios) is a usage error: nothing here can start a program on an iPhone.
  • Android (aarch64-linux-android, x86_64-linux-android): adb, from the PATH or $ANDROID_HOME/platform-tools, pushes each program to a folder of its own under /data/local/tmp on the device that ANDROID_SERIAL names, or the only one connected, and runs it there with adb shell, from that folder. The folder of each program's test files is pushed too, to the same path below it, so a test that reads a data file beside it by a path from the current folder (such as tls/test/key.pem, from runtime/) finds it. Each test gets a temporary folder there, removed when it ends, and the whole folder is removed at the end. Start the emulator first, and build for its ABI (x86_64 on an x64 host, aarch64 on Apple silicon).

A target that is neither runs its programs here, as any program (such as x86_64-apple-darwin on an Apple silicon Mac).

The test program

pep-build --test builds a folder's test program (see pep-build), which pep-test runs with one of two requests:

  • --list prints a line per test, its fields separated by tabs: the name, file:line, returns or panics (a [ShouldPanic] test), and the text the panic must contain.
  • --run NAME [--timeout SECONDS] runs one test (NAME, or FILE::NAME with the file as --list gives it) and exits with 0 when it returns having released everything it allocated; a test that leaves an allocation behind (or releases one too many) exits with 1 and says so on standard error. A panic aborts the program, and so does a stack overflow, after panic: stack overflow; a test that runs past the timeout ends it with exit status 124. However the program ends (the test returns, panics, crashes or times out), every process the test started ends with it (a job object on Windows, a process group elsewhere): one left running would hold the pipes to its output open, and pep-test would wait on it. With PEPPER_TEST_DIRECTORY set and not empty, the program first makes that folder its current one (and exits with 2 when it cannot enter it).

On Windows, a test program turns off Windows Error Reporting for its crashes and abort(), so a test that panics or crashes ends at once, without a dialog.