Skip to content

Commit b5fd99b

Browse files
jacobsacopybara-github
authored andcommitted
gtest.cc: run tests within a test suite in a deterministic order.
Ensure that tests are run in the order specified in the source code, even if they are registered manually using RegisterTest. There should be no behavior change for the common case. PiperOrigin-RevId: 520729483 Change-Id: I400c78400c6929fccae0676214d993251f31888f
1 parent 0cd05c6 commit b5fd99b

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

googletest/src/gtest.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <chrono> // NOLINT
4545
#include <cmath>
4646
#include <cstdint>
47+
#include <cstring>
4748
#include <initializer_list>
4849
#include <iomanip>
4950
#include <ios>
@@ -2981,6 +2982,25 @@ void TestSuite::Run() {
29812982

29822983
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
29832984

2985+
// Ensure our tests are in a deterministic order.
2986+
//
2987+
// We do this by sorting lexicographically on (file, line number), providing
2988+
// an order matching what the user can see in the source code.
2989+
//
2990+
// In the common case the line number comparison shouldn't be necessary,
2991+
// because the registrations made by the TEST macro are executed in order
2992+
// within a translation unit. But this is not true of the manual registration
2993+
// API, and in more exotic scenarios a single file may be part of multiple
2994+
// translation units.
2995+
std::stable_sort(test_info_list_.begin(), test_info_list_.end(),
2996+
[](const TestInfo* const a, const TestInfo* const b) {
2997+
if (const int result = std::strcmp(a->file(), a->file())) {
2998+
return result < 0;
2999+
}
3000+
3001+
return a->line() < b->line();
3002+
});
3003+
29843004
// Call both legacy and the new API
29853005
repeater->OnTestSuiteStart(*this);
29863006
// Legacy API is deprecated but still available

0 commit comments

Comments
 (0)