TestPartResult Internal Architecture in Google Test: How Test Outcomes Are Represented
The TestPartResult class in Google Test encapsulates every assertion result using a four-state Type enum and metadata fields, enabling the framework to distinguish between success, non-fatal failures, fatal failures, and skipped test parts.
The TestPartResult class serves as the fundamental data structure within the google/googletest framework for capturing the outcome of individual test parts. Whether an assertion succeeds, fails fatally, fails non-fatally, or is skipped, the framework instantiates a TestPartResult object to record the specific outcome along with source location and diagnostic information.
Core Components of the TestPartResult Class
The TestPartResult class is defined in googletest/include/gtest/gtest-test-part.h (lines 57-109). It combines a strongly-typed enumeration with contextual metadata to provide a complete snapshot of any assertion's execution.
The Type Enum and Outcome Classification
At the heart of TestPartResult lies the Type enumeration (lines 57-64), which defines four distinct states:
kSuccess: The assertion evaluated successfully.kNonFatalFailure: The assertion failed but execution continues (e.g.,EXPECT_*macros).kFatalFailure: The assertion failed and aborted the current test (e.g.,ASSERT_*macros).kSkip: The test part was intentionally skipped viaGTEST_SKIP().
The constructor (lines 66-71) captures this type alongside the source file name, line number, summary string, and full message.
Metadata and Diagnostic Accessors
Each TestPartResult stores precise location and message data:
file_name()andline_number()(lines 80-88): Return the source location, ornullptrand-1if unknown.summary()andmessage()(lines 90-94): Provide human-readable diagnostics. The summary is a truncated version generated by the staticExtractSummaryhelper (lines 15-16) that removes stack traces, whilemessage()contains the complete diagnostic text.
Convenience Predicates
Rather than comparing enum values directly, the class exposes boolean accessors (lines 96-109): passed(), failed(), nonfatally_failed(), fatally_failed(), and skipped(). These predicates simplify conditional logic when processing test results programmatically.
How TestPartResult Represents Different Test Outcomes
The framework uses the type_ field to determine flow control and reporting behavior for each test part.
Success: When type_ == kSuccess, passed() returns true. No failure message is stored, and execution proceeds to the next statement.
Non-fatal Failure: When type_ == kNonFatalFailure, both failed() and nonfatally_failed() return true. The test continues executing subsequent assertions, but the failure is recorded in the TestPartResultArray.
Fatal Failure: When type_ == kFatalFailure, fatally_failed() and failed() are true. The current test method terminates immediately after the TestPartResult is reported and stored.
Skip: When type_ == kSkip, skipped() returns true. The framework marks the test part as intentionally bypassed, recording file and line information but no failure diagnostics.
Integration with the Google Test Framework
Individual TestPartResult objects do not exist in isolation; they are collected, stored, and reported through specific framework mechanisms.
TestPartResultArray Collection
As defined in googletest/include/gtest/gtest-test-part.h (lines 31-49), the TestPartResultArray class manages a sequence of TestPartResult objects. This array is owned by TestResult (declared in googletest/include/gtest/gtest.h), which aggregates all parts for a single test case. You can query the array via TestResult::test_part_results() or access individual entries through TestResult::GetTestPartResult().
Reporting via TestPartResultReporterInterface
When an assertion macro executes, it creates a TestPartResult and dispatches it through the TestPartResultReporterInterface (lines 55-61). The default implementation, DefaultGlobalTestPartResultReporter (implemented in googletest/src/gtest-internal-inl.h), receives the object via ReportTestPartResult and appends it to the current thread's TestResult. This decouples assertion evaluation from result storage, enabling custom reporters to intercept outcomes without modifying core logic.
Practical Example: Accessing TestPartResult Objects
Below are examples demonstrating how assertions generate TestPartResult instances internally, and how to inspect these objects programmatically.
First, basic assertions create different outcome types automatically:
TEST(MathTest, SimpleAssertions) {
EXPECT_EQ(2 + 2, 4); // Generates TestPartResult with kSuccess
EXPECT_TRUE(false); // Generates kNonFatalFailure (test continues)
ASSERT_TRUE(false); // Generates kFatalFailure (test aborts here)
GTEST_SKIP(); // Generates kSkip
}
Second, you can iterate through results to analyze outcomes programmatically:
TEST(FailureInspection, RetrieveResults) {
EXPECT_EQ(1, 2); // Non-fatal failure
EXPECT_TRUE(true); // Success
const ::testing::TestResult& tr = ::testing::UnitTest::GetInstance()
->current_test_info()
->result();
for (int i = 0; i < tr.total_part_count(); ++i) {
const ::testing::TestPartResult& r = tr.GetTestPartResult(i);
std::cout << "Part " << i << ": "
<< (r.passed() ? "PASSED" : "FAILED")
<< " (" << r.summary() << ")\n";
}
}
Summary
- TestPartResult is the core value object in Google Test that captures the outcome of every individual assertion or test part.
- The
Typeenum defines four distinct states:kSuccess,kNonFatalFailure,kFatalFailure, andkSkip. - Metadata fields store source location (
file_name,line_number) and diagnostic messages (summary,message), with the latter processed by theExtractSummaryutility. - Boolean predicates (
passed(),failed(),fatally_failed(), etc.) provide type-safe outcome inspection. TestPartResultArrayaggregates results withinTestResult, whileTestPartResultReporterInterfacehandles the dispatch of results to storage and output sinks.
Frequently Asked Questions
What are the four possible outcomes of a TestPartResult in Google Test?
The four outcomes are defined in the Type enum within gtest-test-part.h: kSuccess for passing assertions, kNonFatalFailure for failed EXPECT_* macros that allow execution to continue, kFatalFailure for failed ASSERT_* macros that abort the test, and kSkip for test parts bypassed via GTEST_SKIP().
How does Google Test distinguish between fatal and non-fatal failures?
Google Test distinguishes these through the Type enum value stored in TestPartResult. Non-fatal failures use kNonFatalFailure, causing nonfatally_failed() to return true while allowing the test to continue. Fatal failures use kFatalFailure, causing fatally_failed() to return true and triggering immediate test termination via exception-based or longjmp-based stack unwalling (platform dependent).
Where is TestPartResult defined in the Google Test source code?
The class is defined in googletest/include/gtest/gtest-test-part.h in the google/googletest repository. This header also defines the TestPartResultArray container and TestPartResultReporterInterface, while the implementation of default reporters resides in googletest/src/gtest-internal-inl.h.
Can I access TestPartResult objects programmatically in my test code?
Yes. During test execution, you can obtain the current TestResult via ::testing::UnitTest::GetInstance()->current_test_info()->result(), then iterate through its parts using total_part_count() and GetTestPartResult(index). Each returned TestPartResult provides accessors like passed(), failed(), file_name(), line_number(), and summary() for inspection.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →