You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
4.0 KiB

  1. if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  2. message(STATUS "Setting tests build type to MinSizeRel as none was specified")
  3. set(CMAKE_BUILD_TYPE MinSizeRel CACHE STRING "Choose the type of build." FORCE)
  4. set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release"
  5. "MinSizeRel" "RelWithDebInfo")
  6. endif()
  7. # Full set of test files (you can override these; see below)
  8. set(PYBIND11_TEST_FILES
  9. test_alias_initialization.cpp
  10. test_buffers.cpp
  11. test_callbacks.cpp
  12. test_chrono.cpp
  13. test_class_args.cpp
  14. test_constants_and_functions.cpp
  15. test_copy_move_policies.cpp
  16. test_docstring_options.cpp
  17. test_eigen.cpp
  18. test_enum.cpp
  19. test_eval.cpp
  20. test_exceptions.cpp
  21. test_inheritance.cpp
  22. test_issues.cpp
  23. test_keep_alive.cpp
  24. test_kwargs_and_defaults.cpp
  25. test_methods_and_attributes.cpp
  26. test_modules.cpp
  27. test_multiple_inheritance.cpp
  28. test_numpy_array.cpp
  29. test_numpy_dtypes.cpp
  30. test_numpy_vectorize.cpp
  31. test_opaque_types.cpp
  32. test_operator_overloading.cpp
  33. test_pickling.cpp
  34. test_python_types.cpp
  35. test_sequences_and_iterators.cpp
  36. test_smart_ptr.cpp
  37. test_stl_binders.cpp
  38. test_virtual_functions.cpp
  39. )
  40. # Invoking cmake with something like:
  41. # cmake -DPYBIND11_TEST_OVERRIDE="test_issues.cpp;test_picking.cpp" ..
  42. # lets you override the tests that get compiled and run. You can restore to all tests with:
  43. # cmake -DPYBIND11_TEST_OVERRIDE= ..
  44. if (PYBIND11_TEST_OVERRIDE)
  45. set(PYBIND11_TEST_FILES ${PYBIND11_TEST_OVERRIDE})
  46. endif()
  47. string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
  48. # Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
  49. # keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
  50. # skip message).
  51. list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
  52. if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
  53. find_package(Eigen3 QUIET)
  54. if(EIGEN3_FOUND)
  55. message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
  56. else()
  57. list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
  58. message(STATUS "Building tests WITHOUT Eigen")
  59. endif()
  60. endif()
  61. # Create the binding library
  62. pybind11_add_module(pybind11_tests pybind11_tests.cpp
  63. ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
  64. pybind11_enable_warnings(pybind11_tests)
  65. if(EIGEN3_FOUND)
  66. target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
  67. target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
  68. endif()
  69. set(testdir ${PROJECT_SOURCE_DIR}/tests)
  70. # Always write the output file directly into the 'tests' directory (even on MSVC)
  71. if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
  72. set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
  73. foreach(config ${CMAKE_CONFIGURATION_TYPES})
  74. string(TOUPPER ${config} config)
  75. set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
  76. endforeach()
  77. endif()
  78. # Make sure pytest is found or produce a fatal error
  79. if(NOT PYBIND11_PYTEST_FOUND)
  80. execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pytest --version --noconftest OUTPUT_QUIET ERROR_QUIET
  81. RESULT_VARIABLE PYBIND11_EXEC_PYTHON_ERR)
  82. if(PYBIND11_EXEC_PYTHON_ERR)
  83. message(FATAL_ERROR "Running the tests requires pytest. Please install it manually (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
  84. endif()
  85. set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
  86. endif()
  87. # A single command to compile and run the tests
  88. add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES}
  89. DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})
  90. if(PYBIND11_TEST_OVERRIDE)
  91. add_custom_command(TARGET pytest POST_BUILD
  92. COMMAND ${CMAKE_COMMAND} -E echo "Note: not all tests run: -DPYBIND11_TEST_OVERRIDE is in effect")
  93. endif()
  94. # And another to show the .so size and, if a previous size, compare it:
  95. add_custom_command(TARGET pybind11_tests POST_BUILD
  96. COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_SOURCE_DIR}/tools/libsize.py
  97. $<TARGET_FILE:pybind11_tests> ${CMAKE_CURRENT_BINARY_DIR}/sosize-$<TARGET_FILE_NAME:pybind11_tests>.txt)