3

I have some test added by command add_test:

find_program(PYTEST "pytest")
add_test(NAME test_something COMMAND ${PYTEST})

But before this test I need to copy some test files (including python test scripts to be run). For this purpose there is a custom target generate_init_queries. Since add_test doesn't create a target I can't use add_dependencies to link my custom target generate_init_queries and this test. I supposed that there should exist a test target in CMake and added the command:

add_dependencies(test generate_init_queries)

But it resulted in error annot add target-level dependencies to non-existent target "test". How can I copy files before running the test in make test?

user2717575
  • 369
  • 7
  • 16

2 Answers2

4

This question is highly related to CMake & CTest : make test doesn't build tests which has gotten a lot more attention. The best answer nowadays (the question is from 2009) is IMO this one. I.e. using the fixtures feature in cmake available from version 3.7 onwards.

See the FIXTURES_REQUIRED docs for a good overview and usage example. I can also recommend the test-properties documentation page for a broader overview on what properties can be used to influence ctest test execution.

So modified for your use case, a possible solution would roughly look like this:

add_test(gen_init_queries
  "${CMAKE_COMMAND}"
  --build "${CMAKE_BINARY_DIR}"
  --config "$<CONFIG>"
  --target generate_init_queries
)
set_tests_properties(gen_init_queries PROPERTIES FIXTURES_SETUP f_init_queries)
set_tests_properties(test PROPERTIES  FIXTURES_REQUIRED f_init_queries)
suluke
  • 683
  • 5
  • 14
-2

Did it through add_custom_target with ALL keyword:

find_program(PYTEST "pytest")
add_test(test_something ${PYTEST} test_something.py)
add_custom_target(test_something_py_copy ALL COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/test_something.py ${TEST_DATA_DIR}/test_something.py DEPENDS generate_init_queries)
user2717575
  • 369
  • 7
  • 16
  • 2
    You still need to run `make` before `make test`. Not sure what is your achievement with *ALL*. It looks like you had some **other problem** then one you describing in the question post. – Tsyvarev Dec 29 '17 at 10:22