Introduction
In our previous post, we explored Test Reporting with REST Assured and Allure, learning how to generate detailed test reports. Now, we’ll dive into Integration with CI/CD, a critical practice for automating REST Assured tests in continuous integration and continuous deployment pipelines. This guide uses GitHub Actions to set up a CI/CD pipeline, integrating REST Assured tests and Allure reports. It’s designed for beginners and experienced developers, providing clear explanations and practical examples.
Key Point: Integrating REST Assured tests with CI/CD ensures automated, consistent, and reliable API testing, enabling faster feedback and higher quality in software delivery.
What is CI/CD Integration?
Continuous Integration (CI) involves automatically building and testing code changes in a shared repository, ensuring early detection of issues. Continuous Deployment (CD) extends this by automatically deploying successful changes to production.
In the context of REST Assured, CI/CD integration means running API tests automatically whenever code is pushed to a repository, generating reports, and notifying teams of results. We’ll use GitHub Actions, a CI/CD platform, to execute REST Assured tests and publish Allure reports.
Setting Up CI/CD for REST Assured
To integrate REST Assured tests with CI/CD, we’ll:
- Create a sample REST Assured test project.
- Configure a GitHub Actions workflow to run tests.
- Generate and publish Allure reports.
- Use the public API
https://jsonplaceholder.typicode.com
for testing.
Ensure your project is hosted on GitHub and includes the necessary dependencies.
Project Setup
Create a Maven project with REST Assured, JUnit, and Allure dependencies. Below is the pom.xml
:
11
11
2.27.0
1.9.22
io.rest-assured
rest-assured
5.4.0
test
org.junit.jupiter
junit-jupiter
5.10.2
test
org.hamcrest
hamcrest
2.2
test
io.qameta.allure
allure-junit5
${allure.version}
test
org.apache.maven.plugins
maven-surefire-plugin
3.2.5
-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"
allure.results.directory
target/allure-results
org.aspectj
aspectjweaver
${aspectj.version}
io.qameta.allure
allure-maven
2.12.0
${allure.version}
Create a sample REST Assured test in src/test/java
:
package com.example.tests;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
@Feature("Post API Tests")
public class PostApiTest {
@Test
@Description("Verify retrieval of a single post by ID")
public void testGetPost() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
sendGetRequest(1)
.then()
.statusCode(200)
.body("id", equalTo(1))
.body("title", notNullValue());
}
@Test
@Description("Verify creation of a new post")
public void testCreatePost() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
String requestBody = "{\"title\": \"Test Post\", \"body\": \"Test Body\", \"userId\": 1}";
given()
.contentType("application/json")
.body(requestBody)
.when()
.post("/posts")
.then()
.statusCode(201)
.body("title", equalTo("Test Post"));
}
@Step("Send GET request for post ID {postId}")
private Response sendGetRequest(int postId) {
return given()
.pathParam("postId", postId)
.when()
.get("/posts/{postId}");
}
}
Push this project to a GitHub repository (e.g., your-username/rest-assured-tests
).
Configuring GitHub Actions Workflow
Create a GitHub Actions workflow to run REST Assured tests and generate Allure reports.
Create a file named .github/workflows/ci.yml
in your repository:
name: REST Assured CI Pipeline
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build-and-test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '11'
distribution: 'temurin'
- name: Cache Maven dependencies
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: ${{ runner.os }}-maven-
- name: Build and run tests
run: mvn clean test
- name: Generate Allure report
run: mvn allure:report
- name: Upload Allure results
if: always()
uses: actions/upload-artifact@v4
with:
name: allure-results
path: target/allure-results
- name: Publish Allure report
if: always()
uses: simple-elf/allure-report-action@v1.7
with:
allure_results: target/allure-results
gh_pages: gh-pages
allure_report: allure-report
- name: Deploy report to GitHub Pages
if: always()
uses: peaceiris/actions-gh-pages@v4
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: allure-report
Explanation:
on
: Triggers the workflow on pushes or pull requests to themain
branch.runs-on: ubuntu-latest
: Uses a Ubuntu runner for the job.- Steps:
- Checks out the code.
- Sets up JDK 11 for Maven.
- Caches Maven dependencies for faster builds.
- Runs
mvn clean test
to execute tests. - Generates an Allure report with
mvn allure:report
. - Uploads Allure results as artifacts.
- Publishes the report to GitHub Pages using a third-party action.
Enable GitHub Pages:
- Go to your repository’s Settings > Pages.
- Set the source to the
gh-pages
branch. - After the workflow runs, the Allure report will be available at
https://your-username.github.io/rest-assured-tests/allure-report
.
Important: Ensure your repository is public or has GitHub Actions enabled for private repositories. The GITHUB_TOKEN
secret is automatically available for deploying to GitHub Pages.
Running Tests in CI/CD
With the workflow configured, pushing code to the main
branch or opening a pull request triggers the pipeline:
- Tests run using
mvn clean test
. - Allure results are generated in
target/allure-results
. - The report is published to GitHub Pages.
View the pipeline status in the Actions tab of your GitHub repository. Download Allure results from the artifacts section or access the report via the GitHub Pages URL.
Example: Enhancing Tests with Logging
Add logging to tests to capture details in the CI/CD pipeline for debugging.
package com.example.tests;
import io.qameta.allure.Allure;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.qameta.allure.Step;
import io.restassured.RestAssured;
import io.restassured.response.Response;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
@Feature("Post API Tests")
public class PostApiWithLoggingTest {
@Test
@Description("Verify retrieval of a post with logging")
public void testGetPostWithLogging() {
RestAssured.baseURI = "https://jsonplaceholder.typicode.com";
Response response = sendGetRequest(1);
Allure.addAttachment("Response Body", "application/json", response.asString(), ".json");
response.then()
.statusCode(200)
.body("id", equalTo(1))
.body("title", notNullValue());
}
@Step("Send GET request for post ID {postId}")
private Response sendGetRequest(int postId) {
String requestDetails = "GET /posts/" + postId;
Allure.addAttachment("Request Details", "text/plain", requestDetails, ".txt");
return given()
.log().all()
.pathParam("postId", postId)
.when()
.get("/posts/{postId}");
}
}
Explanation:
log().all()
: Logs request and response details to the console, captured in CI logs.Allure.addAttachment
: Attaches request details and response body to the Allure report.- These attachments are accessible in the GitHub Pages report, aiding debugging in CI/CD.
Using Request Specification in CI/CD
Use a Request Specification to ensure consistent test setup in the CI/CD pipeline.
package com.example.tests;
import io.qameta.allure.Description;
import io.qameta.allure.Feature;
import io.restassured.RestAssured;
import io.restassured.builder.RequestSpecBuilder;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;
@Feature("Post API Tests")
public class PostApiWithSpecTest {
private static RequestSpecification requestSpec;
@BeforeAll
public static void setup() {
requestSpec = new RequestSpecBuilder()
.setBaseUri("https://jsonplaceholder.typicode.com")
.addHeader("Accept", "application/json")
.log(LogDetail.ALL)
.build();
}
@Test
@Description("Verify post retrieval using request specification")
public void testGetPostWithSpec() {
given()
.spec(requestSpec)
.pathParam("postId", 1)
.when()
.get("/posts/{postId}")
.then()
.statusCode(200)
.body("id", equalTo(1))
.body("userId", equalTo(1));
}
}
Explanation:
requestSpec
: Centralizes base URI, headers, and logging.spec(requestSpec)
: Applies the specification to tests, ensuring consistency in CI/CD runs.
Pro Tip: Use Request Specifications to reduce duplication and maintain consistent logging and headers across tests in the CI/CD pipeline.
Tips for Beginners
- Start Simple: Begin with a basic workflow running
mvn test
before adding reporting. - Use Artifacts: Upload Allure results as artifacts to debug failures without redeploying.
- Monitor Logs: Check GitHub Actions logs for Maven errors or test failures.
- Add Notifications: Configure GitHub Actions to send Slack or email alerts for pipeline status.
Troubleshooting Tip: If the pipeline fails, check the GitHub Actions logs for Maven errors, missing dependencies, or API rate limits. Ensure Allure results are generated in target/allure-results
before report publication.
What’s Next?
In the next post, we’ll cover Best Practices for REST Assured, exploring tips and techniques to write maintainable, efficient, and robust API tests. Stay tuned for more hands-on examples!