-1

I would like PASS_REGULAR_EXPRESSION to also check stderr. Is it possible? I tried the following:

add_test(NAME test COMMAND executable)
set_tests_properties(test PROPERTIES
  WILL_FAIL true
  PASS_REGULAR_EXPRESSION "I was printed to stderr"
)

but it doesn't work since PASS_REGULAR_EXPRESSION checks only stdout. I tried to redirect stderr to stdout with:

add_test(NAME test COMMAND executable 2>&1)

But it doesn't seem to work either.

Aleksander Krauze
  • 3,115
  • 7
  • 18

1 Answers1

0

PASS_REGULAR_EXPRESSION does check stderr, but it causes the exit code to be ignored. Your example therefore failed due to WILL_FAIL: CTest found the regular expression, but it expected not to, and therefore marked the test as failed. After removing WILL_FAIL, the tests pass:

// main.c
#include <stdio.h>

int main(void) {
  fprintf(stderr, "I was printed to stderr\n");
}
# CMakeLists.txt
cmake_minimum_required(VERSION 3.16)
project(SO_74340897)

add_executable(executable main.c)
enable_testing()

add_test(NAME test COMMAND executable)
set_tests_properties(test PROPERTIES
  #WILL_FAIL true
  PASS_REGULAR_EXPRESSION "I was printed to stderr"
)

Running the tests like:

$ ctest
Test project /home/jschulze/tmp/stackoverflow74340897/build
    Start 1: test
1/1 Test #1: test .............................   Passed    0.00 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.00 sec
jonas-schulze
  • 238
  • 1
  • 13