Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running only one single test with CMake + make

Tags:

testing

cmake

If I have a bunch of tests for my project, I can run them - after cmakeing and makeing to build, building - with make test.

But what if I only want to run one of my tests? That is, one of the items for which I have a add_test() in the tests CMakeFile.txt ?

like image 726
einpoklum Avatar asked Jan 12 '19 14:01

einpoklum


2 Answers

tl;dr - Do this:

cd $YOUR_BUILD_DIRECTORY
ctest -R name_of_your_test

Explanation

Here is how you likely got confused:

  1. You were trying to do this with make, since make test runs all the tests for you. This won't work for a single test (although there's a workaround - see @danger89's answer). ctest is the cross-platform way to do it.

  2. You started using ctest, e.g. in your main project directory, and you probably got something like:

     *********************************
     No test configuration file found!
     *********************************
     Usage
    
       ctest [options]
    

    which wasn't helpful.

  3. ... So you thought "Ok, maybe ctest has a -C switch, like CMake and GNU Make" - and indeed, it has a -C switch! but that didn't do what you expected:

     [joeuser:/home/joeuser/src/myproj]$ ctest -C build
     Test project /home/joeuser/src/myproj
     No tests were found!!!
    

What you actually need to do:

  cd $YOUR_BUILD_DIRECTORY
  ctest -R name_of_your_test

(note that -R matches a regular expression.) This should work. Note that you can list the tests to be run rather than actually run them by passing -N to ctest.


Thanks goes to @RTsyvarev for pointing me in the right direction

like image 177
einpoklum Avatar answered Oct 17 '22 23:10

einpoklum


einpoklum is not fully correct. Actually you can use make test if you like. CMake will generate a Makefile and I found out that it accepts an ARGS variable.

Meaning you are able run specific tests via for example the -R (regex) parameter. Which will look like this when using make test:

make test ARGS="-R '^test_'"

This will run only the testcases files that starts with test_ in their filename.

Note: -R above is just an example, it will accept all the arguments that ctest command will accept as well.

Anyway the make test example above will do exactly the same as running:

ctest -R '^test_'
like image 31
Melroy van den Berg Avatar answered Oct 17 '22 23:10

Melroy van den Berg