Просмотр исходного кода

Support running unit tests for plug-ins

Ghostkeeper 8 лет назад
Родитель
Сommit
ecc973951e
2 измененных файлов с 44 добавлено и 0 удалено
  1. 4 0
      CMakeLists.txt
  2. 40 0
      cmake/CuraTests.cmake

+ 4 - 0
CMakeLists.txt

@@ -1,11 +1,15 @@
 project(cura NONE)
 cmake_minimum_required(VERSION 2.8.12)
 
+set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake/
+                      ${CMAKE_MODULE_PATH})
+
 include(GNUInstallDirs)
 
 set(URANIUM_SCRIPTS_DIR "${CMAKE_SOURCE_DIR}/../uranium/scripts" CACHE DIRECTORY "The location of the scripts directory of the Uranium repository")
 
 # Tests
+include(CuraTests)
 # Note that we use exit 0 here to not mark the build as a failure on test failure
 add_custom_target(tests)
 add_custom_command(TARGET tests POST_BUILD COMMAND "PYTHONPATH=${CMAKE_SOURCE_DIR}/../Uranium/:${CMAKE_SOURCE_DIR}" ${PYTHON_EXECUTABLE} -m pytest -r a --junitxml=${CMAKE_BINARY_DIR}/junit.xml ${CMAKE_SOURCE_DIR} || exit 0)

+ 40 - 0
cmake/CuraTests.cmake

@@ -0,0 +1,40 @@
+# Copyright (c) 2017 Ultimaker B.V.
+# Cura is released under the terms of the AGPLv3 or higher.
+
+enable_testing()
+include(CMakeParseArguments)
+
+function(cura_add_test)
+    set(_single_args NAME DIRECTORY PYTHONPATH)
+    cmake_parse_arguments("" "" "${_single_args}" "" ${ARGN})
+
+    if(NOT _NAME)
+        message(FATAL_ERROR "cura_add_test requires a test name argument")
+    endif()
+
+    if(NOT _DIRECTORY)
+        message(FATAL_ERROR "cura_add_test requires a directory to test")
+    endif()
+
+    if(NOT _PYTHONPATH)
+        set(_PYTHONPATH ${_DIRECTORY})
+    endif()
+
+    add_test(
+        NAME ${_NAME}
+        COMMAND ${PYTHON_EXECUTABLE} -m pytest --junitxml=${CMAKE_BINARY_DIR}/junit-${_NAME}.xml ${_DIRECTORY}
+    )
+    set_tests_properties(${_NAME} PROPERTIES ENVIRONMENT LANG=C)
+    set_tests_properties(${_NAME} PROPERTIES ENVIRONMENT PYTHONPATH=${_PYTHONPATH})
+endfunction()
+
+cura_add_test(NAME pytest-main DIRECTORY ${CMAKE_SOURCE_DIR}/tests PYTHONPATH ${CMAKE_SOURCE_DIR})
+
+file(GLOB_RECURSE _plugins plugins/*/__init__.py)
+foreach(_plugin ${_plugins})
+    get_filename_component(_plugin_directory ${_plugin} DIRECTORY)
+    if(EXISTS ${_plugin_directory}/tests)
+        get_filename_component(_plugin_name ${_plugin_directory} NAME)
+        cura_add_test(NAME pytest-${_plugin_name} DIRECTORY ${_plugin_directory} PYTHONPATH "${CMAKE_SOURCE_DIR}:${_plugin_directory}")
+    endif()
+endforeach()