summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAbseil Team <absl-team@google.com>2023-04-05 13:18:59 -0700
committerCopybara-Service <copybara-worker@google.com>2023-04-05 13:27:21 -0700
commit057b4e904fd754135dc19ff557c14036fd316425 (patch)
tree962f3c25f9d8f7a9d8accbdf87e70e9f05d5f5b0
parent7ee260c54921571b18b15049304426fe151c1265 (diff)
downloadgoogletest-git-057b4e904fd754135dc19ff557c14036fd316425.tar.gz
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: 522136303 Change-Id: If155e2666780af0e514fbbf5ff2b157d5fe2fef1
-rw-r--r--googletest/src/gtest.cc20
1 files changed, 20 insertions, 0 deletions
diff --git a/googletest/src/gtest.cc b/googletest/src/gtest.cc
index 7832492d..15cee3f0 100644
--- a/googletest/src/gtest.cc
+++ b/googletest/src/gtest.cc
@@ -44,6 +44,7 @@
#include <chrono> // NOLINT
#include <cmath>
#include <cstdint>
+#include <cstring>
#include <initializer_list>
#include <iomanip>
#include <ios>
@@ -2981,6 +2982,25 @@ void TestSuite::Run() {
TestEventListener* repeater = UnitTest::GetInstance()->listeners().repeater();
+ // Ensure our tests are in a deterministic order.
+ //
+ // We do this by sorting lexicographically on (file, line number), providing
+ // an order matching what the user can see in the source code.
+ //
+ // In the common case the line number comparison shouldn't be necessary,
+ // because the registrations made by the TEST macro are executed in order
+ // within a translation unit. But this is not true of the manual registration
+ // API, and in more exotic scenarios a single file may be part of multiple
+ // translation units.
+ std::stable_sort(test_info_list_.begin(), test_info_list_.end(),
+ [](const TestInfo* const a, const TestInfo* const b) {
+ if (const int result = std::strcmp(a->file(), b->file())) {
+ return result < 0;
+ }
+
+ return a->line() < b->line();
+ });
+
// Call both legacy and the new API
repeater->OnTestSuiteStart(*this);
// Legacy API is deprecated but still available