{"id":6200,"library":"pytest-cpp","title":"pytest-cpp","description":"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'.","status":"active","version":"2.6.0","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-cpp","tags":["pytest","cpp","testing","plugin","google-test","boost-test","catch2"],"install":[{"cmd":"pip install pytest-cpp","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"pytest-cpp is a plugin for the pytest testing framework and requires pytest to function.","package":"pytest"}],"imports":[{"note":"pytest-cpp operates as a plugin that pytest discovers and loads automatically. User-facing interactions are primarily through pytest's command-line interface and pytest.ini configuration, rather than direct Python imports from the `pytest_cpp` package for its core test discovery and execution features.","symbol":"No direct imports for core functionality","correct":"pytest automatically discovers and loads the plugin once installed."}],"quickstart":{"code":"/* C++ test file: my_cpp_tests.cpp (using Google Test) */\n#include <gtest/gtest.h>\n\nTEST(ExampleTest, Succeeds) {\n    ASSERT_EQ(1, 1);\n}\n\nTEST(ExampleTest, Fails) {\n    ASSERT_EQ(1, 2); // This test will fail\n}\n\nint main(int argc, char **argv) {\n    ::testing::InitGoogleTest(&argc, argv);\n    return RUN_ALL_TESTS();\n}\n\n# Compile the C++ test (example for Linux/macOS with g++ and Google Test)\n# g++ my_cpp_tests.cpp -o test_app -I/usr/local/include -L/usr/local/lib -lgtest -lgtest_main -pthread\n# (Adjust include/lib paths as needed for your system/build setup)\n\n# To run with pytest-cpp:\n# 1. Ensure pytest-cpp is installed: pip install pytest-cpp\n# 2. Navigate to the directory containing 'test_app' executable.\n# 3. Run pytest:\n# pytest\n\n# Example pytest.ini configuration to customize C++ file discovery:\n# [pytest]\n# cpp_files = my_cpp_test_exec*\n#   cpp_arguments = -v\n\n# Expected output for the above example (truncated):\n# ============================= test session starts =============================\n# ...\n# collected 2 items\n# \n# test_app::ExampleTest.Succeeds PASSED                                  [ 50%]\n# test_app::ExampleTest.Fails FAILED                                   [100%]\n# \n# =================================== FAILURES ==================================\n# ____________________________ ExampleTest.Fails _____________________________\n# \n#     ASSERT_EQ(1, 2), failed at my_cpp_tests.cpp:10\n#     Expected: 1\n#     Actual: 2\n# \n# =========================== short test summary info ===========================\n# FAILED test_app::ExampleTest.Fails - ASSERT_EQ(1, 2), failed at my_cpp_tests.cpp:10\n# ========================= 1 failed, 1 passed in ...s =========================\n","lang":"bash","description":"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."},"warnings":[{"fix":"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.","message":"Do not pass C++ test framework-specific filtering arguments (e.g., `--gtest_filter='*MyTest*'` or `--catch_test_filter '[tag]'`) directly via the `cpp_arguments` configuration option or command line. This can conflict with `pytest-cpp`'s native test filtering and lead to incorrect or unexpected behavior.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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*`.","message":"C++ test executables might not be discovered if their filenames do not match the default `pytest-cpp` discovery patterns (`test_*` or `*_test`).","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"C++ test executables must be properly compiled and dynamically linked with their respective test frameworks (Google Test, Boost.Test, Catch2) and any other necessary libraries (e.g., `pthread`). If they cannot run standalone, `pytest-cpp` will report execution failures.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}