Skip to main content

pytest plugin for random test runner with reproducible execution

Project description

pytest-monkey

A pytest plugin and CLI tool for running tests multiple times in random order, supporting reproducible random test runs. Similar to the Android monkey tool. Normal pytest runs all test cases sequentially once. This tool randomly runs tests a specified number of times (e.g., 10000), stopping on failure (configurable to continue). Each run prints a random seed value, allowing you to reproduce the same test order by specifying the seed.

Features

  • Random Order Execution: Randomly select and run tests multiple times from a test collection
  • Reproducible Runs: Ensure test order reproducibility via seed parameter
  • pytest Plugin Mode: Seamlessly integrate as a pytest plugin
  • Standalone CLI Tool: Use independently as a command-line tool
  • Detailed Reports: Automatically generate JSON and HTML format test reports
  • Failure Control: Support continuing execution or stopping immediately on failure

Installation

pip install pytest-monkey

Or using PDM:

pdm install

Usage

1. As pytest Plugin

Add --random-runner option to pytest command:

# Basic usage - run 100 random tests
pytest tests/ --random-runner --random-count 100

# Specify seed to reproduce test order
pytest tests/ --random-runner --random-count 100 --random-seed 42

# Continue on failure
pytest tests/ --random-runner --random-count 100 --random-continue-on-fail

# Show test output (similar to pytest -s)
pytest tests/ --random-runner --random-count 100 --random-no-capture

2. As CLI Tool

Use the installed pytest-monkey command:

# Run 100 random tests from directory
pytest-monkey tests/ --count 100

# Run 50 tests from single file
pytest-monkey tests/test_file.py --count 50

# Specify test method and set seed
pytest-monkey tests/test_file.py::test_name --count 20 --seed 42

# Continue on failure, verbose output
pytest-monkey tests/ --count 100 --continue-on-fail -v

# Test names and stdout/stderr are shown by default (equivalent to pytest -v -s)
pytest-monkey tests/ --count 100

Parameter Reference

pytest Plugin Parameters

Parameter Description
--random-runner Enable random test run mode
--random-count Number of test runs (required)
--random-seed Random seed (auto-generated 10-digit number by default)
--random-continue-on-fail Continue execution on failure
--random-no-capture Disable output capture, show test output

CLI Parameters

Parameter Description
test_spec Test target (file/class/method/directory path)
--count Number of test runs (required)
--seed Random seed (auto-generated 10-digit number by default)
--continue-on-fail Continue execution on failure
--report-dir Report save directory (default ./reports)
-v, --verbose Show verbose run results (enabled by default)
-s, --no-capture Show test stdout/stderr without capture (enabled by default)

Test Reports

Two reports are automatically generated after each run:

  • JSON Report: reports/report.json
  • HTML Report: reports/report.html

Reports include:

  • Random seed
  • Total runs, pass/fail statistics
  • Detailed results for each test (name, duration, error message)
  • Run timestamp

Reproducing Tests

Random seed is printed during run, for example:

Random seed: 1234567890

Use the same seed to reproduce the same test order:

pytest-monkey tests/ --count 100 --seed 1234567890

Test Selection Syntax

Supports pytest standard selection syntax:

  • File: tests/test_xxx.py
  • Class: tests/test_xxx.py::TestClass
  • Method: tests/test_xxx.py::TestClass::test_method
  • Directory: tests/

Development

Project Structure

pytest-monkey/
├── runner/
│   ├── core.py       # Core runner
│   ├── collector.py  # Test collector
│   ├── selector.py   # Random selector
│   ├── executor.py   # Test executor
│   ├── reporter.py   # Report generator
│   └── models.py     # Data models
├── pytest_monkey.py          # pytest plugin entry
├── random_runner.py         # CLI entry
└── tests/                   # Test files

Running Tests

pytest tests/

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_monkey-0.1.0.tar.gz (12.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_monkey-0.1.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file pytest_monkey-0.1.0.tar.gz.

File metadata

  • Download URL: pytest_monkey-0.1.0.tar.gz
  • Upload date:
  • Size: 12.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_monkey-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f476b2852a75464fb3107a710cbb4259ba403ce842290c371ea9f8c10c51643c
MD5 289a474eace2409798f586aa1536825e
BLAKE2b-256 bf743f8a9f92ebb0a2a0ce2fe93bfd489d98ea4778b9c2d9e71fdf6bee07ae14

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_monkey-0.1.0.tar.gz:

Publisher: ci.yml on enrico-j-yang/pytest-monkey

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_monkey-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytest_monkey-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pytest_monkey-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3c7e91bfdeaabe9fe16320de5fcdb83011e5b22357424315a0155dac9916dc62
MD5 b82ee55130be692297b898722abef87a
BLAKE2b-256 4a429e87faee9b973bb3e9302377df4f19fe5f66f8ade63dcbab65ba958e9d46

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_monkey-0.1.0-py3-none-any.whl:

Publisher: ci.yml on enrico-j-yang/pytest-monkey

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page