Step-by-Step Guide to Automating FastAPI E2E Tests with GitHub Actions

Automating end-to-end (E2E) tests for your FastAPI applications is essential for maintaining code quality and ensuring reliable deployments. GitHub Actions provides a powerful platform to automate these tests seamlessly. This guide walks you through setting up automated FastAPI E2E tests using GitHub Actions.

Prerequisites

  • Basic knowledge of FastAPI
  • Experience with GitHub repositories
  • Docker installed locally (optional for testing)
  • Access to a GitHub repository where your FastAPI project resides

Step 1: Prepare Your FastAPI Application for Testing

Ensure your FastAPI application can be started with a command and that tests can be run via a script. For example, you might have a script in your pyproject.toml or requirements.txt that runs your E2E tests.

Example directory structure:

  • main.py – Your FastAPI app
  • tests/e2e/test_api.py – Your E2E tests
  • Dockerfile – Docker configuration (if used)

Step 2: Create a GitHub Actions Workflow File

In your repository, create a new directory named .github/workflows. Inside, add a new YAML file, e.g., e2e-tests.yml.

This file defines the automation process for your tests.

Step 3: Define the Workflow Configuration

Below is a sample configuration for running E2E tests on push events to the main branch.

name: E2E Tests for FastAPI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest
    services:
      fastapi:
        image: tiangolo/uvicorn-gunicorn-fastapi:latest
        ports:
          - 8000:80
        options: --health-ccheck --health-start-period=10s

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.11'

      - name: Install dependencies
        run: |
          python -m pip install --upgrade pip
          pip install -r requirements.txt

      - name: Start FastAPI server
        run: |
          uvicorn main:app --host 0.0.0.0 --port 80 &
          sleep 10

      - name: Run E2E tests
        run: |
          pytest tests/e2e

Step 4: Write Your E2E Tests

Create your E2E tests using a testing framework like pytest. Your tests should send requests to http://localhost:80 or the appropriate URL if using Docker or other environment setups.

Example test (tests/e2e/test_api.py):

import requests

def test_get_items():
    response = requests.get("http://localhost:80/items")
    assert response.status_code == 200
    data = response.json()
    assert "items" in data

Step 5: Commit and Push Your Workflow

Save your workflow file and push it to your GitHub repository. Once pushed, GitHub Actions will automatically trigger the workflow on specified events, running your E2E tests.

Additional Tips

  • Use Docker for more consistent testing environments.
  • Configure environment variables securely via GitHub Secrets.
  • Set up notifications for failed tests to stay informed.

Automating FastAPI E2E tests with GitHub Actions helps catch bugs early and streamlines your deployment process. Customize the workflow to fit your project’s needs and enjoy reliable, automated testing.