Q36 of 40 · REST Assured
How do you measure and optimise REST Assured test execution time?
Short answer
Short answer: Profile first: most suites are I/O-bound and parallelise well. Reduce redundant auth-token fetches with @BeforeAll caching. Use response spec SLA assertions (.expectResponseTime) to catch slow endpoints. Profile with maven-surefire timing output before tuning parallelism — bottlenecks are usually data setup, not REST Assured overhead.
Detail
Step 1 — measure: run with mvn test -Dsurefire.reportFormat=brief to see per-test timing. Identify the 20% of tests responsible for 80% of the run time.
Step 2 — parallelise I/O-bound tests: API tests wait on the network — they are almost always I/O-bound and parallelise extremely well. Increasing thread count from 1 to 4 typically reduces total time by 3-3.5x with no code changes.
Step 3 — eliminate redundant setup:
- Token fetch: done once in
@BeforeAll, not per test - Test data creation: expensive resources created once per class (
@BeforeAll) for read-only tests; per-method only for mutating tests - Base URI and spec building: done once, never re-created per test
Step 4 — reduce test scope:
- A slow test that asserts 20 fields in one chain is fine — don't split it
- A test that re-creates 50 database rows to test one field is a design problem — fix the data strategy
Step 5 — SLA assertions catch slow endpoints proactively:
resSpec = new ResponseSpecBuilder()
.expectResponseTime(lessThan(2000L), MILLISECONDS)
.build();
// EXAMPLE
// Before optimisation: sequential, one token fetch per test
@Test void test1() { fetchToken(); given()... }
@Test void test2() { fetchToken(); given()... }
@Test void test3() { fetchToken(); given()... }
// 3 tests × (200ms token fetch + 300ms API call) = 1500ms
// After: one token fetch, parallel execution
@BeforeAll
static void setup() {
accessToken = fetchToken(); // 200ms — once for the suite
reqSpec = new RequestSpecBuilder()
.addHeader("Authorization", "Bearer " + accessToken)
.build();
}
// junit-platform.properties:
// junit.jupiter.execution.parallel.enabled=true
// junit.jupiter.execution.parallel.config.fixed.parallelism=4
// 3 tests × 300ms API call / 4 threads ≈ 300ms total (+ 200ms setup = 500ms)
// SLA assertion — fails test if response > 2s (catches regressions early)
resSpec = new ResponseSpecBuilder()
.expectResponseTime(lessThan(2000L), MILLISECONDS)
.build();