API Automation Testing Using BDD Approach:- In this blog we are going to talk about API Automation testing testingusing BDD approach. Currently, there are a lot of tools available for achieving BDD. Choosing one of them is always a tedious task. If there is any confusion regarding what is to be build, or we are having communication problem within the team, then one should start thinking about the BDD approach as this approach help getting all the stakeholders in sync. An early step in BDD is the definition of a shared vocabulary between stakeholders, domain experts, and engineers.
Most importantly, the business should be able to understand these tests, so you can reduce uncertainty and build confidence in what you are building. Let us see the Problem statement below and also work on the solution.
- Problem Statement: API automation test script creation, improve test scenario readability and test execution result reporting.
- Solution: Test cases are very helpful in understanding the system and software functionality but when it comes to business stakeholders and top management who do not want to get into all the details separately, BDD provides a way in which all stakeholders can work on one common page. BDD provides common English language for business requirements and on the basis of that QA and developers can perform their respective tasks. As all the details and requirements are on one common page, it helps in smooth coordination and exact task progress and validation can be done.
Introduction to BDD:-
BDD (Behaviour-Driven Development) is used to minimise the communication gaps between various team members. BDD enables the team to understand customer requirements and uses real-world examples. BDD focuses on business rules and behaviour.
Examples, requirements and automated tests become living documentation of how the system behaves. Test-Driven Development (TDD) is a software development technique where automated tests are written before the business code. Developers use those tests to drive development.
The BDD flavor of TDD uses natural language to describe tests. These tests can be understood by non-programmers and are often based on examples created collaboratively using Example Mapping. To achieve this, It uses ‘Gherkin’ as the language. Thus, gherkin enables us to write natural language tests which is one of the most used tools.
In this blog, we are going to use ‘cucumber’ which is used to execute all those tests which are written in ‘gherkin’. If we talking about the Automated test then it is the by-product of BDD/TDD.
Introduction to Cucumber:-
As we already discussed ‘BDD’ and ‘Gherkin’ and we know that these are the concepts and ways of doing software project development. There are many tools available in the market which support BDD and enables ‘Gherkin’ natural language test to run. Cucumber is one of the most used and famous tools to support BDD. Cucumber reads all the executable tests and steps which were written in plain English language and validate what software is intended to perform. To understand scenarios, it is mandatory to follow the syntax and rules. In short those rules are called ‘Gherkin’.
Test Automation and Execution process:-
Test Automation and execution is one of the core functionality which is necessary to have in any automation framework. These functionalities are as follows:
- Project URL and Environment where any Software project can run.
- Given prerequisites and conditions.
- Actual Test and its expected behaviour.
We can see these are the main pillars for Automation testing and in any Automation technique we need to manage all these things wisely and effectively. Any test need to have Base URL from where testing can be started. After this, we need some scripts and preconditions to load. Now we can execute actual test case. After achieving all the above task, test execution reports will be generated.
An example use case for API automation:-
Let’s take an example for list users API which handles various functionalities related to users and for this it has various routes/endpoints. All the endpoints have different outputs/response to support one common/complete feature. For this, it needs to have read, write, update, delete methods for various endpoints.
- List users endpoint
- Single user endpoint
- Create user endpoint
- Update user endpoint
- Delete user endpoint
- Register user endpoint
We will be using following capabilities to accomplish our Automation example in this blog:
- TestNG – We will using the TestNG library in this scenario as it provides a wide variety of Annotation. Also, it provides great Test Case management-related capabilities and most importantly, it provides a good reporting interface.
- Rest Assured – Rest Assured is one of the most powerful java libraries for Rest API Automation Testing.
- Java – Java is one of the most versatile programming languages and supported by a huge community all over the world.
- Cucumber – We will use cucumber as a BDD tool for creating Automation script and Cucumber is one of the first choice of BDD tools all over the world.
- Jenkins – We will use Jenkins as a CI/CD tool for scheduling our test run and auto report generation.
Automated test cases Examples:-
- Go to File >> New Project. Choose maven as a type and leave ‘Create from Archetype’ unchecked and click on next.
- Now give the group id and artefact id and save the project.
- Right-click on the java folder under test, and add a new package. Call it features, Same again, another package called runner, another package called glue code right-click on the test folder and add a new directory, call it resources, Right-click on the new resources folder and choose Mark Directory as->Test Resources Root.
- Now right click on features and create a file named “Test.feature”.
- Now go to pom.xml file and add TestNG, cucumber, RestAssured dependencies.
- Let’s right-click on “runner” and add a java class named TestRunner.java.
- Go to glue code and add a java class named Test_Steps_UsersList.java and BDDStyleMethod.java.
- Now let’s go to runner >> TestRunner class and run(right click/ ctrl+shift+F10 ) the class.
- Now go to runner >> TestRunner class and add “com.cucumber.listener.ExtentCucumberFormatter:target/cucumber-reports/report.html” In “plugin” to generate HTML Extent Report.
- After successful execution go-to target >> cucumber-reports and open the report.html file in any browser.
- Data-driven testing:- In Cucumber we use Scenario Outline for data-driven testing. First of all, we need to add “Scenario Outline” as a key. After that, we need to add “Examples” keyword. In this blog, example is used to declare a data table. In conclusion, these examples will act as a test data for test execution. After defining in Test.feature file we need to use these in our step definition in < >. And we need to use Regex for using this in “Test_steps”.
we will see indepth working of cucumber and feature file in our next blog. In next blog we will focus more on various cucumber keyword and see how we can manage our Test Automation. Scope of this API Automation Testing blog is restricted to introduction of RestAssured with cucumber only. In this blog we coverd all the basic setup and high level idea of BDD.