guide to setting up API tests of plumber?

Hi all,

Is there any guide or example package demonstrating how to run tests of APIs within packages built using Plumber? I tried following the example package here: GitHub - sol-eng/plumbpkg: Example R package that includes Plumber APIs but I am having issues which I do not understand how to debug, there is not much information on testing on the official docs. If anyone has any tips, I'd appreciate it, thanks.

If anyone is curious why I am posting this, my CI tests sometimes fail to connect to the port. See here: Dev · dd-harp/MicroMoB@c53c366 · GitHub and the test from which they originate MicroMoB/test-api_mosquito.R at dev · dd-harp/MicroMoB · GitHub

The port might already be in use on the machine. Maybe define port using port <- plumber:::findPort() which will return an available port. Build your test around that.

Thanks @meztez, I imported findPort into the package but I still get connection refused errors, as:

Error in `curl::curl_fetch_memory(url, handle = handle)`: Failed to connect to localhost port 6177: Connection refused

where 6177 is a port returned by findPort(). Are there any other suggestions you have?

Did you start the plumber API you are trying to reach using this port? I do not see any change in the repo.

Also try setting your host in pr$run. Connection refused is different from cannot resolve host name. It makes me wonder...

pr$run(host = "", port = port)

Other things I'm thinking. Maybe the CI tool (github actions) is not letting you open port on the machine to avoid exploits?

Do the test work locally?

Hi @meztez, I figured out the issue and pulled it into the repo. Thanks for taking a look at this.

The issue was that the port that was being set was not actually being passed to the API getting set up inside of the callr process. So the problem has nothing to do with plumber, just the way the scoping in callr works, I guess it didn't find the variable. The port variable is now passed as an argument to the function evaluated in the callr function, and all tests work as expected now, locally and on GitHub Actions.

That being said, is there any more updated guide to testing than the test package here? (GitHub - sol-eng/plumbpkg: Example R package that includes Plumber APIs) Just curious as it hasn't been updated in a few years, not sure if best practices have changed since.

This topic was automatically closed after 45 days. 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.