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]...
| Option | What it does | Default |
|---|---|---|
<path> | A .pep file to test, or a folder to search for files holding [Test] functions; repeatable | the current folder |
-f <text>, --filter <text> | Run only the tests whose name contains the text; repeated, any of them | every test |
-l, --list | List the tests instead of running them | |
-j <count>, --jobs <count> | How many tests run at once | one 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 once | every test |
-t <seconds>, --timeout <seconds> | Fail a test still running after this long; 0 for no limit | 60 |
-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 | |
--locked | Passed to pep-build: fail, rather than fetch, when the project's pep.lock is out of date (also PEPPER_LOCKED=1) | |
--offline | Passed to pep-build: fetch nothing over the network, building with what .pep/deps and the download cache hold (also PEPPER_OFFLINE=1) | |
--frozen | Both --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-features | Passed to pep-build: leave the project's package's default feature off | |
--all | Test 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, --help | Prints the usage and these options | |
-V, --version | Prints 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
- 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.pepfile with a line that starts with[Test]. - Builds one test program per folder, of that folder's test files,
with
pep-build --test(found next topep-test, else on thePATH), 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 beprivate(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. - Lists each program's tests with
--listand keeps those that match--filter. With--shard I/Nit keeps everyNth of those, starting with theIth: 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 theNruns see the same files. With--listit prints them and stops: the test's name, orfile::namewhen the tests come from more than one file. - Runs each test as
program --run FILE::NAME --timeout SECONDS, each in its own process, up to--jobsat once, and printstest 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 bywarning: 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. - 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--filterleft 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, orx86_64-apple-ios-simulatoron an Intel Mac): each program runs in a booted simulator withxcrun simctl spawn, from this Mac's file system, so its temporary folder is the onepep-testmakes here.simctlstarts it in another folder, sopep-testnames the current one inPEPPER_TEST_DIRECTORYand the program goes back there first: data files beside a test are found as on the Mac.simctlpasses noUSER, and the simulator's user database lacks the Mac's user, soos.user_name()finds none. The simulator is the one whose UDIDPEPPER_IOS_SIMULATORholds, 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 thePATHor$ANDROID_HOME/platform-tools, pushes each program to a folder of its own under/data/local/tmpon the device thatANDROID_SERIALnames, or the only one connected, and runs it there withadb 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 astls/test/key.pem, fromruntime/) 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_64on an x64 host,aarch64on 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:
--listprints a line per test, its fields separated by tabs: the name,file:line,returnsorpanics(a[ShouldPanic]test), and the text the panic must contain.--run NAME [--timeout SECONDS]runs one test (NAME, orFILE::NAMEwith the file as--listgives 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, afterpanic: 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, andpep-testwould wait on it. WithPEPPER_TEST_DIRECTORYset 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.