RunCMakeTest.cmake 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. include(RunCTest)
  2. set(RunCMake_TEST_TIMEOUT 60)
  3. unset(ENV{CTEST_PARALLEL_LEVEL})
  4. unset(ENV{CTEST_OUTPUT_ON_FAILURE})
  5. set(CASE_CTEST_TEST_ARGS "")
  6. set(CASE_CTEST_TEST_LOAD "")
  7. function(run_ctest_test CASE_NAME)
  8. set(CASE_CTEST_TEST_ARGS "${ARGN}")
  9. run_ctest(${CASE_NAME})
  10. endfunction()
  11. run_ctest_test(TestQuiet QUIET)
  12. # Tests for the 'Test Load' feature of ctest
  13. #
  14. # Spoof a load average value to make these tests more reliable.
  15. set(ENV{__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING} 5)
  16. set(RunCTest_VERBOSE_FLAG -VV)
  17. # Verify that new tests are started when the load average falls below
  18. # our threshold.
  19. run_ctest_test(TestLoadPass TEST_LOAD 6)
  20. # Verify that new tests are not started when the load average exceeds
  21. # our threshold and that they then run once the load average drops.
  22. run_ctest_test(TestLoadWait TEST_LOAD 2)
  23. # Verify that when an invalid "TEST_LOAD" value is given, a warning
  24. # message is displayed and the value is ignored.
  25. run_ctest_test(TestLoadInvalid TEST_LOAD "ERR1")
  26. # Verify that new tests are started when the load average falls below
  27. # our threshold.
  28. set(CASE_CTEST_TEST_LOAD 7)
  29. run_ctest_test(CTestTestLoadPass)
  30. # Verify that new tests are not started when the load average exceeds
  31. # our threshold and that they then run once the load average drops.
  32. set(CASE_CTEST_TEST_LOAD 4)
  33. run_ctest_test(CTestTestLoadWait)
  34. # Verify that when an invalid "CTEST_TEST_LOAD" value is given,
  35. # a warning message is displayed and the value is ignored.
  36. set(CASE_CTEST_TEST_LOAD "ERR2")
  37. run_ctest_test(CTestTestLoadInvalid)
  38. # Verify that the "TEST_LOAD" value has higher precedence than
  39. # the "CTEST_TEST_LOAD" value
  40. set(CASE_CTEST_TEST_LOAD "ERR3")
  41. run_ctest_test(TestLoadOrder TEST_LOAD "ERR4")
  42. unset(ENV{__CTEST_FAKE_LOAD_AVERAGE_FOR_TESTING})
  43. unset(CASE_CTEST_TEST_LOAD)
  44. unset(RunCTest_VERBOSE_FLAG)
  45. function(run_TestChangeId)
  46. set(CASE_TEST_PREFIX_CODE [[
  47. set(CTEST_CHANGE_ID "<>1")
  48. ]])
  49. run_ctest(TestChangeId)
  50. endfunction()
  51. run_TestChangeId()
  52. function(run_TestOutputSize)
  53. set(CASE_CTEST_TEST_ARGS EXCLUDE RunCMakeVersion)
  54. set(CASE_TEST_PREFIX_CODE [[
  55. set(CTEST_CUSTOM_MAXIMUM_PASSED_TEST_OUTPUT_SIZE 10)
  56. set(CTEST_CUSTOM_MAXIMUM_FAILED_TEST_OUTPUT_SIZE 12)
  57. ]])
  58. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  59. add_test(NAME PassingTest COMMAND ${CMAKE_COMMAND} -E echo PassingTestOutput)
  60. add_test(NAME FailingTest COMMAND ${CMAKE_COMMAND} -E no_such_command)
  61. ]])
  62. run_ctest(TestOutputSize)
  63. endfunction()
  64. run_TestOutputSize()
  65. # Test --test-output-truncation
  66. function(run_TestOutputTruncation mode expected)
  67. set(CASE_CTEST_TEST_ARGS EXCLUDE RunCMakeVersion)
  68. set(TRUNCATED_OUTPUT ${expected}) # used in TestOutputTruncation-check.cmake
  69. set(CASE_TEST_PREFIX_CODE [[
  70. set( CTEST_CUSTOM_TEST_OUTPUT_TRUNCATION${mode})
  71. ]])
  72. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  73. add_test(NAME Truncation_${mode} COMMAND ${CMAKE_COMMAND} -E echo 123456789)
  74. ]])
  75. run_ctest(TestOutputTruncation)
  76. endfunction()
  77. run_TestOutputTruncation("head" "...6789")
  78. run_TestOutputTruncation("middle" "12....*...89")
  79. run_TestOutputTruncation("tail" "12345...")
  80. run_ctest_test(TestRepeatBad1 REPEAT UNKNOWN:3)
  81. run_ctest_test(TestRepeatBad2 REPEAT UNTIL_FAIL:-1)
  82. function(run_TestRepeat case return_value )
  83. set(CASE_CTEST_TEST_ARGS RETURN_VALUE result EXCLUDE RunCMakeVersion ${ARGN})
  84. string(CONCAT suffix_code [[
  85. add_test(NAME testRepeat
  86. COMMAND ${CMAKE_COMMAND} -D COUNT_FILE=${CMAKE_CURRENT_BINARY_DIR}/count.cmake
  87. -P "]] "${RunCMake_SOURCE_DIR}/TestRepeat${case}" [[.cmake")
  88. set_property(TEST testRepeat PROPERTY TIMEOUT 5)
  89. ]])
  90. string(APPEND CASE_CMAKELISTS_SUFFIX_CODE "${suffix_code}")
  91. run_ctest(TestRepeat${case})
  92. #write to end of the test file logic to Verify we get the expected
  93. #return code
  94. string(REPLACE "RETURN_VALUE:" "" return_value "${return_value}" )
  95. file(APPEND "${RunCMake_BINARY_DIR}/TestRepeat${case}/test.cmake"
  96. "
  97. set(expected_result ${return_value})
  98. message(STATUS \${result})
  99. if(NOT result EQUAL expected_result)
  100. message(FATAL_ERROR \"expected a return value of: \${expected_result},
  101. instead got: \${result}\")
  102. endif()
  103. "
  104. )
  105. endfunction()
  106. run_TestRepeat(UntilFail RETURN_VALUE:1 REPEAT UNTIL_FAIL:3)
  107. run_TestRepeat(UntilPass RETURN_VALUE:0 REPEAT UNTIL_PASS:3)
  108. run_TestRepeat(AfterTimeout RETURN_VALUE:0 REPEAT AFTER_TIMEOUT:3)
  109. # test repeat and not run tests interact correctly
  110. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  111. add_test(NAME testNotRun
  112. COMMAND ${CMAKE_COMMAND}/doesnt_exist)
  113. set_property(TEST testNotRun PROPERTY TIMEOUT 5)
  114. ]])
  115. run_TestRepeat(NotRun RETURN_VALUE:1 REPEAT UNTIL_PASS:3)
  116. unset(CASE_CMAKELISTS_SUFFIX_CODE)
  117. # test --stop-on-failure
  118. function(run_stop_on_failure)
  119. set(CASE_CTEST_TEST_ARGS EXCLUDE RunCMakeVersion)
  120. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  121. add_test(NAME StoppingTest COMMAND ${CMAKE_COMMAND} -E false)
  122. add_test(NAME NotRunTest COMMAND ${CMAKE_COMMAND} -E true)
  123. ]])
  124. run_ctest_test(stop-on-failure STOP_ON_FAILURE)
  125. endfunction()
  126. run_stop_on_failure()
  127. # Make sure environment gets logged
  128. function(run_environment)
  129. set(ENV{BAD_ENVIRONMENT_VARIABLE} "Bad environment variable")
  130. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  131. set_property(TEST RunCMakeVersion PROPERTY ENVIRONMENT "ENV1=env1;ENV2=env2")
  132. ]])
  133. run_ctest(TestEnvironment)
  134. endfunction()
  135. run_environment()
  136. # test for OUTPUT_JUNIT
  137. run_ctest_test(OutputJUnit OUTPUT_JUNIT junit.xml REPEAT UNTIL_FAIL:2)
  138. # Verify that extra measurements get reported.
  139. function(run_measurements)
  140. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  141. add_test(
  142. NAME double_measurement
  143. COMMAND ${CMAKE_COMMAND} -E
  144. echo <DartMeasurement type="numeric/double" name="my_custom_value">1.4847</DartMeasurement>)
  145. add_test(
  146. NAME double_measurement2
  147. COMMAND ${CMAKE_COMMAND} -E
  148. echo <CTestMeasurement type="numeric/double" name="another_custom_value">1.8474</CTestMeasurement>)
  149. add_test(
  150. NAME img_measurement
  151. COMMAND ${CMAKE_COMMAND} -E
  152. echo <DartMeasurementFile name="TestImage" type="image/png">]] ${IMAGE_DIR}/cmake-logo-16.png [[</DartMeasurementFile>)
  153. add_test(
  154. NAME img_measurement2
  155. COMMAND ${CMAKE_COMMAND} -E
  156. echo <CTestMeasurementFile name="TestImage2" type="image/png">]] ${IMAGE_DIR}/cmake-logo-16.png [[</CTestMeasurementFile>)
  157. add_test(
  158. NAME file_measurement
  159. COMMAND ${CMAKE_COMMAND} -E
  160. echo <DartMeasurementFile name="my_test_input_data" type="file">]] ${IMAGE_DIR}/cmake-logo-16.png [[</DartMeasurementFile>)
  161. add_test(
  162. NAME file_measurement2
  163. COMMAND ${CMAKE_COMMAND} -E
  164. echo <CTestMeasurementFile name="another_test_input_data" type="file">]] ${IMAGE_DIR}/cmake-logo-16.png [[</CTestMeasurementFile>)
  165. ]])
  166. run_ctest(TestMeasurements)
  167. endfunction()
  168. run_measurements()
  169. # Verify that test output can override the Completion Status.
  170. function(run_completion_status)
  171. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  172. add_test(
  173. NAME custom_details
  174. COMMAND ${CMAKE_COMMAND} -E
  175. echo test output\n<CTestDetails>CustomDetails</CTestDetails>\nmore output)
  176. ]])
  177. run_ctest(TestCompletionStatus)
  178. endfunction()
  179. run_completion_status()
  180. # Verify that running ctest_test() multiple times with different label arguments
  181. # doesn't break.
  182. function(run_changing_labels)
  183. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  184. add_test(NAME a COMMAND ${CMAKE_COMMAND} -E true)
  185. set_property(TEST a PROPERTY LABELS a)
  186. add_test(NAME b COMMAND ${CMAKE_COMMAND} -E true)
  187. set_property(TEST b PROPERTY LABELS b)
  188. ]])
  189. run_ctest(TestChangingLabels)
  190. endfunction()
  191. run_changing_labels()
  192. # Verify that test output can add additional labels
  193. function(run_extra_labels)
  194. set(CASE_CMAKELISTS_SUFFIX_CODE [[
  195. add_test(
  196. NAME custom_labels
  197. COMMAND ${CMAKE_COMMAND} -E
  198. echo before\n<CTestLabel>label2</CTestLabel>\n<CTestLabel>label1</CTestLabel>\n<CTestLabel>label3</CTestLabel>\n<CTestLabel>label2</CTestLabel>\nafter)
  199. set_tests_properties(custom_labels PROPERTIES LABELS "label1")
  200. ]])
  201. run_ctest(TestExtraLabels)
  202. endfunction()
  203. run_extra_labels()