Pass argument from ctest to gtest
Asked Answered
Q

4

17

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!

Quickstep answered 31/10, 2017 at 12:25 Comment(1)
Those who also has this problem should perhaps upwote this Kitware request: gitlab.kitware.com/cmake/cmake/-/issues/20470Thereupon
T
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.

Tilly answered 16/7, 2019 at 13:22 Comment(2)
Could you add an example of how to filter with ctest? ctest -L FixtureName.* did not work for me.Wristwatch
NVM seems like I should have used -R instead of -L.Wristwatch
E
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.

Eurus answered 29/1, 2018 at 4:35 Comment(1)
This actually answers the question.Enterotomy
F
1

Take a look at CMakes's add_test add_test.

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

Fixer answered 31/10, 2017 at 12:51 Comment(2)
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.Quickstep
And why is it not working to use ctest -L <RegEx to match the tests I want>Fixer
C
1

If you have a complex project with multiple test binaries (== multiple CMake add_test commands) each of which might have many different Google Tests, then an alternative option is to do:

env GTEST_FILTER="*some-keyword*" ctest -VV -R '.*some-test-type.*'

some-keyword will be passed to --gtest_filter

some-test-type will be used by CTest to filter which test binary (add_test command).

For example:

env GTEST_FILTER="*authentication*" ctest -VV -R '.*system.*'

...to run just the Authentication-related System tests. Any non-Authentication or non-System tests are skipped.

Cleareyed answered 9/1 at 16:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.