Chapter 12
Testing a REST Web Service

Sometimes the user of the system we need to test isn’t a human being but another computer program. For systems like these, the user interface is often a REST[56] web service. To automate tests against it, we need to make our Cucumber step definitions talk to the web service as though they were a regular client application.

There are two main approaches for testing a web application with Cucumber—in-process and out-of-process. Figure 12, Two main approaches for testing REST APIs with Cucumber gives a high-level overview of these two approaches.

images/rest-stack.png

Figure 12. Two main approaches for testing REST APIs with Cucumber

The preferred approach is usually in-process, which means that Cucumber is running in the same Ruby process as your own application. There is no running web server or HTTP traffic, and this is both fast to run and easy to set up. Being able to easily reset state (usually in a database) between scenarios is another strength of the in-process approach, as we explained in Chapter 10, Databases.

Your application doesn’t have to be written in Ruby in order to be testable with Cucumber. Applications written in Java, .NET, PHP, or any other programming language can be tested using the out-of-process approach, which means starting your application before Cucumber runs and then having Cucumber talk to it using an HTTP client library.

This is obviously a very technical chapter. We’ll use the Ruby web framework Sinatra to create a very simple web service from scratch. We’ve chosen Sinatra because it allows us to build the web service in just a few lines of code, meaning we can focus our attention on tests rather than implementation.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.210