Pytest Integration
pytest-integration is a plugin for pytest that allows users to organize tests into 'unit', 'integration', and 'slow integration' categories. It ensures that integration tests run only after unit tests, and slow integration tests run after quick integration tests, stopping execution of later stages if earlier ones fail. When `pytest-cov` is installed, it automatically disables code coverage for integration tests, assuming unit tests handle comprehensive coverage. The current version is 0.2.3, last released on November 17, 2022, suggesting a low-activity or stable maintenance phase.
Common errors
-
No tests were collected or integration tests are not running.
cause Tests might not be correctly marked with `@pytest.mark.integration_test` or `@pytest.mark.slow_integration_test`, or the corresponding command-line options (`--with-integration`, `--with-slow-integration`) are missing.fixEnsure relevant tests have the correct markers. Run pytest with `pytest --with-integration` to execute quick integration tests, and `pytest --with-slow-integration` for slow integration tests. By default, only unit tests (without these markers) run. -
Code coverage report is missing data from integration tests even though they ran successfully.
cause The `pytest-integration` plugin, by default, disables coverage collection for tests marked as `integration_test` or `slow_integration_test` when `pytest-cov` is active.fixTo include coverage from integration tests, add the `--integration-cover` flag to your pytest command: `pytest --with-integration --integration-cover`.
Warnings
- gotcha When using `pytest-xdist`, each node will collect and run tests independently based on its index. This can lead to situations where integration tests might still run on a node even if a unit test failed on another node, potentially masking cross-node dependencies.
- gotcha If `pytest-cov` is installed, `pytest-integration` automatically disables code coverage collection for all integration and slow integration tests by default. This is based on the assumption that unit tests should provide full code coverage.
- deprecated Direct integration with `setuptools` (e.g., using `python setup.py test` or `pytest-runner`) is not recommended by the core pytest project and may stop working in the future. This applies to any pytest plugin, including `pytest-integration`.
Install
-
pip install pytest-integration
Imports
- @pytest.mark.integration_test
import pytest @pytest.mark.integration_test def test_something_integrated(): ... - @pytest.mark.slow_integration_test
import pytest @pytest.mark.slow_integration_test def test_something_slow_integrated(): ...
Quickstart
import pytest
def test_unit_example():
assert 1 + 1 == 2
@pytest.mark.integration_test
def test_integration_example():
# Simulate an integration call
assert 'integration' in 'this is an integration test'
@pytest.mark.slow_integration_test
def test_slow_integration_example():
# Simulate a slow integration call
assert True
# To run: pytest --with-integration --with-slow-integration