Skip to content

Type Tests in namespace are put in anonymous namespace if name generator does not start with digit #365

@DominikGrabiec

Description

@DominikGrabiec

Google Test TYPED_TESTs which are defined in a namespace are instead put into the anonymous namespace in the Test Explorer list if a custom name generator is provided which generates names not starting with a digit.

Without a name generator:

#include <gtest/gtest.h>
#include <string>

namespace example
{
template <typename T> struct TestSuite : public ::testing::Test {};
using TestTypes = ::testing::Types<int, float>;
TYPED_TEST_SUITE(TestSuite, TestTypes);
TYPED_TEST(TestSuite, test_zero){}
TYPED_TEST(TestSuite, test_one){}
}

You end up with integers for the test type names but the tests end up in the correct namespace:
Image

Adding a name generator with simple names:

#include <gtest/gtest.h>
#include <string>

namespace example
{
class NameGenerator
{
public:
	template <typename T> static std::string GetName(int i)
	{
		if constexpr (std::is_same_v<T, int>) return "int";
		if constexpr (std::is_same_v<T, float>) return "float";
	}
};

template <typename T> struct TestSuite : public ::testing::Test{};
using TestTypes = ::testing::Types<int, float>;
TYPED_TEST_SUITE(TestSuite, TestTypes, NameGenerator);
TYPED_TEST(TestSuite, test_zero){}
TYPED_TEST(TestSuite, test_one){}
}

You end up with nicer test type names but the tests end up in the anonymous namespace:
Image

Changing the name generator to start with a digit:

#include <gtest/gtest.h>
#include <string>

namespace example
{
class NameGenerator
{
public:
	template <typename T> static std::string GetName(int i)
	{
		if constexpr (std::is_same_v<T, int>) return "0_int";
		if constexpr (std::is_same_v<T, float>) return "0_float";
	}
};

template <typename T> struct TestSuite : public ::testing::Test{};
using TestTypes = ::testing::Types<int, float>;
TYPED_TEST_SUITE(TestSuite, TestTypes, NameGenerator);
TYPED_TEST(TestSuite, test_zero){}
TYPED_TEST(TestSuite, test_one){}
}

Which ends up with somewhat more correct naming, but it shouldn't be necessary.
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions