Compare commits

...

7 Commits

Author SHA1 Message Date
Alessandro Colace
51a4a04ba1
Merge ed86686425dfb67d7197f1057a01222e907b051f into 3ff51c3e80f2c2eb105d43ecb9acab9a62e01600 2026-07-29 18:02:18 +01:00
Copybara-Service
3ff51c3e80 Merge pull request #5041 from alvinjaison:fix/get-test-suite-shuffle-index
PiperOrigin-RevId: 955317170
Change-Id: I40e01cdafe58768ccdb743dcf38717e48edcde41
2026-07-28 09:58:12 -07:00
Abseil Team
c6f04243ed Automated Code Change
PiperOrigin-RevId: 955021426
Change-Id: I3ab06e960011d1d0cd44ea00595a5067aaa7f30b
2026-07-27 22:07:28 -07:00
Abseil Team
af4c3cfcb3 Automated Code Change
PiperOrigin-RevId: 955017590
Change-Id: I5125f56376603462fb63796fec1fbc50fd4d935f
2026-07-27 21:57:20 -07:00
Abseil Team
ecb18f0b03 Automated Code Change
PiperOrigin-RevId: 955003621
Change-Id: I83518a7f947d9f7d9b89fe14e1219d32bcdc95a3
2026-07-27 21:17:32 -07:00
Alvin Jaison
1269db9643 Fix GetTestSuite(i) ignoring shuffle order UnitTestImpl::GetTestSuite(int i) computes the shuffled index from test_suite_indices_ but then accesses test_suites_ using the raw parameter 'i' instead of the computed 'index'. This causes the public API UnitTest::GetTestSuite(i) to return test suites in original registration order rather than the active shuffle order. The mutable variant GetMutableSuiteCase(i) correctly uses 'index', and the equivalent function TestSuite::GetTestInfo(i) also uses 'index'. This was a copy-paste typo introduced in commit bd851333 (Sep 2009) when the shuffle feature was first added. The bug is observable from within TestEventListener callbacks during test execution when --gtest_shuffle is active. 2026-07-24 16:11:55 +01:00
Alessandro Colace
ed86686425 Add --gtest_list_tests_brief to omit GetParam/TypeParam annotations
When listing parameterized tests, googletest appends
"# GetParam() = <printed value>" after each test name (and
"# TypeParam = ..." after each typed-test suite). For parameter types
with custom test-name generators (the 4th argument of
INSTANTIATE_TEST_SUITE_P), the annotation is redundant; for large
parameter values, it dominates the output.

Add a boolean flag --gtest_list_tests_brief (and matching
GTEST_LIST_TESTS_BRIEF env var) that suppresses both annotations
during --gtest_list_tests. Default is false, preserving existing
behavior. Extends googletest-list-tests-unittest.py with a regression
test.

Fixes #4937
2026-05-03 14:37:21 +02:00
11 changed files with 97 additions and 16 deletions

View File

@ -30,6 +30,8 @@
// Tests that Google Mock constructs can be used in a large number of // Tests that Google Mock constructs can be used in a large number of
// threads concurrently. // threads concurrently.
#include <iterator>
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
@ -188,7 +190,7 @@ TEST(StressTest, CanUseGMockWithThreads) {
&TestPartiallyOrderedExpectationsWithThreads, &TestPartiallyOrderedExpectationsWithThreads,
}; };
const int kRoutines = sizeof(test_routines) / sizeof(test_routines[0]); const int kRoutines = std::size(test_routines);
const int kCopiesOfEachRoutine = kMaxTestThreads / kRoutines; const int kCopiesOfEachRoutine = kMaxTestThreads / kRoutines;
const int kTestThreads = kCopiesOfEachRoutine * kRoutines; const int kTestThreads = kCopiesOfEachRoutine * kRoutines;
ThreadWithParam<Dummy>* threads[kTestThreads] = {}; ThreadWithParam<Dummy>* threads[kTestThreads] = {};

View File

@ -110,6 +110,10 @@ GTEST_DECLARE_bool_(install_failure_signal_handler);
// are actually run if the flag is provided. // are actually run if the flag is provided.
GTEST_DECLARE_bool_(list_tests); GTEST_DECLARE_bool_(list_tests);
// This flag suppresses the "# GetParam() = ..." and "# TypeParam = ..."
// annotations when listing tests with --gtest_list_tests.
GTEST_DECLARE_bool_(list_tests_brief);
// This flag controls whether Google Test emits a detailed XML report to a file // This flag controls whether Google Test emits a detailed XML report to a file
// in addition to its normal textual output. // in addition to its normal textual output.
GTEST_DECLARE_string_(output); GTEST_DECLARE_string_(output);

View File

@ -39,6 +39,8 @@
#include "sample2.h" #include "sample2.h"
#include <iterator>
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace { namespace {
// In this example, we test the MyString class (a simple string). // In this example, we test the MyString class (a simple string).
@ -78,7 +80,7 @@ const char kHelloString[] = "Hello, world!";
TEST(MyString, ConstructorFromCString) { TEST(MyString, ConstructorFromCString) {
const MyString s(kHelloString); const MyString s(kHelloString);
EXPECT_EQ(0, strcmp(s.c_string(), kHelloString)); EXPECT_EQ(0, strcmp(s.c_string(), kHelloString));
EXPECT_EQ(sizeof(kHelloString) / sizeof(kHelloString[0]) - 1, s.Length()); EXPECT_EQ(std::size(kHelloString) - 1, s.Length());
} }
// Tests the copy c'tor. // Tests the copy c'tor.

View File

@ -587,7 +587,7 @@ class GTEST_API_ UnitTestImpl {
// total_test_suite_count() - 1. If i is not in that range, returns NULL. // total_test_suite_count() - 1. If i is not in that range, returns NULL.
const TestSuite* GetTestSuite(int i) const { const TestSuite* GetTestSuite(int i) const {
const int index = GetElementOr(test_suite_indices_, i, -1); const int index = GetElementOr(test_suite_indices_, i, -1);
return index < 0 ? nullptr : test_suites_[static_cast<size_t>(i)]; return index < 0 ? nullptr : test_suites_[static_cast<size_t>(index)];
} }
// Legacy API is deprecated but still available // Legacy API is deprecated but still available

View File

@ -322,6 +322,12 @@ GTEST_DEFINE_bool_(
GTEST_DEFINE_bool_(list_tests, false, "List all tests without running them."); GTEST_DEFINE_bool_(list_tests, false, "List all tests without running them.");
GTEST_DEFINE_bool_(
list_tests_brief,
testing::internal::BoolFromGTestEnv("list_tests_brief", false),
"True if --gtest_list_tests should omit the per-test "
"\"# GetParam() = ...\" and \"# TypeParam = ...\" annotations.");
// The net priority order after flag processing is thus: // The net priority order after flag processing is thus:
// --gtest_output command line flag // --gtest_output command line flag
// GTEST_OUTPUT environment variable // GTEST_OUTPUT environment variable
@ -6390,6 +6396,7 @@ static void PrintOnOneLine(const char* str, int max_length) {
void UnitTestImpl::ListTestsMatchingFilter() { void UnitTestImpl::ListTestsMatchingFilter() {
// Print at most this many characters for each type/value parameter. // Print at most this many characters for each type/value parameter.
const int kMaxParamLength = 250; const int kMaxParamLength = 250;
const bool brief = GTEST_FLAG_GET(list_tests_brief);
for (auto* test_suite : test_suites_) { for (auto* test_suite : test_suites_) {
bool printed_test_suite_name = false; bool printed_test_suite_name = false;
@ -6400,7 +6407,7 @@ void UnitTestImpl::ListTestsMatchingFilter() {
if (!printed_test_suite_name) { if (!printed_test_suite_name) {
printed_test_suite_name = true; printed_test_suite_name = true;
printf("%s.", test_suite->name()); printf("%s.", test_suite->name());
if (test_suite->type_param() != nullptr) { if (!brief && test_suite->type_param() != nullptr) {
printf(" # %s = ", kTypeParamLabel); printf(" # %s = ", kTypeParamLabel);
// We print the type parameter on a single line to make // We print the type parameter on a single line to make
// the output easy to parse by a program. // the output easy to parse by a program.
@ -6409,7 +6416,7 @@ void UnitTestImpl::ListTestsMatchingFilter() {
printf("\n"); printf("\n");
} }
printf(" %s", test_info->name()); printf(" %s", test_info->name());
if (test_info->value_param() != nullptr) { if (!brief && test_info->value_param() != nullptr) {
printf(" # %s = ", kValueParamLabel); printf(" # %s = ", kValueParamLabel);
// We print the value parameter on a single line to make the // We print the value parameter on a single line to make the
// output easy to parse by a program. // output easy to parse by a program.
@ -6708,6 +6715,10 @@ static const char kColorEncodedHelpMessage[] =
" List the names of all tests instead of running them. The name of\n" " List the names of all tests instead of running them. The name of\n"
" TEST(Foo, Bar) is \"Foo.Bar\".\n" " TEST(Foo, Bar) is \"Foo.Bar\".\n"
" @G--" GTEST_FLAG_PREFIX_ " @G--" GTEST_FLAG_PREFIX_
"list_tests_brief@D\n"
" When listing tests, omit the \"# GetParam() = ...\" and\n"
" \"# TypeParam = ...\" annotations.\n"
" @G--" GTEST_FLAG_PREFIX_
"filter=@YPOSITIVE_PATTERNS" "filter=@YPOSITIVE_PATTERNS"
"[@G-@YNEGATIVE_PATTERNS]@D\n" "[@G-@YNEGATIVE_PATTERNS]@D\n"
" Run only the tests whose name matches one of the positive patterns " " Run only the tests whose name matches one of the positive patterns "
@ -6830,6 +6841,7 @@ static bool ParseGoogleTestFlag(const char* const arg) {
GTEST_INTERNAL_PARSE_FLAG(filter); GTEST_INTERNAL_PARSE_FLAG(filter);
GTEST_INTERNAL_PARSE_FLAG(internal_run_death_test); GTEST_INTERNAL_PARSE_FLAG(internal_run_death_test);
GTEST_INTERNAL_PARSE_FLAG(list_tests); GTEST_INTERNAL_PARSE_FLAG(list_tests);
GTEST_INTERNAL_PARSE_FLAG(list_tests_brief);
GTEST_INTERNAL_PARSE_FLAG(output); GTEST_INTERNAL_PARSE_FLAG(output);
GTEST_INTERNAL_PARSE_FLAG(brief); GTEST_INTERNAL_PARSE_FLAG(brief);
GTEST_INTERNAL_PARSE_FLAG(print_time); GTEST_INTERNAL_PARSE_FLAG(print_time);

View File

@ -96,6 +96,53 @@ MyInstantiation/ValueParamTest\.
""" """
) )
# The expected output when running googletest-list-tests-unittest_ with
# --gtest_list_tests and --gtest_list_tests_brief. The "# TypeParam = ..."
# and "# GetParam() = ..." annotations must be omitted.
EXPECTED_OUTPUT_BRIEF_RE = re.compile(
r"""FooDeathTest\.
Test1
Foo\.
Bar1
Bar2
DISABLED_Bar3
Abc\.
Xyz
Def
FooBar\.
Baz
FooTest\.
Test1
DISABLED_Test2
Test3
TypedTest/0\.
TestA
TestB
TypedTest/1\.
TestA
TestB
TypedTest/2\.
TestA
TestB
My/TypeParamTest/0\.
TestA
TestB
My/TypeParamTest/1\.
TestA
TestB
My/TypeParamTest/2\.
TestA
TestB
MyInstantiation/ValueParamTest\.
TestA/0
TestA/1
TestA/2
TestB/0
TestB/1
TestB/2
"""
)
# The expected output when running googletest-list-tests-unittest_ with # The expected output when running googletest-list-tests-unittest_ with
# --gtest_list_tests and --gtest_filter=Foo*. # --gtest_list_tests and --gtest_filter=Foo*.
EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile( EXPECTED_OUTPUT_FILTER_FOO_RE = re.compile(
@ -220,6 +267,19 @@ class GTestListTestsUnitTest(gtest_test_utils.TestCase):
other_flag='--gtest_filter=Foo*', other_flag='--gtest_filter=Foo*',
) )
def testListTestsBriefOmitsParamAnnotations(self):
"""--gtest_list_tests_brief omits "# GetParam() =" and "# TypeParam ="."""
output = Run(['--gtest_list_tests', '--gtest_list_tests_brief'])
self.assertTrue(
EXPECTED_OUTPUT_BRIEF_RE.match(output),
'output of "--gtest_list_tests --gtest_list_tests_brief" is "%s",\n'
'which does not match regex "%s"'
% (output, EXPECTED_OUTPUT_BRIEF_RE.pattern),
)
self.assertNotIn('GetParam()', output)
self.assertNotIn('TypeParam =', output)
if __name__ == '__main__': if __name__ == '__main__':
gtest_test_utils.Main() gtest_test_utils.Main()

View File

@ -32,6 +32,7 @@
// This file verifies Google Test event listeners receive events at the // This file verifies Google Test event listeners receive events at the
// right times. // right times.
#include <iterator>
#include <string> #include <string>
#include <vector> #include <vector>
@ -498,8 +499,7 @@ int main(int argc, char** argv) {
"1st.OnTestProgramEnd"}; "1st.OnTestProgramEnd"};
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ #endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_
VerifyResults(events, expected_events, VerifyResults(events, expected_events, std::size(expected_events));
sizeof(expected_events) / sizeof(expected_events[0]));
// We need to check manually for ad hoc test failures that happen after // We need to check manually for ad hoc test failures that happen after
// RUN_ALL_TESTS finishes. // RUN_ALL_TESTS finishes.

View File

@ -36,6 +36,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <algorithm> #include <algorithm>
#include <iterator>
#include <string> #include <string>
#include "gtest/gtest-spi.h" #include "gtest/gtest-spi.h"
@ -149,7 +150,7 @@ TEST(LoggingTest, InterleavingLoggingAndAssertions) {
static const int a[4] = {3, 9, 2, 6}; static const int a[4] = {3, 9, 2, 6};
printf("(expecting 2 failures on (3) >= (a[i]))\n"); printf("(expecting 2 failures on (3) >= (a[i]))\n");
for (int i = 0; i < static_cast<int>(sizeof(a) / sizeof(*a)); i++) { for (int i = 0; i < static_cast<int>(std::size(a)); i++) {
printf("i == %d\n", i); printf("i == %d\n", i);
EXPECT_GE(3, a[i]); EXPECT_GE(3, a[i]);
} }

View File

@ -39,6 +39,7 @@
#include <cstdint> #include <cstdint>
#include <functional> #include <functional>
#include <iostream> #include <iostream>
#include <iterator>
#include <list> #include <list>
#include <set> #include <set>
#include <sstream> #include <sstream>
@ -737,10 +738,7 @@ const int test_generation_params[] = {36, 42, 72};
class TestGenerationTest : public TestWithParam<int> { class TestGenerationTest : public TestWithParam<int> {
public: public:
enum { enum { PARAMETER_COUNT = std::size(test_generation_params) };
PARAMETER_COUNT =
sizeof(test_generation_params) / sizeof(test_generation_params[0])
};
typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment; typedef TestGenerationEnvironment<PARAMETER_COUNT> Environment;

View File

@ -39,6 +39,7 @@
#include <deque> #include <deque>
#include <forward_list> #include <forward_list>
#include <functional> #include <functional>
#include <iterator>
#include <limits> #include <limits>
#include <list> #include <list>
#include <map> #include <map>
@ -1784,7 +1785,7 @@ TEST(IsValidUTF8Test, IllFormedUTF8) {
// too. // too.
{"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}}; {"\xEE\x80\x80", "\"\\xEE\\x80\\x80\"\n As Text: \"\""}};
for (int i = 0; i < int(sizeof(kTestdata) / sizeof(kTestdata[0])); ++i) { for (int i = 0; i < int(std::size(kTestdata)); ++i) {
EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]); EXPECT_PRINT_TO_STRING_(kTestdata[i][0], kTestdata[i][1]);
} }
} }

View File

@ -33,6 +33,8 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <iterator>
// Verifies that the command line flag variables can be accessed in // Verifies that the command line flag variables can be accessed in
// code once "gtest.h" has been #included. // code once "gtest.h" has been #included.
// Do not move it after other gtest #includes. // Do not move it after other gtest #includes.
@ -5853,9 +5855,8 @@ class ParseFlagsTest : public Test {
// to specify the array sizes. // to specify the array sizes.
#define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \ #define GTEST_TEST_PARSING_FLAGS_(argv1, argv2, expected, should_print_help) \
TestParsingFlags(sizeof(argv1) / sizeof(*argv1) - 1, argv1, \ TestParsingFlags(std::size(argv1) - 1, argv1, std::size(argv2) - 1, argv2, \
sizeof(argv2) / sizeof(*argv2) - 1, argv2, expected, \ expected, should_print_help)
should_print_help)
}; };
// Tests parsing an empty command line. // Tests parsing an empty command line.