diff --git a/Modules/private/CreateCoverageTargets.cmake b/Modules/private/CreateCoverageTargets.cmake index 2e3da8934..8ed970e7f 100644 --- a/Modules/private/CreateCoverageTargets.cmake +++ b/Modules/private/CreateCoverageTargets.cmake @@ -552,39 +552,8 @@ function(_create_coverage_targets_impl) COMMENT "Cleaning coverage data files (C++ and Python)" ) - # Add Python coverage target if pytest-cov is available - if(Python_FOUND) - execute_process( - COMMAND ${Python_EXECUTABLE} -c "import pytest_cov" - RESULT_VARIABLE PYTEST_COV_CHECK - OUTPUT_QUIET - ERROR_QUIET - ) - if(PYTEST_COV_CHECK EQUAL 0) - add_custom_target( - coverage-python - COMMAND - ${CMAKE_COMMAND} -E echo - "[Coverage] Generating Python coverage report using pytest-cov..." - COMMAND - ${CMAKE_COMMAND} -E env PYTHONPATH=${PROJECT_SOURCE_DIR}/test/python - PHLEX_INSTALL=${PROJECT_SOURCE_DIR} ${Python_EXECUTABLE} -m pytest - ${PROJECT_SOURCE_DIR}/test/python/test_phlex.py --cov=${PROJECT_SOURCE_DIR}/test/python - --cov-report=term-missing --cov-report=xml:${CMAKE_BINARY_DIR}/coverage-python.xml - --cov-report=html:${CMAKE_BINARY_DIR}/coverage-python-html - --cov-config=${PROJECT_SOURCE_DIR}/test/python/.coveragerc - WORKING_DIRECTORY ${CMAKE_BINARY_DIR} - COMMENT "Generating Python coverage report" - VERBATIM - ) - message(STATUS "Added 'coverage-python' target for Python test coverage (pytest-cov)") - else() - message( - STATUS - "pytest-cov not found; Python coverage target not available. Install with: pip install pytest-cov" - ) - endif() - endif() + # Note: The coverage-python target is defined in test/python/CMakeLists.txt + # where it can use the proper test environment setup (PYTHONPATH, etc.) message( STATUS diff --git a/test/python/.coveragerc b/test/python/.coveragerc index e0c6793ab..7eedeba1d 100644 --- a/test/python/.coveragerc +++ b/test/python/.coveragerc @@ -1,12 +1,23 @@ [run] # Include non-test Python modules for coverage measurement -source = . +source = + ../../plugins/python/python omit = test_*.py **/test_*.py + unit_test_*.py + **/unit_test_*.py + # Omit all files in test directories + */test/* + */tests/* [report] # Exclude test files from coverage reports omit = test_*.py **/test_*.py + unit_test_*.py + **/unit_test_*.py + # Omit all files in test directories + */test/* + */tests/* diff --git a/test/python/CMakeLists.txt b/test/python/CMakeLists.txt index 7e97c8167..2ce3629ad 100644 --- a/test/python/CMakeLists.txt +++ b/test/python/CMakeLists.txt @@ -78,6 +78,7 @@ if(HAS_CPPYY) -m pytest --cov=${CMAKE_CURRENT_SOURCE_DIR} + --cov=${PROJECT_SOURCE_DIR}/plugins/python/python --cov-report=term-missing --cov-report=xml:${CMAKE_BINARY_DIR}/coverage-python.xml --cov-report=html:${CMAKE_BINARY_DIR}/coverage-python-html @@ -96,15 +97,6 @@ if(HAS_CPPYY) add_test(NAME py:phlex COMMAND ${PYTEST_COMMAND} WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}) set_property(TEST py:phlex PROPERTY ENVIRONMENT "PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL}") - - if(HAS_PYTEST_COV) - add_custom_target( - coverage-python - COMMAND ${PYTEST_COMMAND} - WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} - COMMENT "Running Python coverage report" - ) - endif() endif() set(ACTIVE_PY_CPHLEX_TESTS "") @@ -261,3 +253,34 @@ add_test( COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/unit_test_variant.py ) set_tests_properties(py:unit_variant PROPERTIES ENVIRONMENT "${PYTHON_TEST_ENVIRONMENT}") + +# Python coverage target +if(HAS_PYTEST_COV AND ENABLE_COVERAGE) + if(HAS_CPPYY) + # When cppyy is available, use the full pytest command + add_custom_target( + coverage-python + COMMAND + ${CMAKE_COMMAND} -E env PYTHONPATH=${TEST_PYTHONPATH} + PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL} ${PYTEST_COMMAND} + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Running Python coverage report" + ) + else() + # When cppyy is not available, run a simpler pytest command + # for standalone Python tests (like unit_test_variant.py) + set(PYTHON_TEST_PHLEX_INSTALL_FALLBACK ${CMAKE_SOURCE_DIR}) + add_custom_target( + coverage-python + COMMAND + ${CMAKE_COMMAND} -E env PYTHONPATH=${TEST_PYTHONPATH} + PHLEX_INSTALL=${PYTHON_TEST_PHLEX_INSTALL_FALLBACK} ${Python_EXECUTABLE} -m pytest + --cov=${CMAKE_CURRENT_SOURCE_DIR} --cov=${PROJECT_SOURCE_DIR}/plugins/python/python + --cov-report=term-missing --cov-report=xml:${CMAKE_BINARY_DIR}/coverage-python.xml + --cov-report=html:${CMAKE_BINARY_DIR}/coverage-python-html + --cov-config=${CMAKE_CURRENT_SOURCE_DIR}/.coveragerc unit_test_variant.py + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + COMMENT "Running Python coverage report (standalone tests only)" + ) + endif() +endif()