Skip to content
Snippets Groups Projects

Make one ctest per one py file with pytests

Closed Michal Mazurek requested to merge mimazure/Gaudi:mimazure-pytests-one-per-file into master
2 unresolved threads
1 file
+ 9
4
Compare changes
  • Side-by-side
  • Inline
+ 9
4
@@ -726,10 +726,15 @@ function(gaudi_add_tests type)
if(NOT test_directory)
set(test_directory "${CMAKE_CURRENT_SOURCE_DIR}/tests/pytest")
endif()
get_filename_component(name "${test_directory}" NAME)
add_test(NAME ${package_name}.${name}
COMMAND run $<TARGET_FILE:pytest> -v --doctest-modules ${test_directory})
set_tests_properties(${package_name}.${name} PROPERTIES LABELS "${PROJECT_NAME};${package_name}")
file(GLOB_RECURSE pytest_files RELATIVE "${test_directory}" "${test_directory}/*.py") # ordered lexicographically
    • pytest is a bit more clever than that: it only takes tests from files called test_*.py or *_test.py (see the doc).

      With this approach you may try to run non-test files as test (not really a problem, but a waste of time and a bit confusing).

      It might be better to use pytest --collect-only and parse the output.

Please register or sign in to reply
string(TOLOWER "${package_name}" subdir_name_lower)
foreach(pytest_file IN LISTS pytest_files)
string(REPLACE ".py" "" pytest_name "${pytest_file}")
string(REGEX REPLACE "^${subdir_name_lower}\\." "" pytest_name "${pytest_name}")
add_test(NAME ${package_name}.${pytest_name}
COMMAND run $<TARGET_FILE:pytest> -v ${test_directory}/${pytest_file})
set_tests_properties(${package_name}.${pytest_name} PROPERTIES LABELS "${PROJECT_NAME};${package_name}")
endforeach()
else()
message(FATAL_ERROR "${type} is not a valid test framework.")
endif()
Loading