Skip to main content

Fast and Reliable Test Setup with REST APIs


This is a guest post from Robert Schultheis, a Test Engineer at Knewton who is doing great things with REST APIs in his testing practice

When we list out the properties that make a test a "good test", some things that often come up are:

  • Precise. The test should test one thing, and one thing only. A bug in some part of the application that is unrelated to the test should not cause the test to fail.
  • Independent. A test should not have it's outcome affected by any other test in the test suite.
  • Fast. This doesn't need much explanation. The quicker a test suite runs, the more useful it is.

Of course there are many other properties that might be included in such a list, but I mention these specifically because they can be particularly challenging to achieve in a Selenium test suite. The reason for this is that Selenium tests often involve setups that may include a sign-up, a login, some navigation, form submission, or other interactions with a website. Only after doing those things are you ready to assert on some aspect of the website. If you confine your tests to only being allowed to interact with the website, there is little that can be done to alleviate this issue.

A Solution

REST APIs are increasingly common in the backends of newer websites. If you are fortunate enough to have such an architecture in the site you are testing it may make sense to make use of those REST APIs in your Selenium test suite. By using calls to the REST APIs in the backend to perform parts of a test setup, and using Selenium to perform the assertion within the website normally, we can end up with tests that are faster, more focused, and more reliable.

REST APIs are based on the HTTP protocol, which is the same protocol that powers essentially the entire internet. Nearly every modern programming language has libraries that can be used to make HTTP requests, thus we can add support for calling into REST APIs to virtually any test suite.