Skip to content

scripting-drafts/API-Testing

Repository files navigation

Restful API Framework

Tests

This repository contains integration-style pytest tests that exercise the main endpoints (ping, auth, create/list/get/update/patch/delete bookings). Tests are intended to run against the public demo service but can be adapted to use mocks for offline testing or CI.

Quick start

  1. Clone the repository and change into the project root:

    git clone <repo-url>
    cd restful_api_framework
  2. Create a virtual environment and install dependencies:

    python -m venv .venv
    source .venv/bin/activate      # macOS / Linux
    .venv\Scripts\Activate.ps1     # Windows PowerShell
    pip install -r requirements.txt

    If requirements.txt is not present, install the minimal deps:

    pip install pytest requests
  3. Run the full test suite (integration tests hit the live demo API):

    pytest -q

Environment / Configuration

  • BASE_URL

    • Default (used in tests): https://restful-booker.herokuapp.com
    • You can override this to point tests at another server:
      export BASE_URL="http://localhost:3000"   # macOS / Linux
      setx BASE_URL "http://localhost:3000"     # Windows (new session required)
  • AUTH_USERNAME / AUTH_PASSWORD

    • Default used by tests: admin / password123
    • You can override if your server uses different credentials:
      export AUTH_USERNAME="admin"
      export AUTH_PASSWORD="password123"

Test design

  • Tests are written with pytest.
  • They exercise the public API directly and therefore are integration tests that require network access.
  • Fixtures include:
    • base_url — base endpoint
    • auth_token — obtains an auth token from /auth and is used for update/delete operations
    • create_booking — helper fixture to create bookings and perform best-effort cleanup
  • Recommended marker:
    • Mark integration tests with @pytest.mark.integration if you want to skip them during fast/local runs:
      pytest -m "not integration"
      pytest -m integration

Running a single test

Run a single test file:

pytest tests/test_restful_booker.py::test_create_and_get_booking -q

Run tests matching a keyword:

pytest -k create -q

Offline / Mocked testing

If you prefer not to hit the live service:

  • Use libraries such as responses, requests-mock, or vcrpy to stub HTTP interactions.
  • Example (install):
    pip install responses
  • Replace network calls in tests with mocked responses, or add a pytest fixture that toggles a requests adapter.

Project layout (convention)

  • api/ - optional in-repo application code
  • tests/ - pytest test modules
    • conftest.py - shared fixtures and sys.path adjustments
  • requirements.txt - Python dependencies
  • README.md

Continuous integration

  • Add a CI step to create a virtualenv, install -r requirements.txt, and run pytest.
  • For public demo tests, consider isolating or gating integration tests so CI does not fail due to external service outages. Use pytest -m "not integration" in fast CI pipelines, and run integration tests separately.

About

Schema validations, performance and load tests

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages