jlconlin
jlconlin

Reputation: 15064

How to use CTest to check difference between two files?

I'm trying to create some integrated tests for some legacy software. The current way to do testing is to run the code with known inputs and manually compare the output to the known output.

I'd like to automate this process, since I'm already using CMake I would like to do this with CTest. I have about two dozen inputs/outputs that I need to check.

What is the right way to do this? I only have a moderate amount of experience with CMake and even less with CTest.

Upvotes: 4

Views: 1497

Answers (1)

Fraser
Fraser

Reputation: 78398

You can use the PASS_REGULAR_EXPRESSION property of tests to achieve this.

Say you have code which takes an input of a single int and outputs the phrase "The result is " with 10*input appended. So e.g. in C++ something like:

#include <iostream>
#include <cstdlib>

int main(int argc, const char* argv[]) {
  if (argc != 2)
    return -1;
  std::cout << "The result is " << 10 * std::atoi(argv[1]) << '\n';
  return 0;
}

Then you could test for this using CTest by doing:

cmake_minimum_required(VERSION 3.0)
project(Example)

add_executable(example main.cpp)

include(CTest)

set(Inputs 1 2 3)
foreach(Input ${Inputs})
  add_test(NAME Test${Input} COMMAND example ${Input})
  set_tests_properties(Test${Input} PROPERTIES
                       PASS_REGULAR_EXPRESSION "The result is ${Input}0")
endforeach()

Upvotes: 4

Related Questions