python-cmake / pytest-cmake

Pytest module for CMake
https://python-cmake.github.io/pytest-cmake/
MIT License
25 stars 0 forks source link

Enable usage for partial build #31

Closed MarcelKoller closed 1 month ago

MarcelKoller commented 2 months ago

In a big software project you not always want to build and test the whole project. You sometimes only want to build specific subprojects and execute their tests. With the current implementation of pytest_discover_tests this does not seem to be possible - at least to my understanding. When running ctest (even with a regex that only matches the tests of one subproject) all CTestTestfile.cmake files in the project tree are checked and if at least one of the ${_tests_file}s (of other subprojects) was not built this will result in an inclusion error. One possible solution that I can think of would be to create the (empty) ${_tests_file} within pytest_discover_tests if it does not exist yet. What do you think?

MarcelKoller commented 2 months ago

BTW: gtest seems to solve this issue like that:

file(WRITE "${ctest_include_file}"
      "if(EXISTS \"${ctest_tests_file}\")\n"
      "  include(\"${ctest_tests_file}\")\n"
      "else()\n"
      "  add_test(${TARGET}_NOT_BUILT ${TARGET}_NOT_BUILT)\n"
      "endif()\n"
)
MarcelKoller commented 1 month ago

Any thoughts on this @buddly27?

buddly27 commented 1 month ago

@MarcelKoller, apologize for the very late reply!

This is a valid point. Many C++ libraries that provide Python bindings require partial builds to ship products without the bindings, so we don't want Pytest to run all the time.

When running ctest (even with a regex that only matches the tests of one subproject) all CTestTestfile.cmake files in the project tree are checked and if at least one of the ${_tests_file}s (of other subprojects) was not built this will result in an inclusion error.

Right, I was able to replicate the issue. I mostly create partial builds via a custom CMake conditional definitions, so I didn't consider checking what the '--target' option would do. Unlike 'gtest', the 'pytest_discover_tests' command is not set up to run "POST_BUILD" after the corresponding CMake target containing the tests has been built, so I didn't think that using an intermediate "include" script would be necessary. But it seems to fix this issue indeed.

I'll push a fix shortly!

buddly27 commented 1 month ago

This should have been resolved in #34 and is included in version 0.8.0.

Thanks for pointing out this limitation!