Search code examples
apitestingcucumberautomated-testsweb-api-testing

How do I run cucumber tests when testing an rest or graphql API


This is my first time playing with cucumber and also creating a suite which tests and API. My questions is when testing the API does it need to be running?

For example I've got this in my head,

  • Start express server as background task

  • Then when that has booted up (How would I know if that happened?) then run the cucumber tests?

I don't really know the best practises for this. Which I think is the main problem here sorry.

It would be helpful to see a .travis.yml file or a bash script.


Solution

  • I can't offer you a working example. But I can outline how I would approach the problem.

    Your goal is to automate the verification of a rest api or similar. That is, making sure that a web application responds in the expected way given a specific question.

    For some reason you want to use Cucumber.

    The first thing I would like to mention is that Behaviour-Driven Development, BDD, and Cucumber are not testing tools. The purpose with BDD and Cucumber is to act as a communication tool between those who know what the system should do, those who write code to make it happen, and those who verify the behaviour. That’s why the examples are written in, almost, a natural language.

    How would I approach the problem then?

    • I would verify the vast majority of the behaviour by calling the methods that make up the API from a unit test or a Cucumber scenario. That is, verify that they work properly without a running server. And without a database. This is fast and speed is important. I would probably verify more than 90% of the logic this way.

    • I would verify the wiring by firing up a server and verify that it is possible to reach the methods verified in the previous step. This is slow so I would do as little as possible here. I would, if possible, fire up the server from the code used to implement the verification. I would start the server as a part of the test setup.

    This didn’t involve any external tools. It only involved your programming language and some libraries. The reason for doing it this way is that I want to to be as portable as possible. The fewer tools you use, the easier it gets to work with something.

    It has happened that I have done some of the setup in my build tool and had it start a server before running the integration tests. This is usually more heavy weight and something I avoid if possible.

    So, verify the behaviour without a server. Verify the wiring with a server. It is important to only verify the wiring in this step. The logic has been verified earlier, there is no need to repeat it.

    Speed, as in a fast feedback loop, is very important. Building and testing the entire system should, in a good world, take seconds rather than minutes.