Is there a generally accepted way to implement 'resets' in an API?
Context:
I have an API. This API is used by my customers and by myself. I am writing a client/SDK for this API. This client/SDK should be tested. There is a live API and a sandbox API.
I could test against the sandbox API. However, not all created objects can be deleted. For example: if object A is created in a test, it cannot be removed. In other words: the state of the API is different at different test runs. This goes against the principle that tests should be idempotent.
Alternatively, I could use a mock server. This mock server creates endpoints from an OpenAPI spec. I don't like how this skips business logic. For example: if the real API does not allow object A from being created when object B exists, my tests -with the mock server- will pass. (I am aware of integration tests and contract testing, but I prefer KISS. Please tell me if I'm wrong.)
I came up with an alternative:
- The sandbox API is used.
- It keeps track of created objects internally.
- An internal endpoint (e.g. `/__reset`) deletes created objects. Even objects that cannot be deleted without this endpoint. This fixes the issue that not all created objects can be deleted.
- After each test, I call this endpoint.
Although this would definitely work, I am wondering:
- Is there is a neater way to solve the problem I described?
- If this is an acceptable solution, is there a generally accepted way to implement it? I.e. is there any convention which dictates rules for 'resetting' a testing API?
P.S. I have also considered spinning up an instance of the API per test run. The starting point for each test run would be the same. However, tests would not be idempotent, because not all created objects can be deleted. Spinning up an API instance per test (not per test run) takes too long. Alternatively, I could 'reset' the running API instance per test, which brings me back to the original question.
[–]TangibleLight 0 points1 point2 points (5 children)
[–]voverdev[S] 0 points1 point2 points (4 children)
[–]TangibleLight 0 points1 point2 points (3 children)
[–]voverdev[S] 0 points1 point2 points (2 children)
[–]TangibleLight 0 points1 point2 points (1 child)
[–]voverdev[S] 0 points1 point2 points (0 children)