Metadata-Version: 2.4
Name: pytest-xdist-load-testing
Version: 0.2.4
Summary: xdist scheduler to repeately run tests
Author-email: Xavier Vergés <github@verg.es>
Maintainer-email: Xavier Vergés <github@verg.es>
License: 
        The MIT License (MIT)
        
        Copyright (c) 2025 Xavier Vergés
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in
        all copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        THE SOFTWARE.
        
Project-URL: Repository, https://github.com/xverges/pytest-xdist-load-testing
Classifier: Framework :: Pytest
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Testing
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: License :: OSI Approved :: MIT License
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: pytest>=8.4.2
Requires-Dist: pytest-xdist>=3.8.0
Dynamic: license-file

# pytest-xdist-load-testing

[![PyPI version](https://img.shields.io/pypi/v/pytest-xdist-load-testing.svg)](https://pypi.org/project/pytest-xdist-load-testing)
[![Python versions](https://img.shields.io/pypi/pyversions/pytest-xdist-load-testing.svg)](https://pypi.org/project/pytest-xdist-load-testing)
[![Build Status](https://github.com/xverges/pytest-xdist-load-testing/actions/workflows/main.yml/badge.svg)](https://github.com/xverges/pytest-xdist-load-testing/actions/workflows/main.yml)

A pytest-xdist scheduler for continuous load testing with weighted test selection

## Features

* **Continuous Test Execution**: Runs tests repeatedly until manually interrupted
* **Weighted Test Selection**: Control test execution frequency using the `@weight` decorator
* **Random Selection**: Tests are selected randomly based on their weights using `random.choices`
* **Graceful Interruption**: Tests and fixtures can stop the scheduler programmatically
* **pytest-xdist Integration**: Seamlessly integrates with pytest-xdist's distributed testing

## Requirements

* Python 3.9+
* pytest >= 8.4.2
* pytest-xdist >= 3.8.0

## Installation

```bash
pip install pytest-xdist-load-testing
```

## Examples

See the [`examples/`](https://github.com/xverges/pytest-xdist-load-testing/tree/main/examples)
folder for working examples.

### Basic Load Testing

Run your tests with pytest-xdist to enable the load testing scheduler:

```bash
pytest --load-test -n 4 path/to/test_module.py  # Run with 4 workers
```

The scheduler will continuously supply tests to workers until interrupted (Ctrl+C).

**Important**: Load testing requires specifying a single test module. Running multiple modules will result in an error:

```bash
pytest --load-test -n 4 tests/  # ERROR: Multiple modules detected
pytest --load-test -n 4 test_a.py test_b.py  # ERROR: Multiple modules
```

This restriction ensures proper fixture handling and test isolation during continuous execution.

### Stopping the Scheduler

Tests can stop the scheduler programmatically using the `stop_load_testing` function:

```python
from pytest_xdist_load_testing import stop_load_testing

def test_with_stop_condition():
    result = check_system_health()
    if result.critical_failure:
        stop_load_testing("Critical failure detected")
```

### Load Test with Weighted Distribution

```python
from pytest_xdist_load_testing import weight

@weight(70)
def test_read_heavy():
    """70% of requests"""
    assert api.get("/data").status_code == 200

@weight(20)
def test_write_operations():
    """20% of requests"""
    assert api.post("/data", json={}).status_code == 201

@weight(10)
def test_admin_operations():
    """10% of requests"""
    assert api.delete("/data/old").status_code == 204
```

## Command Line Options

The plugin adds the following command line option:

```text
--load-test    Enable load testing mode with continuous test execution
```

## Rate Limiting and Shared State

For rate limiting and shared state management across pytest-xdist workers,
see the companion package [pytest-xdist-rate-limit](https://github.com/xverges/pytest-xdist-rate-limit).

## Documentation

📚 **[Full Documentation](https://xverges.github.io/pytest-xdist-load-testing/)**

* [API Reference](https://xverges.github.io/pytest-xdist-load-testing/api/reference/) - Complete API documentation

## License

Distributed under the terms of the [MIT](https://opensource.org/licenses/MIT) license, "pytest-xdist-load-testing" is free and open source software

## Issues

If you encounter any problems, please [file an issue](https://github.com/xverges/pytest-xdist-load-testing/issues) along with a detailed description.

---

This [pytest](https://github.com/pytest-dev/pytest) plugin was generated with [Cookiecutter](https://github.com/audreyr/cookiecutter) along with [@hackebrot](https://github.com/hackebrot)'s [cookiecutter-pytest-plugin](https://github.com/pytest-dev/cookiecutter-pytest-plugin) template.
