pytest-hot-reloading
pytest-hot-reloading is a pytest plugin that enables hot-reloading of tests during development. It watches for changes in your project files and automatically re-runs affected tests, providing immediate feedback without restarting the pytest session. It is currently in an alpha stage, meaning its API and behavior may change. Releases are irregular due to its early development phase.
Common errors
-
ModuleNotFoundError: No module named 'pytest_hot_reloading'
cause The `pytest-hot-reloading` package was not installed or installed in a different Python environment than the one running pytest.fixEnsure the plugin is installed in your active environment: `pip install pytest-hot-reloading`. -
pytest is running but tests are not hot-reloading after file changes.
cause This usually indicates a pytest version incompatibility, a conflicting plugin, or an issue with the underlying file watcher.fixFirst, verify your `pytest` version is compatible (see warnings). Check for other plugins that might interfere: `pytest --trace-config` or try disabling others with `pytest -p no:another-plugin`. Ensure your file system isn't causing issues (e.g., WSL, network drives might require specific configurations). -
RuntimeError: The watchfiles library is not installed. Please install it with 'pip install watchfiles'.
cause `watchfiles` is a required dependency that might not have been installed automatically (e.g., due to an older `pip` version or isolated build environment issues).fixManually install the missing dependency: `pip install watchfiles`. Also, ensure your `pip` is up to date (`pip install --upgrade pip`).
Warnings
- gotcha This library is in an alpha stage (e.g., `0.1.0a19`). Expect potential API changes, instability, and incomplete documentation.
- gotcha Hot-reloading can interfere with certain pytest fixtures or other pytest plugins that rely on a clean session state or complex setup/teardown processes, leading to unexpected test failures or behavior.
- breaking This version of `pytest-hot-reloading` (0.1.0a19) requires `pytest` versions between `7.0` (inclusive) and `9.0` (exclusive). It is incompatible with `pytest 9.0` and newer.
- gotcha File watching performance might be impacted in very large repositories, repositories with many rapidly changing files, or on certain network file systems.
Install
-
pip install pytest-hot-reloading
Quickstart
# 1. Install the plugin # pip install pytest-hot-reloading # 2. Create a test file (e.g., test_example.py) # with the content: # def test_initial_check(): # assert True # 3. Run pytest normally from your terminal # pytest # 4. While pytest is running, modify test_example.py # For instance, change 'assert True' to 'assert False' and save. # The tests should automatically re-run and show the new result. # Change it back to 'assert True' and save to see it pass again.