Skip to content
VirtualLP Virtual Learning Platform

Behaviour-Driven Development and API Testing

14 min read Topic 9 of 31 2 hands-on labs

Before testing software, agree on what it should do. Behaviour-Driven Development (BDD) uses examples to describe that behaviour.

API (Application Programming Interface) testing checks a service by sending it requests directly. For example, send a request to add a student, then check the saved details. You can do this without opening the website's pages.

Behaviour-Driven Development (BDD)

BDD

Behaviour-Driven Development (BDD) uses examples to agree on what software should do. Users, developers and testers write the examples together. Tools such as Cucumber can connect them to automated tests.

ATM

Given my account has ₹5,000, When I withdraw ₹2,000, Then I get ₹2,000 cash And my balance is ₹3,000. A bank manager can read and agree to this, and a tester can automate it. Both are looking at the same sentence.

flowchart LR
    F["Feature file (Gherkin)<br/>Given / When / Then"] --> C[Cucumber runner]
    C -->|matches each step text| S["Step definitions<br/>@Given @When @Then (Java)"]
    S --> APP[Application<br/>e.g. via Selenium WebDriver]
    C --> R[Report: passed / failed / undefined steps]

A feature file describes the behaviour. Cucumber matches each step to a Java method called a step definition. The method runs the action or check. Cucumber reports which steps passed or failed.

Gherkin keywords

Keyword Purpose
Feature: The capability being described
Background: Steps that run before every scenario
Scenario: One concrete example
Given The starting situation
When The action
Then The expected result
And / But Continue the previous kind of step
Scenario Outline: + Examples: The same scenario run with a table of data
@tag Group scenarios (@smoke, @regression) to run only some of them
Feature: Login
  Scenario Outline: Login attempts
    Given the user is on the login page
    When the user logs in with "<username>" and "<password>"
    Then the message "<message>" is shown

    Examples:
      | username | password | message           |
      | student  | Pass@123 | Welcome, student  |
      | student  | wrong    | Invalid password  |

What it means: one scenario, run twice – once with the right password (expect "Welcome, student") and once with a wrong one (expect "Invalid password"). <username> is filled from each row of the table.

Each step is matched to a step definition:

@When("the user logs in with {string} and {string}")
public void login(String user, String pass) {
    driver.findElement(By.id("username")).sendKeys(user);
    driver.findElement(By.id("password")).sendKeys(pass);
    driver.findElement(By.id("login")).click();
}

The two {string} parts catch the values in quotes and pass them in as user and pass; the method then types them and clicks Login.

A step with no matching definition is reported as undefined, and Cucumber suggests a code snippet for it.

API testing

API test

An API test sends a request directly to a service and checks the reply. It can check service behaviour without using a web page.

SOAP (originally Simple Object Access Protocol) REST (Representational State Transfer)
Message format XML (Extensible Markup Language) envelope Usually JSON (JavaScript Object Notation)
Contract WSDL (Web Services Description Language) Optional – OpenAPI
Operations Named operations HTTP (Hypertext Transfer Protocol) methods on resources (GET, POST, PUT, DELETE)
Errors SOAP Fault HTTP status codes (400, 404, 500)

What to check in an API test

  • Status code – 200, 201, 204, 400, 404…
  • Headers – Content-Type, Location
  • Body – the values, JSON schema, XML elements (XPath/JSONPath)
  • Response time – below a limit
  • Negative cases – wrong input returns the right error

SoapUI and Postman

Feature SoapUI Postman
Best known for SOAP and REST functional + load testing REST API development and testing
Structure Project → TestSuite → TestCase → TestSteps Collection → folders → requests
Checks Assertions – Valid HTTP Status, Contains, XPath / JSONPath Match, SLA (Service Level Agreement) Test scripts in JavaScript – pm.test(...), pm.expect(...)
Chaining requests Property Transfer Variables – pm.environment.set("id", …)
Environments Properties / environments Environments ({{baseUrl}})
Running many TestSuite runner, LoadTest (threads, strategy) Collection Runner, Newman (CLI (Command-Line Interface))
// Postman – Tests tab
pm.test("Status is 201", () => pm.response.to.have.status(201));
pm.environment.set("studentId", pm.response.json().id);
  • Line 1 – the test passes only if the status is 201 Created.
  • Line 2 – saves the new student's id into a variable for the next request.
Key point

BDD: Feature → Scenario → Given/When/Then → step definitions. API testing: check status, headers, body and time. SoapUI uses assertions and property transfer; Postman uses JavaScript tests and variables.

Practise it

Virtual lab · Testing Tools · 40 min

Behaviour-driven development with Cucumber

Run Gherkin feature files against the VLP Demo Store, turn undefined steps into Java step definitions from Cucumber’s snippets, get all scenarios green and filter runs with tags.

Open lab

Virtual lab · Testing Tools · 45 min

Functional and load testing of a REST API with SoapUI and Postman

Build a SoapUI test case for the Students API with assertions and property transfer, run a load test, then recreate the checks in Postman with test scripts, an environment and the Collection Runner.

Open lab

Quick revision

  1. BDD turns business examples into automated tests.
  2. Gherkin: Feature, Scenario, Given, When, Then, Scenario Outline, Examples, tags.
  3. Step definitions connect plain-English steps to code.
  4. API tests check status, headers, body and response time.
  5. SoapUI: suites, assertions, property transfer, load tests; Postman: collections, scripts, environments, runner.

Practise this topic

Hands-on virtual labs

Apply what you just read in a realistic, guided simulation. Free with a verified account.

Create a free account to track your progress and unlock virtual labs.