blob: 684535a40bde170f78cdc58b1d6324c49eeea34b [file] [log] [blame]
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02001if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
2 message(STATUS "Setting tests build type to MinSizeRel as none was specified")
Wenzel Jakob7962f302016-09-17 12:58:18 +02003 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")
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02006endif()
7
8set(PYBIND11_TEST_FILES
Jason Rhinelanderec62d972016-09-09 02:42:51 -04009 test_alias_initialization.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020010 test_buffers.cpp
11 test_callbacks.cpp
Trent Houliston352149e2016-08-25 23:08:04 +100012 test_chrono.cpp
Jason Rhinelander5fffe202016-09-06 12:17:06 -040013 test_class_args.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020014 test_constants_and_functions.cpp
Jason Rhinelander52f4be82016-09-03 14:54:22 -040015 test_eigen.cpp
Dean Moldovana9a37b42016-08-13 00:57:24 +020016 test_enum.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020017 test_eval.cpp
18 test_exceptions.cpp
19 test_inheritance.cpp
20 test_issues.cpp
21 test_keep_alive.cpp
22 test_kwargs_and_defaults.cpp
23 test_methods_and_attributes.cpp
24 test_modules.cpp
Dean Moldovan568ec6b2016-09-20 11:52:25 +020025 test_multiple_inheritance.cpp
Ivan Smirnov91b3d682016-08-29 02:41:05 +010026 test_numpy_array.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020027 test_numpy_dtypes.cpp
28 test_numpy_vectorize.cpp
29 test_opaque_types.cpp
30 test_operator_overloading.cpp
31 test_pickling.cpp
32 test_python_types.cpp
33 test_sequences_and_iterators.cpp
Dean Moldovan568ec6b2016-09-20 11:52:25 +020034 test_smart_ptr.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020035 test_stl_binders.cpp
36 test_virtual_functions.cpp
37)
38
Jason Rhinelander52f4be82016-09-03 14:54:22 -040039string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020040
Jason Rhinelander52f4be82016-09-03 14:54:22 -040041# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
42# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
43# skip message).
44list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
45if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
46 find_package(Eigen3 QUIET)
47
48 if(EIGEN3_FOUND)
49 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
50 else()
51 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
52 message(STATUS "Building tests WITHOUT Eigen")
53 endif()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020054endif()
55
56# Create the binding library
Wenzel Jakobdac38582016-09-29 21:30:00 +020057pybind11_add_module(pybind11_tests pybind11_tests.cpp
58 ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
59
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020060pybind11_enable_warnings(pybind11_tests)
61
62if(EIGEN3_FOUND)
63 target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
64 target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
65endif()
66
67set(testdir ${PROJECT_SOURCE_DIR}/tests)
68
69# Always write the output file directly into the 'tests' directory (even on MSVC)
70if(NOT CMAKE_LIBRARY_OUTPUT_DIRECTORY)
71 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${testdir})
72 foreach(config ${CMAKE_CONFIGURATION_TYPES})
73 string(TOUPPER ${config} config)
74 set_target_properties(pybind11_tests PROPERTIES LIBRARY_OUTPUT_DIRECTORY_${config} ${testdir})
75 endforeach()
76endif()
77
Jason Rhinelanderdd3d56a2016-08-26 17:11:40 -040078# Make sure pytest is found or produce a fatal error
Dean Moldovan18319d52016-08-13 02:44:56 +020079if(NOT PYBIND11_PYTEST_FOUND)
Wenzel Jakob6a1734a2016-10-09 20:10:49 +020080 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)
Jason Rhinelanderdd3d56a2016-08-26 17:11:40 -040083 message(FATAL_ERROR "Running the tests requires pytest. Please install it manually (try: ${PYTHON_EXECUTABLE} -m pip install pytest)")
Dean Moldovan18319d52016-08-13 02:44:56 +020084 endif()
Wenzel Jakobb55a5c52016-10-09 13:51:05 +020085 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
Dean Moldovan18319d52016-08-13 02:44:56 +020086endif()
87
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020088# A single command to compile and run the tests
Jason Rhinelander52f4be82016-09-03 14:54:22 -040089add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES}
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020090 DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})