Read Anthropic’s case study about Graphite Reviewer

Automating API contract testing in GitHub pull requests

Sara Verdi
Sara Verdi
Graphite software engineer
Try Graphite

API contract testing is an important practice in software development that ensures APIs function as intended and meet predefined specifications. By validating that API responses align with the expected contract, teams can catch issues early in the development cycle. Automating API contract testing within GitHub pull requests (PRs) ensures that any proposed changes do not break existing functionality.

This guide will explore how to automate API contract testing in GitHub PRs, integrating it with your continuous integration (CI) processes, leveraging tools like Graphite Automations.

API contract testing focuses on verifying that the API's behavior conforms to its contract, which typically includes:

  • Request and response formats: Ensuring that API requests and responses adhere to specified formats (e.g., JSON schemas).
  • Status codes: Checking that the API returns the correct HTTP status codes.
  • Error messages: Verifying that error messages are returned as expected.

Automating these tests can:

  • Reduce manual testing effort.
  • Ensure consistent testing across different branches and environments.
  • Catch issues early in the development process.

Before automating API contract testing in your GitHub PRs, ensure you have:

  • A GitHub repository with API code.
  • A CI/CD pipeline configured (e.g., GitHub Actions).
  • A testing framework (e.g., Postman, Jest, or Mocha) that supports API contract testing.

To get started, clearly define your API contract using a specification format like OpenAPI. This specification describes your API endpoints, request and response structures, and any other relevant details.

Here's a simple example of an OpenAPI contract:

Terminal
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/users:
get:
summary: Get all users
responses:
'200':
description: A list of users
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string

Using a testing framework, write tests that validate your API against the defined contract. For instance, if you're using Jest with Supertest for Node.js, your tests might look like this:

Terminal
const request = require('supertest')
const app = require('../app') // Your Express app
describe('GET /users', () => {
it('should return a list of users', async () => {
const response = await request(app).get('/users')
expect(response.status).toBe(200)
expect(response.body).toBeInstanceOf(Array)
// Additional checks for user object structure can go here
})
})

Create a .github/workflows/api-testing.yml file in your repository to configure GitHub Actions for running your API tests on each PR. Here's a sample configuration:

Terminal
name: API Testing
on:
pull_request:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14'
- name: Install dependencies
run: npm install
- name: Run API contract tests
run: npm test

This configuration will trigger the API tests every time a PR is created or updated against the main branch.

To optimize your development workflow, leverage Graphite Automations to automate repetitive code review tasks. Graphite allows you to create automation rules that trigger specific actions when pull requests (PRs) meet certain criteria. Here's how you can integrate Graphite Automations:

  1. Set up Graphite automations: Navigate to the Automations section in the Graphite web app and click on Create rule. For detailed instructions, refer to Graphite's documentation.

  2. Define automation rules: Configure conditions that match PR attributes, such as the author, files changed, or directories modified. For example, you can set up rules to automatically add reviewers when a PR includes changes to critical code areas.

  3. Specify actions: Choose one or more actions for Graphite to perform when the conditions are met. Actions can include:

    • Adding reviewers or assignees to the PR.
    • Applying labels for better categorization.
    • Leaving a comment to provide additional instructions or reminders.
    • Notifying team members via Slack for immediate attention.
    • Posting a GIF to celebrate approvals or milestones.

By integrating Graphite Automations, you ensure that PRs are consistently handled according to your team's workflow. This automation reduces manual effort, ensures timely reviews, and keeps everyone informed, allowing your team to focus on delivering high-quality code.

Once your API contract tests are automated, it's essential to monitor their performance. You can use Graphite Protections to ensure that tests pass before merging changes. Additionally, consider integrating notifications to alert your team of test failures.

  • Keep your contracts updated: Regularly review and update your API contracts and tests as your API evolves.
  • Implement versioning: Use versioning for your API to manage changes effectively without breaking existing contracts.
  • Run tests in parallel: If you have a large test suite, configure your CI pipeline to run tests in parallel, reducing feedback time.
  • Use mock services: For complex APIs, consider using mock services to simulate responses and avoid dependencies on external systems.

Automating API contract testing in GitHub pull requests is a powerful strategy for maintaining the quality and reliability of your APIs. By leveraging GitHub Actions and Graphite Automations, you can streamline your testing process and ensure that your API changes are validated efficiently.

For further exploration, check out these resources:

By following these practices, you can accelerate your development workflow and reduce the risk of introducing bugs into your API.

Built for the world's fastest engineering teams, now available for everyone