Stop 08 · API TestingTopics · 5/9

Automating API Tests (REST Assured / Requests)

Part of the QA Testing Roadmap

In short

Clicking Send in Postman doesn't scale to 400 endpoints run on every commit. Code-based API tests run in seconds, live in Git and plug straight into CI.

Moving from manual Postman testing to automated, code-based API tests — REST Assured for Java, Requests library for Python — enabling API tests to run automatically as part of a CI/CD pipeline.

The purple blob feeds dough balls into a small machine that stacks round chapatis.

Think of it like this

The automatic chapati machine

Rolling 500 chapatis by hand for a wedding takes all day and each one comes out a bit different. The chapati machine does the same steps every time, fast. Automated API tests are that machine for your requests and checks.

Watch it happen

What each automated API test does

Send
Status
Body
Time
Report

1/5Build the request in code: method, URL, headers, JSON body.

The big ideas

What you need to know

Requests for Python

The requests library plus pytest is the simplest way to start API automation.

REST Assured for Java

REST Assured uses a given().when().then() style that reads like a sentence. Popular in Java teams.

Same checks, every build

Tests run on each commit, so a broken endpoint is caught within minutes.

Share helpers

Put base URLs, auth headers and common checks in one helper module.

See it in code

Walk through the code

python
1import requests2 3BASE = "https://jsonplaceholder.typicode.com"4 5def test_get_post_returns_expected_fields():6    r = requests.get(f"{BASE}/posts/1", timeout=10)7    assert r.status_code == 2008    body = r.json()9    assert body["id"] == 110    assert all(key in body for key in ("userId", "title", "body"))11 12def test_create_post_returns_201():13    r = requests.post(f"{BASE}/posts", json={"title": "hi", "body": "x", "userId": 1}, timeout=10)14    assert r.status_code == 20115    assert r.json()["title"] == "hi"

1/3Send a GET and check the status first. If that fails, nothing else matters.

Save as test_posts.py and run pytest -v. In Java, REST Assured expresses the same checks with given/when/then.

Save yourself the pain

Common mistakes

Oops

Copying the full URL and token into every single test.

Do this instead

Keep the base URL and auth in one config or fixture and reuse it.

Oops

Tests that depend on data someone else might delete from the shared QA server.

Do this instead

Create the data you need in the test, then clean it up afterwards.

Your mission

Automate three API checks

~35 min

Quick check

Why always pass a timeout to requests.get() in a test?

Go deeper

  1. 1Automate a set of API tests using your chosen language's library.
  2. 2Practice asserting both status code and response body content in your automated tests.
  3. 3Learn to parameterize tests to run the same logic against multiple input scenarios.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge