Search code examples
rapitravis-citestthat

How to retry R testthat test on API error?


Some tests rely on some external services (e.g. APIs). Sometimes, these external services will go down. This can cause tests to fail and (worse) continuous integration tools to fail.

Is there a way to instruct testthat tests and regular package examples re-run examples/tests more than once, ideally with the second attempt being 5 minutes after the first?


Solution

  • Ideally you would write your tests in a way that they don't call API or database.

    Instead you will mock API end points according to the specification and also write test for cases where API returns unexpected results or errors.

    Here is an example of package that allows you to do so:

    https://github.com/nealrichardson/httptest

    If you are worried that your vendor might change API, talk to them and extract details on their API change management.

    Ask them this:

    What is your change management process? How do you avoid introducing break changes to existing endpoints that people are using?

    (modified from this post)

    If you have to check that API is still the same, draw the line between API validation and testing of your code.

    You will need two separate processes:

    • Unit / Acceptance tests that are executed against the mocks of the API end points. Those run fast and are focused on the logic of your application.

    • pipeline for regular validation of the API. If your code is already live, you are likely to find out of any breaking changes in API anyway. So this is highly redundant. In exceptional cases this can be useful, but only with a very bad vendor.