Plumber API
When to use this pattern?
Use this pattern to test a Plumber API you own.
How this pattern works?
- Launch API in a background process with
mirai
. - Perform test requests with
httr2
. - Use a
test-app-<endpoint>.R
file pattern. It will allow you to test one endpoint at a time to get the fastest feedback during development. - Test API responses and business logic separately.
- Wrap business logic in functions/objects that can be tested independently of the API. It will allow you to get feedback on the business logic without needing to start the API server.
- Test shapes of API endpoints, not their content. Focus on what defines a contract of the API, don’t test the actual data returned.
Remember
- Use Arrange, Act, Assert (AAA) pattern to structure your tests.
- Arrange: Startup the API, prepare requests.
- Act: Perform the request.
- Assert: Verify the response.
- Wrap requests code in functions to avoid code duplication and keep the tests readable.
- Don’t duplicate tests of edge cases of business logic in the API tests, test them in the business logic tests only. Focus on happy path, look for things that can go wrong in the API itself, like serialization and deserialization of data.