If I have a bunch of tests for my project, I can run them - after cmake
ing and make
ing 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
?
tl;dr - Do this:
cd $YOUR_BUILD_DIRECTORY
ctest -R name_of_your_test
Here is how you likely got confused:
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.
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.
... 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
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_'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With