Dynamic variable in testthat setup

Dear RStudio Community,

In our CI pipeline we currently test that an API (built with plumber) can be succesfully run in a sub process and run the tests defined by the developer. We follow the same framework as in plumbpkg: plumbpkg/tests/testthat/test-plumber.R at main · sol-eng/plumbpkg · GitHub

My question is how can we set the port dynamically? One needs first to make sure that the port on the server running the CI tests is available and then use the same value in the tests. How can this be achieved?

Pseudo-code here:

while port_number is not available
port_number <- port_number+1, continue

thestthat::setup
run api in a background process on port <- port_number

thestthat runs tests pointing at localhost:port_number/endpoints

testthat::teardown
kill background process

Alternatively, can the port be found automatically by plumber and then passed onto a global object for the tests to use? Running plumber::plumb$run on RStudio Server Pro uses a random available port.

Best regards,
Alessandro Pirrotta

For anyone interested I have found helpful answers on github/testthat: https://github.com/r-lib/testthat/issues/1197

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.