Wednesday January 16, 2019
In this Revolutionary Era of Technology, applications depend highly on well tested, well designed, developed, and well-documented API.
However, the number of such API’s is still increasing. Writing test scripts for REST endpoints is no more enough. APIs nowadays, demand living documentation with good tests.
This tutorial will give you an introduction to Cucumber. Cucumber is the most popular tool for testing REST APIs and user acceptance testing.
Cucumber – A Testing Framework
Cucumber is a framework highly used for testing REST APIs, compatible with BDD (Behaviour Driven Development). BDD allows the developers and users to define the operations of an application in plain text. It works on the DSL (Gherkin Domain Specific Language).
Gherkin is the language of cucumber used for writing tests. It is a simple but powerful syntax that allows the testers and developers to write complex tests and keeping it simple and easy-to-write for non-technical users.
One of Cucumber’s most convincing highlights is that it gives the capacity to compose these portrayals utilizing plain and easy content in your local language.
Cucumber’s dialect, Gherkin, is usable in a developing assortment of human dialects, which also includes LOLZ. The benefit of this is that these element portrayals can be composed or potentially comprehended by non-specialized individuals engaged with the undertaking.
Very importantly, Cucumber isn’t a substitution for RSpec, test/unit, and so on. It’s anything but a low dimension testing/determination system.
Cucumber is crucial for an advancement approach called Behavior Driven Development (BDD).
BDD with Cucumber– Introduction
Behaviour Driven Development (BDD) is a communitarian way to deal with programming advancement that connects the correspondence hole among business and IT.
Also Read: API Security Testing: Rules And Checklist
It enables groups to discuss prerequisites with more accuracy, find absconds early and deliver programming that remains part viable after some time.
This is what Cucumber Framework does. Cucumber BDD enables groups to make business necessities that can be comprehended by the entire group. Determining precedents reveals misconception that individuals probably won’t know about.
Groups training CucumberBDD center on forestalling abandon instead of discovering them. This prompts less improve and faster time to showcase.
The two fundamental practices in the Cucumber BDD approach are disclosure workshops, which connect the correspondence hole among business, IT, and executable features.
Cucumber BDD – Features
A Feature is what your product does (or ought to do) and for the most part, relates to a client story or an approach to tell when it’s done and that it works. The most common organization of a feature is:
Highlight: <short description>
Inside the meaning of a feature, you can give a plain content depiction of the story. You can utilize any arrangement for this; however, having a type of layout makes it simpler to see the critical bits of data with a speedy look. One of the typical layouts is talked by Mike Cohn in a User Stor Applied:
As a <role>
I need <feature>
so that <business value>
This arrangement centers use around three essential inquiries:
Who’s utilizing the framework?
What’s happening to them?
For what reason do they give it a second thought?
Cucumber – Situations
A component is characterized by at least one situation. A situation is an arrangement of ventures through the component that practices one way. Cucumber utilizes the BDD style that Dan North advanced with his jBehave venture: given-_when_-_then_.
A situation is comprised of 3 segments identified with the 3 sorts of steps:
Given: This sets up preconditions, or setting, for the situation. It works much like the setup in xUnit and before squares in RSpec.
At the point when: This is the thing that the component is discussing, the activity, the conduct that was centered around.
At that point: This checks postconditions… it confirms that the correct thing occurs in the When arrange.
The general type of a situation is:
Opting for Cucumber – Why you should choose?
We should expect there is a necessity from a customer for an E-Commerce site to build the offers of the item with actualizing some new highlights on the site. The main test of the improvement group is to change over the customer thought into something that really conveys the advantages to the customer.
The first thought is magnificent. However, the main test here is that the individual who is building up the thought isn’t a similar individual who has this thought.
On the off chance that the individual who has the thought, happens to be a skilled programming designer, at that point we may be in luckiness: the thought could be transformed into working programming while never waiting to be disclosed to any other individual.
Presently the thought should be imparted and needs to go from Business Owners (Client) to the advancement groups or numerous other individuals.
Most programming tasks include groups of few people working cooperatively together, so amazing correspondence is basic to their prosperity.
Also Read: Selenium Automation Testing With Cucumber Integration
As you most likely know, great correspondence isn’t just about articulately depicting your plans to other people; you additionally need to request criticism to guarantee you’ve been seen accurately.
This is the reason the programming groups have figured out how to function in little augmentations, utilizing the product that is constructed steadily as the criticism that says to the partners that are this is what he meant? With the assistance of Gherkin dialect, cucumber encourages the revelation and utilization of a pervasive dialect inside the group.
Tests written in cucumber straightforwardly collaborate with the advancement code, yet the tests are written in a dialect that is very straightforward by the business partners. Cucumber test expels numerous mistaken assumptions sometime before they make any ambiguities into the code.
Configuring JUnit with Cucumber
To make the JUnit aware of Cucumber and read feature files when operating, the ‘Cucumber’ must be declared as the ‘Runner’.
In the above example, it can be observed that the Feature element of CucumberOption locates the feature file that was created before. Glue. gives paths to definitions. But, if the step definition and test case are in the same package as in the above tutorial, then the element may be dropped.
Running Tests for REST APIs
First and foremost, we will get started with JSON structure to demonstrate the data uploaded to the server by a POST application. We will then download this application to the client using a GET. This structure will be saved in the jsonString field.
A WireMock server plays its role to demonstrate a REST API. i.e.
WireMockServer wireMockServer = new WireMockServer(options().dynamicPort());
This tutorial will also be using Apache HttpClient API to illustrate the client used to connect to the server which is –
CloseableHttpClient httpClient = HttpClients.createDefault();
Lets, begin with writing testing code within step definitions.
Note: The server ‘ wireMockServer.start();’ should be running already before the client connects to it.
WireMock API will be used now to stub the REST service:
Now, a POST application needs to be sent to the content received from the jsonString field decalared already above to the server:
HttpPost request = new HttpPost(“http://localhost:” + wireMockServer.port() + “/create”);
StringEntity entity = new StringEntity(jsonString);
HttpResponse response = httpClient.execute(request);
If the POST application has been successfully handelled, the following code will assert:
Note: The server must stop after use: wireMockServer.stop();
In this article, we covered some fundamental use of the Cucumber framework that uses the Gherkin domain-specific language to test the REST APIs.
With the assistance of Gherkin dialect cucumber encourages the disclosure and utilization of an omnipresent dialect inside the group. Tests written in cucumber straightforwardly associate with the advancement code, yet the tests are written in a dialect that is very straightforward by the business partners. Cucumber test evacuates numerous errors well before they make any ambiguities into the code.
Also Read: Top 10 Automation Testing Tools 2019