pytest-custom-exit-code
pytest-custom-exit-code is a pytest plugin that allows users to customize the exit code of a pytest test session in specific scenarios. Currently at version 0.3.0, its primary function is to alter the default exit codes, such as when no tests are collected or when tests fail. The library has had a slow release cadence, with the last update in August 2019.
Warnings
- breaking This plugin (version 0.3.0) was last released in August 2019 and explicitly declared compatibility up to Python 3.7. It is highly likely to be incompatible with newer Python versions (3.8+) and recent pytest versions (8.x, 9.x) due to breaking changes in both Python and pytest APIs over time. For example, pytest 9.0.0 dropped support for Python 3.8.
- gotcha The plugin's functionality is activated via pytest command-line arguments (`--suppress-no-test-exit-code`, `--suppress-tests-failed-exit-code`) or through the `pytest.ini` configuration file. It does not expose a direct Python API for programmatic use. Users expecting to import and interact with the plugin's logic directly from Python code will find no such interface.
- gotcha The plugin modifies pytest's default exit codes for specific scenarios (e.g., no tests collected or tests failed). Relying on these modified exit codes in CI/CD pipelines or scripts without explicit awareness can lead to misinterpretation of test results, as a 'successful' exit code (0) might mask actual test failures or absence of tests.
Install
-
pip install pytest-custom-exit-code
Quickstart
# To install the plugin pip install pytest-custom-exit-code # --- Example: pytest.ini configuration to suppress 'no tests collected' exit code --- # Create a pytest.ini file in your project root with the following content: # [pytest] # addopts = --suppress-no-test-exit-code # To demonstrate, create an empty test file (e.g., test_nothing.py) # Then run pytest: # pytest test_nothing.py # This would normally exit with code 5 (no tests collected). With the plugin and config, # it will exit with code 0. If you delete the pytest.ini or remove the addopt, it will revert. # --- Example: Using command-line options directly --- # Suppress exit code when no tests are collected: # pytest --suppress-no-test-exit-code # Suppress exit code when tests fail: # pytest --suppress-tests-failed-exit-code