Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions Modules/private/CreateCoverageTargets.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion test/python/.coveragerc
Original file line number Diff line number Diff line change
@@ -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/*
41 changes: 32 additions & 9 deletions test/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 "")
Expand Down Expand Up @@ -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()
Loading