pytest-cpp

2.6.0 · active · verified Tue Apr 14

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'.

Warnings

Install

Imports

Quickstart

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.

/* C++ test file: my_cpp_tests.cpp (using Google Test) */
#include <gtest/gtest.h>

TEST(ExampleTest, Succeeds) {
    ASSERT_EQ(1, 1);
}

TEST(ExampleTest, Fails) {
    ASSERT_EQ(1, 2); // This test will fail
}

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

# Compile the C++ test (example for Linux/macOS with g++ and Google Test)
# g++ my_cpp_tests.cpp -o test_app -I/usr/local/include -L/usr/local/lib -lgtest -lgtest_main -pthread
# (Adjust include/lib paths as needed for your system/build setup)

# To run with pytest-cpp:
# 1. Ensure pytest-cpp is installed: pip install pytest-cpp
# 2. Navigate to the directory containing 'test_app' executable.
# 3. Run pytest:
# pytest

# Example pytest.ini configuration to customize C++ file discovery:
# [pytest]
# cpp_files = my_cpp_test_exec*
#   cpp_arguments = -v

# Expected output for the above example (truncated):
# ============================= test session starts =============================
# ...
# collected 2 items
# 
# test_app::ExampleTest.Succeeds PASSED                                  [ 50%]
# test_app::ExampleTest.Fails FAILED                                   [100%]
# 
# =================================== FAILURES ==================================
# ____________________________ ExampleTest.Fails _____________________________
# 
#     ASSERT_EQ(1, 2), failed at my_cpp_tests.cpp:10
#     Expected: 1
#     Actual: 2
# 
# =========================== short test summary info ===========================
# FAILED test_app::ExampleTest.Fails - ASSERT_EQ(1, 2), failed at my_cpp_tests.cpp:10
# ========================= 1 failed, 1 passed in ...s =========================

view raw JSON →