2

Okay, so I'm writing some BDD tests for a REST API using pytest-bdd.

Let's suppose I have endpoints A, B, C and D. Normally, when someone uses this API, a request is first done to endpoint A, then if A is successful a request to B can be done, and if A and B were successful then C can be called, and so on.

So okay, I started by testing A:

Given the API receives an A request with x argument
Then a 200 status code is returned
And entity a is returned

And then B:

Given entity a created [...]
When the API receives a B request with y argument
Then a 200 status code is returned
And entity B is returned
[...]

When I implemented the Then step for the first scenario (testing A), I checked if a 200 status code was returned.

When implementing the Given step for the second scenario, which expects endpoint A to have been called, should I:

  1. make sure that the endpoint A was both called and completed in a successful way (by checking the status code);
  2. OR should I only implement the request to A, without checking whether it was successful - and expect that, if endpoint A has an issue, then the respectful (first) scenario should be responsible for detecting it?

1 Answer 1

2

When it comes to BDD tests, the "Given" step should be focused on setting up the initial state of the system under test, rather than verifying any particular behavior. Therefore, option 2 is the preferred approach - you should only implement the request to A, without checking whether it was successful.

The idea is that the responsibility of detecting any issues with endpoint A lies with the scenario that specifically tests endpoint A. If endpoint A fails, the test for endpoint A should fail and you should be able to diagnose the issue from there.

In fact, it's generally not recommended to include verifications in the "Given" step at all. Instead, save any verifications for the "Then" step, where you can specifically check that the system is behaving as expected after the initial state has been set up.

So, for your scenario testing endpoint B, the "Given" step should only set up the state needed to test endpoint B, without any verification of endpoint A. The "Then" step can then include any verifications necessary to ensure that the system is behaving as expected after the B endpoint has been called.

For more information on BDD and best practices for writing BDD tests, I recommend reading the Cucumber documentation: https://cucumber.io/docs/bdd/

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.