pytest-cpp is a pytest plugin that allows the pytest runner to discover and execute C++ tests. It supports popular frameworks like Google Test, Boost.Test, and Catch2. This integration enables running tests from multi-language projects with a single command, facilitates parallel execution with plugins like pytest-xdist, and provides uniform JUnit XML output. The current version is 2.6.0, with releases typically occurring 1-3 times per year, categorizing its release cadence as 'Slow'.
pip install pytest-cppVerified import paths — ran on the pinned version, not inferred.
After installing `pytest-cpp`, create a C++ test file (e.g., `my_cpp_tests.cpp`) using a supported framework like Google Test. Compile it into an executable (e.g., `test_app`). By default, `pytest-cpp` discovers executables matching `test_*` or `*_test` patterns. Simply run `pytest` in the directory containing your compiled C++ test executable, and `pytest-cpp` will integrate and execute these tests alongside any Python tests.
To filter C++ tests, use standard pytest filtering facilities like the `-k` (keyword) option, e.g., `pytest -k 'MyTest.SpecificCase'` or `pytest -k 'tag_name'`. pytest-cpp automatically maps these to the underlying C++ test framework's filtering mechanisms.
Ensure your C++ test executables are named following `test_*.exe` or `*_test.exe` conventions. Alternatively, configure the `cpp_files` option in your `pytest.ini` file to specify custom patterns, e.g., `[pytest] cpp_files = my_app_tests*`.
Before running `pytest`, ensure your C++ test executable can be successfully executed directly from the command line, verifying all dependencies are met. Compile with appropriate flags (e.g., `-lgtest`, `-lgtest_main`, `-pthread` for Google Test) and ensure library paths are correctly configured for your environment.