Make tests throw if ASSERT failed
authorCharles Giessen <charles@lunarg.com>
Wed, 15 Mar 2023 23:56:36 +0000 (17:56 -0600)
committerCharles Giessen <46324611+charles-lunarg@users.noreply.github.com>
Mon, 20 Mar 2023 15:35:26 +0000 (09:35 -0600)
This makes the googletest assertions emit an exception whenever an assertion fails. This
prevents tests from continuing after some critical state has been violated. This is
advantageous because it is very easy to forget to put ASSERT_NO_FATAL_FAILURE around every
function which could trigger an error.

tests/loader_testing_main.cpp

index e6caeef..7e37aa0 100644 (file)
 
 #include "test_environment.h"
 
+// Makes any failed assertion throw, allowing for graceful cleanup of resources instead of hard aborts
+class ThrowListener : public testing::EmptyTestEventListener {
+    void OnTestPartResult(const testing::TestPartResult& result) override {
+        if (result.type() == testing::TestPartResult::kFatalFailure) {
+            throw testing::AssertionException(result);
+        }
+    }
+};
+
 int main(int argc, char** argv) {
 #if defined(_WIN32)
     // Avoid "Abort, Retry, Ignore" dialog boxes
@@ -89,6 +98,7 @@ int main(int argc, char** argv) {
     EnvVarWrapper home_env_var{"HOME", "/home/fake_home"};
 #endif
     ::testing::InitGoogleTest(&argc, argv);
+    ::testing::UnitTest::GetInstance()->listeners().Append(new ThrowListener);
     int result = RUN_ALL_TESTS();
 
     return result;