Specify namespace/class in VS2019 Test Explorer while using CTest/CMake

Viewed 610

I'm using CMake + VS2019. In my test definition I have something like:

add_test(NAME "common/base64" COMMAND my_unit_test "common/base64")

VS2019 displays this like:

enter image description here

In project I have hundreds of tests and it is very inconvenient to search through unclear randomly generated names, expanding each items. So my questions:

  • How can I specify recognizable test name (instead of EBF.Tests.52488745200006951440) ?
  • How to specify namespace / class?

Also I can see Vs2019 Test Explorer can group by "Traits" - may be I can specify it over this feature?

2 Answers

It seems that the "EBF.Tests" prefix is the "project name".

You can disable the test prefix or set a new one by altering the TEST_PREFIX setting in your test autodiscovery method or the test naming itself.

For CTest, by altering the test naming:

# use [namespace].[class].[testname] naming 
add_test("lib.namespace.Tests.SomeTest" test_exe)

For Catch2, by altering the test name prefixing by changing TEST_PREFIX in the auto-discovery method:

catch_discover_tests(
    TagTestsTarget
    TEST_SPEC "[tag]"                   # select tests by tag name
    TEST_PREFIX "lib.namespace.Tests."  # last dot required !
)

Fast Solution:

Set the Group By option in the Test Explorer to visualize by Class.

Then name each test in your CMake files as: mytestname.mytestname

As an example: add_test(NAME mytest.mytest COMMAND mytest)

(Not a perfect solution but it works well enough.)

More details:

When naming tests for the Team Explorer, if there is one dot between the name, will be interpreted as class name then function name. If there are two dots it will be interpreted as namespace, class, function. Any more dots are appended in the function string.

You could also display on the Team Explorer by namespace and then use these naming features to group your tests in at most two levels of hierarchy.

As example you could name all the tests related to a given feature with the same class or namespace and they will display nicely in the same set in the Team Explorer window.

Related