Movatterモバイル変換


[0]ホーム

URL:


Running tests in parallel

Setup

To enable parallel testing, you must first be using the 3rd edition1. Then addthe following line to theDESCRIPTION:

Config/testthat/parallel: true

If needed (for example, for debugging) you can temporarily suppressparallel testing withSys.setenv(TESTTHAT_PARALLEL = "false").

By default, testthat will usegetOption("Ncpus", 2)cores. To increase that value for your development machine we recommendsettingTESTTHAT_CPUS in your.Renviron. Theeasiest way to do that is callusethis::edit_r_environ()and then add something like the following:

TESTTHAT_CPUS=4

Tests are run in alphabetical order by default, but you can oftenimprove performance by starting the slowest tests first. Specify thesetests by supplying a comma separated list of glob patterns2 to theConfig/testthat/start-first field in yourDESCRIPTION, e.g.:

Config/testthat/start-first: watcher, parallel*

Basic operation

Each worker begins by loading testthat and the package being tested.It then runs any setup files (so if you have existing setup files you’llneed to make sure they work when executed in parallel).

testthat runs testfiles in parallel. Once the worker poolis initialized, testthat then starts sending test files to workers, bydefault in alphabetical order: as soon as a subprocess has finished, itreceives another file, until all files are done. This means that stateis persisted across test files: options arenot reset, loadedpackages arenot unloaded, the global environment isnot cleared, etc. You are responsible for making sure each fileleaves the world as it finds it.

Common problems

Performance

There is some overhead associated with running tests in parallel:

This overhead generally means that if you have many test files thattake a short amount of time, you’re unlikely to see a huge benefit byusing parallel tests. For example, testthat itself takes about 10s torun tests in serial, and 8s to run the tests in parallel.

Reporters

Default reporters

Seedefault_reporter() for how testthat selects thedefault reporter fordevtools::test() andtestthat::test_local(). In short, testthat selectsProgressReporter for non-parallel andParallelProgressReporter for parallel tests by default.(Other testthat test functions, liketest_check(),test_file() , etc. select different reporters bydefault.)

Parallel support

Most reporters support parallel tests. If a reporter is passed todevtools::test(),testthat::test_dir(), etc.directly, and it does not support parallel tests, then testthat runs thetest files sequentially.

Currently the following reportersdon’t support paralleltests:

The other built-in reporters all support parallel tests, with somesubtle differences:

Writing parallel reporters

To support parallel tests, a reporter must be able to function whenthe test files run in a subprocess. For exampleDebugReporter does not support parallel tests, because itrequires direct interaction with the frames in the subprocess. Whenrunning in parallel, testthat does not provide location information(source references) for test successes.

To support parallel tests, a reporter must setself$capabilities$parallel_support toTRUE initsinitialize() method:

...initialize=function(...) {  super$initialize(...)  self$capabilities$parallel_support<-TRUE  ...}...

When running in parallel, testthat runs the reporter in the mainprocess, and relays information between the reporter and the test codetransparently. (Currently the reporter does not even know that the testsare running in parallel.)

If a reporter does not support parallel updates (see below), thentestthat internally caches all calls to the reporter methods fromsubprocesses, until a test file is complete. This is because thesereporters are not prepared for running multiple test files concurrently.Once a test file is complete, testthat calls the reporter’s$start_file() method, relays all$start_test(),$end_test(),$add_result(), etc. calls inthe order they came in from the subprocess, and calls$end_file() .

Parallel updates

TheParallelProgressReporter supports parallel updates.This means that once a message from a subprocess comes in, the reporteris updated immediately. For this to work, a reporter must be able tohandle multiple test files concurrently.

A reporter declares parallel update support by settingself$capabilities$parallel_updates toTRUE:

...initialize=function(...) {  super$initialize(...)  self$capabilities$parallel_support<-TRUE  self$capabilities$parallel_updates<-TRUE  ...}...

For these reporters, testthat does not cache the messages from thesubprocesses. Instead, when a message comes in:

testthat also calls the new$update() method of thereporter regularly, even if it does not receive any messages from thesubprocess. (Currently aims to do this every 100ms, but there are noguarantees.) The$update() method may implement a spinnerto let the user know that the tests are running.


  1. Seevignette("third-edition") fordetails.↩︎

  2. See?utils::glob2rx for details↩︎


[8]ページ先頭

©2009-2025 Movatter.jp