drodil
drodil

Reputation: 2346

Pass argument from ctest to gtest

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!

Upvotes: 19

Views: 8516

Answers (4)

JBRWilkinson
JBRWilkinson

Reputation: 4875

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.

Upvotes: 1

Daniel Wyatt
Daniel Wyatt

Reputation: 379

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.

Upvotes: 5

Th. Thielemann
Th. Thielemann

Reputation: 2824

Take a look at CMakes's add_test add_test.

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

Upvotes: 1

Misha Tavkhelidze
Misha Tavkhelidze

Reputation: 832

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.

Upvotes: 2

Related Questions