Skip to main content
In this final section we’ll master GitHub Copilot’s core features by building a practical end-to-end project: a Fake Data Generator API. This lesson demonstrates how to design, implement, document, and test a small FastAPI service that returns realistic synthetic data for development and QA—helping your team comply with privacy rules by avoiding real customer data in tests. Goals for this lesson
  • Introduce the project and goals.
  • Create a new FastAPI project and implement the application.
  • Add inline code documentation and generate API documentation (OpenAPI/Swagger).
  • Write unit tests to validate behavior and integrate tests into CI.
  • Demonstrate AI pair-programming workflows with GitHub Copilot to accelerate development.
Project overview Your organization introduced a privacy rule: production customer data cannot be used for testing. Since test data does not need to be real, we’ll implement a Fake Data Generator API that produces realistic, synthetic datasets on demand. The API will be built in Python using FastAPI and will support multiple output formats (JSON, CSV, plain text) and parameterized requests (e.g., record count, fields). Example fake data output (JSON)
What we’ll implement
  • A FastAPI application that generates realistic mock records on demand.
  • Endpoints to request:
    • A specific count of records.
    • Different output formats: JSON (application/json), CSV (text/csv), and plain text.
  • A fetch_fake_data function that produces synthetic records (Pydantic models recommended).
  • Unit tests using pytest and FastAPI’s TestClient.
  • Automatically generated API docs via FastAPI (OpenAPI/Swagger) and developer docs.
This fake data generator helps you comply with privacy rules by producing realistic but synthetic data for development, QA, and demos without exposing customer information.
API design summary
  • Tech stack: Python, FastAPI, Pydantic for schemas, and a lightweight data generator (e.g., Faker).
  • Primary route: POST /api/v1/getfakedata — accepts JSON body with parameters such as count, format, and fields.
  • Response types: application/json, text/csv, or text/plain.
  • Validation: Use Pydantic models to validate request payloads and API responses.
Endpoints at a glance Example request payloads
  • Minimal JSON request:
  • Request specifying output format and fields:
Example curl (JSON response)
Implementation notes and recommendations
  • Use Pydantic models for both request validation and response schemas to get strong typing and automatic OpenAPI docs.
  • Use the Faker library (or a custom generator) to produce realistic but synthetic names, emails, cities, and occupations.
  • Return a well-formed CSV with headers when format is csv. For large datasets consider streaming responses.
  • Add logging and rate-limiting if the service will be exposed across teams.
Example unit test Below is a representative unit test that validates the API endpoint returns the expected fake data when the underlying data-fetching function is patched. This test uses FastAPI’s TestClient and unittest.mock.patch.
Testing and CI tips
  • Use pytest for unit tests, and include fixtures for sample synthetic data.
  • Mock out the generator function to control returned data and test edge cases (zero records, invalid fields, unsupported formats).
  • Add tests to verify content-type headers for each format and that CSV output contains expected headers.
  • Integrate tests into CI (GitHub Actions, GitLab CI, etc.), run formatting (black), linting (flake8/ruff), and tests on every PR.
Implementation checklist
  • Initialize a FastAPI project and repository.
  • Define request and response Pydantic models.
  • Implement fetch_fake_data(count, fields=None) using Faker or similar.
  • Create /api/v1/getfakedata route with format handling and proper response headers.
  • Add unit tests covering success, validation errors, and edge cases.
  • Configure OpenAPI docs (auto with FastAPI) and include additional README developer docs.
  • Add CI pipeline to run tests and checks; optionally publish a Docker image.
Next steps in the lesson
  • Scaffold the FastAPI project and routes for generating fake data.
  • Implement fetch_fake_data so it returns realistic mock records in the requested shape.
  • Add and run tests like the example above, and integrate them into CI.
  • Generate API documentation (OpenAPI/Swagger and additional developer docs).
  • Use GitHub Copilot as an AI pair programmer to accelerate implementation, create tests, and write documentation.
Links and references

Watch Video