UnitTest.cmake 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. if(NOT PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  9. return()
  10. endif()
  11. enable_testing()
  12. include(ProcessorCount)
  13. ProcessorCount(PROCESSOR_COUNT)
  14. if(NOT PROCESSOR_COUNT EQUAL 0)
  15. set(CTEST_RUN_FLAGS -j${PROCESSOR_COUNT})
  16. endif()
  17. mark_as_advanced(CTEST_RUN_FLAGS)
  18. list(APPEND CTEST_RUN_FLAGS
  19. -C $<CONFIG>
  20. --output-on-failure)
  21. list(JOIN CTEST_RUN_FLAGS " " CTEST_RUN_FLAGS_STRING)
  22. set(CTEST_RUN_FLAGS ${CTEST_RUN_FLAGS_STRING} CACHE STRING "Command line arguments passed to ctest for running" FORCE)
  23. # Updates the built-in RUN_TESTS target with the ctest run flags as well
  24. # This requires CMake version 3.17
  25. # Note that we omit benchmarks from running in the auto-generated "RUN_TESTS" target by
  26. # settings a custom value for CTEST_ARGUMENTS, however since we DO want them
  27. # to run in other custom targets such as the "benchmark" suite, we don't re-use
  28. # CMAKE_TEST_ARGUMENTS in the other targets.
  29. # Those targets use the CTEST_RUN_FLAGS variable instead, which is captured above,
  30. # before we append this value:
  31. set(CMAKE_CTEST_ARGUMENTS ${CTEST_RUN_FLAGS} -LE SUITE_benchmark)
  32. #! ly_add_suite_build_and_run_targets - Add CMake Targets for associating dependencies with each
  33. # suite of test supported by Open 3D Engine
  34. function(ly_add_suite_build_and_run_targets)
  35. if(NOT PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  36. return()
  37. endif()
  38. foreach(suite_name ${ARGV})
  39. # Add a custom target for each suite
  40. add_custom_target(TEST_SUITE_${suite_name})
  41. set_target_properties(TEST_SUITE_${suite_name} PROPERTIES
  42. VS_DEBUGGER_COMMAND "${CMAKE_CTEST_COMMAND}"
  43. VS_DEBUGGER_COMMAND_ARGUMENTS "${CTEST_RUN_FLAGS_STRING} -L SUITE_${suite_name}"
  44. FOLDER "CMakePredefinedTargets/Test Suites"
  45. )
  46. endforeach()
  47. endfunction()
  48. # Templates used to generate the registry of unit test modules
  49. set(test_json_template [[
  50. {
  51. "Amazon":
  52. {
  53. @target_test_dependencies_names@
  54. }
  55. }]]
  56. )
  57. set(test_module_template [[
  58. "@stripped_test_target@":
  59. {
  60. "Modules":["$<TARGET_FILE_NAME:@namespace_and_target@>"]
  61. }]]
  62. )
  63. #! ly_delayed_generate_test_runner_registry: Generates a .json file for all the test modules that aztestrunner can process
  64. #
  65. function(ly_delayed_generate_unit_test_module_registry)
  66. if(NOT PAL_TRAIT_BUILD_TESTS_SUPPORTED)
  67. return()
  68. endif()
  69. get_property(ly_delayed_aztestrunner_test_modules GLOBAL PROPERTY LY_AZTESTRUNNER_TEST_MODULES)
  70. list(REMOVE_DUPLICATES ly_delayed_aztestrunner_test_modules) # Strip out any duplicate test targets
  71. set(target_test_dependencies_names)
  72. set(test_module_name_components)
  73. foreach(namespace_and_target ${ly_delayed_aztestrunner_test_modules})
  74. # Strip target namespace from test targets before configuring them into the json template
  75. ly_strip_target_namespace(TARGET ${namespace_and_target} OUTPUT_VARIABLE stripped_test_target)
  76. string(CONFIGURE ${test_module_template} target_module_json @ONLY)
  77. list(APPEND target_test_dependencies_names ${target_module_json})
  78. endforeach()
  79. list(JOIN target_test_dependencies_names ",\n" target_test_dependencies_names)
  80. string(CONFIGURE ${test_json_template} testrunner_json @ONLY)
  81. set(dependencies_setreg ${CMAKE_BINARY_DIR}/unit_test_modules.json)
  82. file(GENERATE OUTPUT ${dependencies_setreg} CONTENT "${testrunner_json}")
  83. endfunction()