title: CMake: setting an environmental variable for ctest (or otherwise getting failed test output from ctest/make test automatically)
body: <p>I want ctest to show me the failed tests output by default. That is, I want to run:</p>
  
  <pre><code>$ make all test
  </code></pre>
  
  <p>and see any output of failed tests without having to <code>cat Testing/Temporary/LastTest.log</code>.</p>
  
  <p>It appears that there are two ways of doing this: </p>
  
  <p>(1) Setting the <code>CTEST_OUTPUT_ON_FAILURE</code> environmental variable:</p>
  
  <pre><code> $ CTEST_OUTPUT_ON_FAILURE=1 make all test
   $ # or CTEST_OUTPUT_ON_FAILURE=1 ctest
  </code></pre>
  
  <p>(2) Specifying the <code>--output-on-failure</code> flag to the <code>ctest</code> invocation:</p>
  
  <pre><code> $ ctest --output-on-failure
  </code></pre>
  
  <p>Is there a way to write a CMakeLists.txt file such that ctests dumps failed tests output by default on a normal "make all test" invocation WITHOUT resorting to exporting the environmental variable globally in the session or resorting to a custom target like <code>make check</code> (as described <a href="https://stackoverflow.com/questions/5709914/using-cmake-how-do-i-get-verbose-output-from-ctest">here</a>)?</p>
  
  <p>I am aware of the <code>SET_TESTS_PROPERTIES()</code> command, but trying it out like this:</p>
  
  <pre><code>CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
  PROJECT(testenv CXX)
  ENABLE_TESTING()
  ADD_EXECUTABLE(hello hello.cpp)
  ADD_TEST(testhello hello)
  
  # Following sets the environment variable for the shell in which the test
  # progoram 'hello' is run, but not the shell in which ctest is run
  SET_TESTS_PROPERTIES(testhello
      PROPERTIES ENVIRONMENT "CTEST_OUTPUT_ON_FAILURE=1")
  </code></pre>
  
  <p>and experimenting shows that the environmental variable is set in the shell that the test program is executed in, but not in the shell that ctest is executed in.</p>