16

I am using gtest to write unit tests for my application. I also have ctest that runs all executables added by add_test CMake command. Is it possible to pass gtest variables through ctest when test execution starts?

I would like to for example sometimes filter out tests with --gtest_filter flag but I don't know how or if this is even possible through ctest? I have tried the following ways:

ctest --gtest_filter=AppTest.*
ctest --test-arguments="--gtest_filter=AppTest.*"

But both still run all tests instead the filtered ones.

Thanks!

drodil
  • 2,292
  • 1
  • 22
  • 37
  • Those who also has this problem should perhaps upwote this Kitware request: https://gitlab.kitware.com/cmake/cmake/-/issues/20470 – Lars Jun 07 '21 at 07:02

3 Answers3

5

For anyone looking here in 2019, recent versions of CMake have gtest_discover_tests (GoogleTest module) which will hoist your tests into CTest and you can filter from there.

IOW rather than having a single add_test in CTest, it will use --gtest_list_tests to call add_test for each of your tests.

Daniel Wyatt
  • 379
  • 3
  • 7
2

For example, to make tests' output verbose:

$ make test ARGS="-V"

To run a particular test:

$ ctest -R <regex>

NB: You can have a look at this for some examples.

1

Take a look at CMakes's add_test add_test.

To filter out tests from CTest you can use -L ctest

Croer01
  • 15
  • 1
  • 3
Th. Thielemann
  • 2,592
  • 1
  • 23
  • 38
  • 4
    I know the params can be added to the add_test command but what I would like to achieve is to filter tests when running ctest - not by changing cmake files to achieve that. – drodil Oct 31 '17 at 12:52
  • And why is it not working to use `ctest -L ` – Th. Thielemann Mar 31 '23 at 13:08