Skip to main content

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

Release files for pytest-monkey 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-monkey 0.1.0
File Size Uploaded
pytest_monkey-0.1.0.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-monkey 0.1.0
File Interpreter ABI Platform
pytest_monkey-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.3 kB

Release files / pytest_monkey-0.1.0.tar.gz

Download URL pytest_monkey-0.1.0.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f476b2852a75464fb3107a710cbb4259ba403ce842290c371ea9f8c10c51643c
BLAKE2b-256 checksum
How to use checksums
bf743f8a9f92ebb0a2a0ce2fe93bfd489d98ea4778b9c2d9e71fdf6bee07ae14
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.

Transparency log

Release files / pytest_monkey-0.1.0-py3-none-any.whl

Download URL pytest_monkey-0.1.0-py3-none-any.whl
Size 15.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3c7e91bfdeaabe9fe16320de5fcdb83011e5b22357424315a0155dac9916dc62
BLAKE2b-256 checksum
How to use checksums
4a429e87faee9b973bb3e9302377df4f19fe5f66f8ade63dcbab65ba958e9d46
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Jul 17, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page