Skip to content

Latest commit

 

History

History
146 lines (101 loc) · 4.07 KB

File metadata and controls

146 lines (101 loc) · 4.07 KB

Test Runner App — test_runner_app.py

Overview

test_runner_app.py is a Flask-based web application that provides a browser UI for discovering, running, and monitoring pytest tests in real time. Instead of running pytest -v --html=report.html from the terminal, users can do everything visually from a web page.

It runs on port 5001 separately from the Petstore API (which runs on port 5000).


How to Start

# Make sure the virtual environment is activated
venv\Scripts\activate

# Start the test runner UI
python test_runner_app.py

The browser will automatically open http://localhost:5001 when started.


Pages

URL Description
http://localhost:5001/ Main test runner UI
http://localhost:5001/report Pytest HTML report viewer

How It Works — Component Breakdown

1. LiveTestPlugin (Class)

A custom pytest plugin that hooks into pytest's internal reporting system.

  • pytest_runtest_logreport — fires after each test runs and captures its result (passed, failed, skipped) and puts it into a shared queue
  • pytest_sessionfinish — fires when all tests are done and sends a done signal to the queue

This is what enables real-time updates in the browser.


2. run_pytest_thread (Function)

Runs pytest in a background thread so the Flask server stays responsive while tests are executing.

  • Accepts a list of selected test node IDs
  • Injects the LiveTestPlugin so results are streamed live
  • Also generates report.html automatically after each run

3. API Routes

GET /

Serves the main UI page (templates/index.html) where users can see and run tests.

GET /api/tests

Uses subprocess to run pytest --collect-only and returns a JSON list of all discovered test IDs.

Example response:

[
  "test_pet.py::test_pet_schema",
  "test_pet.py::test_find_by_status_200[available]",
  "test_store.py::test_patch_order_by_id[sold]"
]

POST /api/run

Accepts a list of selected test IDs and starts running them in a background thread.

Request body:

{ "tests": ["test_pet.py::test_pet_schema"] }

Response:

{ "status": "started" }

Returns 400 if tests are already running.

GET /api/stream

A Server-Sent Events (SSE) endpoint that streams live test results to the browser as they complete.

Event types:

Type Meaning
result A test finished — includes test name and status
done All tests completed
heartbeat Keep-alive ping every second

Example event:

data: {"type": "result", "test": "test_pet.py::test_pet_schema", "status": "passed"}

GET /report

Serves the report viewer page (templates/report_page.html) which embeds the pytest HTML report in an iframe.

GET /report-raw

Serves the raw report.html file generated by pytest. Returns 404 if no report exists yet.

GET /assets/<filename>

Serves the CSS and assets needed by report.html so it renders correctly in the browser.


UI Features (templates/index.html)

Feature Description
Summary Cards Shows total / passed / failed / skipped / pending counts
Test List All discovered tests displayed with checkboxes
Select All / Deselect All Bulk checkbox controls
Run Selected Runs only the checked tests
Live Status Badges Each test badge updates in real time: pending → running → passed/failed/skipped
Progress Bar Visual progress as tests complete
Status Bar Text feedback e.g. "Running... 3/7 completed"
View Report Opens the full pytest HTML report in a new tab

Architecture Flow

Browser → GET /api/tests         → Collect all tests
Browser → POST /api/run          → Start tests in background thread
Browser → GET /api/stream (SSE)  → Receive live results
Browser → GET /report            → View full HTML report

Dependencies

All dependencies are already included in the project's install command:

pip install flask pytest