Skip to content

Commit 683d09d

Browse files
authored
Merge pull request intel#105 from elbeno/add-pytest-deps
✨ Allow extra dependencies to be specified for python tests
2 parents a93d4a4 + 0f5c29a commit 683d09d

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

cmake/coverage.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ else()
5050
)
5151
endif()
5252

53-
function(add_coverage_target name)
53+
function(add_test_coverage_target name)
5454
message(
5555
STATUS
56-
"add_coverage_target(${name}) is disabled because CMAKE_CXX_COMPILER_ID is ${CMAKE_CXX_COMPILER_ID}."
56+
"add_test_coverage_target(${name}) is disabled because CMAKE_CXX_COMPILER_ID is ${CMAKE_CXX_COMPILER_ID}."
5757
)
5858
endfunction()
5959

cmake/test.cmake

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ endfunction()
229229

230230
function(add_python_test_target name)
231231
set(multiValueArgs FILES INCLUDE_FILES INCLUDE_DIRECTORIES LIBRARIES
232-
EXTRA_ARGS)
232+
EXTRA_ARGS EXTRA_DEPS)
233233
cmake_parse_arguments(UNIT "" "" "${multiValueArgs}" ${ARGN})
234234
list(TRANSFORM UNIT_FILES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
235235
list(TRANSFORM UNIT_INCLUDE_FILES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/")
@@ -253,13 +253,21 @@ function(add_python_test_target name)
253253
string(PREPEND name "PYTHON.")
254254
add_test(NAME ${name} COMMAND ${target_test_command} COMMAND_EXPAND_LISTS)
255255

256+
foreach(file ${UNIT_FILES})
257+
get_filename_component(this_dir ${file} DIRECTORY)
258+
if(EXISTS "${this_dir}/conftest.py")
259+
list(APPEND auto_dependencies "${this_dir}/conftest.py")
260+
endif()
261+
endforeach()
262+
256263
add_custom_target(all_${name} ALL DEPENDS run_${name})
257264
add_custom_target(run_${name} DEPENDS ${name}.passed)
258265
add_custom_command(
259266
OUTPUT ${name}.passed
260267
COMMAND ${target_test_command}
261268
COMMAND ${CMAKE_COMMAND} "-E" "touch" "${name}.passed"
262-
DEPENDS ${UNIT_FILES} ${UNIT_LIBRARIES}
269+
DEPENDS ${UNIT_FILES} ${UNIT_LIBRARIES} ${UNIT_EXTRA_DEPS}
270+
${auto_dependencies}
263271
COMMAND_EXPAND_LISTS)
264272

265273
add_dependencies(python_tests "run_${name}")

docs/testing.adoc

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ add_unit_test(
5252
my_test
5353
PYTEST
5454
FILES my_test.py
55-
LIBRARIES my_lib)
55+
LIBRARIES my_lib
56+
EXTRA_DEPS my_header.hpp)
5657
----
5758

5859
`add_unit_test` also supports running tests using https://pytest.org[pytest]. In
@@ -62,6 +63,11 @@ handled with pytest's
6263
https://docs.pytest.org/en/8.0.x/reference/reference.html#pytest.hookspec.pytest_addoption[`addoption`
6364
hook].
6465

66+
NOTE: The `EXTRA_DEPS` argument allows python tests to specify extra
67+
dependencies that are not part of the CMake dependency tree. A `conftest.py`
68+
file in the same directory as any of the `FILE`​s is automatically discovered as
69+
such a dependency.
70+
6571
=== Fuzz tests
6672

6773
[source,cmake]

0 commit comments

Comments
 (0)