blob: 7470c5d930607417b2afa3a66eac1a157c60e84f [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
Ben Northbbe45082016-10-20 21:19:30 +010015 test_copy_move_policies.cpp
Jason Rhinelander52f4be82016-09-03 14:54:22 -040016 test_eigen.cpp
Dean Moldovana9a37b42016-08-13 00:57:24 +020017 test_enum.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020018 test_eval.cpp
19 test_exceptions.cpp
20 test_inheritance.cpp
21 test_issues.cpp
22 test_keep_alive.cpp
23 test_kwargs_and_defaults.cpp
24 test_methods_and_attributes.cpp
25 test_modules.cpp
Dean Moldovan568ec6b2016-09-20 11:52:25 +020026 test_multiple_inheritance.cpp
Ivan Smirnov91b3d682016-08-29 02:41:05 +010027 test_numpy_array.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020028 test_numpy_dtypes.cpp
29 test_numpy_vectorize.cpp
30 test_opaque_types.cpp
31 test_operator_overloading.cpp
32 test_pickling.cpp
33 test_python_types.cpp
34 test_sequences_and_iterators.cpp
Dean Moldovan568ec6b2016-09-20 11:52:25 +020035 test_smart_ptr.cpp
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020036 test_stl_binders.cpp
37 test_virtual_functions.cpp
38)
39
Jason Rhinelander52f4be82016-09-03 14:54:22 -040040string(REPLACE ".cpp" ".py" PYBIND11_PYTEST_FILES "${PYBIND11_TEST_FILES}")
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020041
Jason Rhinelander52f4be82016-09-03 14:54:22 -040042# Check if Eigen is available; if not, remove from PYBIND11_TEST_FILES (but
43# keep it in PYBIND11_PYTEST_FILES, so that we get the "eigen is not installed"
44# skip message).
45list(FIND PYBIND11_TEST_FILES test_eigen.cpp PYBIND11_TEST_FILES_EIGEN_I)
46if(PYBIND11_TEST_FILES_EIGEN_I GREATER -1)
47 find_package(Eigen3 QUIET)
48
49 if(EIGEN3_FOUND)
50 message(STATUS "Building tests with Eigen v${EIGEN3_VERSION}")
51 else()
52 list(REMOVE_AT PYBIND11_TEST_FILES ${PYBIND11_TEST_FILES_EIGEN_I})
53 message(STATUS "Building tests WITHOUT Eigen")
54 endif()
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020055endif()
56
57# Create the binding library
Wenzel Jakobdac38582016-09-29 21:30:00 +020058pybind11_add_module(pybind11_tests pybind11_tests.cpp
59 ${PYBIND11_TEST_FILES} ${PYBIND11_HEADERS})
60
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020061pybind11_enable_warnings(pybind11_tests)
62
63if(EIGEN3_FOUND)
64 target_include_directories(pybind11_tests PRIVATE ${EIGEN3_INCLUDE_DIR})
65 target_compile_definitions(pybind11_tests PRIVATE -DPYBIND11_TEST_EIGEN)
66endif()
67
68set(testdir ${PROJECT_SOURCE_DIR}/tests)
69
70# Always write the output file directly into the 'tests' directory (even on MSVC)
71if(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()
77endif()
78
Jason Rhinelanderdd3d56a2016-08-26 17:11:40 -040079# Make sure pytest is found or produce a fatal error
Dean Moldovan18319d52016-08-13 02:44:56 +020080if(NOT PYBIND11_PYTEST_FOUND)
Wenzel Jakob6a1734a2016-10-09 20:10:49 +020081 execute_process(COMMAND ${PYTHON_EXECUTABLE} -m pytest --version --noconftest OUTPUT_QUIET ERROR_QUIET
82 RESULT_VARIABLE PYBIND11_EXEC_PYTHON_ERR)
83 if(PYBIND11_EXEC_PYTHON_ERR)
Jason Rhinelanderdd3d56a2016-08-26 17:11:40 -040084 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 +020085 endif()
Wenzel Jakobb55a5c52016-10-09 13:51:05 +020086 set(PYBIND11_PYTEST_FOUND TRUE CACHE INTERNAL "")
Dean Moldovan18319d52016-08-13 02:44:56 +020087endif()
88
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020089# A single command to compile and run the tests
Jason Rhinelander52f4be82016-09-03 14:54:22 -040090add_custom_target(pytest COMMAND ${PYTHON_EXECUTABLE} -m pytest -rws ${PYBIND11_PYTEST_FILES}
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020091 DEPENDS pybind11_tests WORKING_DIRECTORY ${testdir})