google / google/googletest

Allow group test functions in Value-Parametrized Tests

Open
#2,567 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
C++
Stars
39.5k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

Let's say I have test data like this (looks like in #2566 ? No accident!):

```cpp
struct test_data
{
foo f;
bool bar;
} tests[] = {
{ foo(), true },
{ foo(), false },
// ...
};

class X : public testing::TestWIthParam{}

TEST_P(X, first) {}
TEST_P(X, second) {}
```

For the most part, this is exactly what I want. But let's say my `foo` type should behave like a value type, including hashing to sufficiently unique values.

I can always define a standalone test case to do that:

```cpp
TEST(Z, unique_hashes)
{
std::set hashes;
std::hash hasher;

for (size_t i = 0 ; i < sizeof(tests) / sizeof(test_data) ; ++i) {
hashes.insert(hasher(tests[i].f);
}

// The expecation is that the size of the set is the same as the size of the test data
ASSERT_EQ(sizeof(tests) / sizeof(test_data), hashes.size());
}
```

It works just fine - but the `Z` test is reported as its own unique group when really it belongs to tests on the same test data. I'd like to see something like this:

```cpp
TEST_PG(X, unique_hashes)
{
std::set hashes;
std::hash hasher;

for (auto item : GetValues()) {
hashes.insert(hasher(item.f);
}

// The expecation is that the size of the set is the same as the size of the test data
ASSERT_EQ(GetValues().size(), hashes.size());
}
```

Which then gets instantiated along with the rest of the suite. Except of course only once, not once for each value.

Contributor guide

Open the contributing guide

Research direction

Start by tracing how TEST_P registers value-parameterized tests and how the supplied test_data array is instantiated. Investigate how a grouped function such as the proposed TEST_PG could access all values and be reported with the same suite; done means it runs once per suite rather than once per value while remaining grouped with the other tests.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.