Google C++ Testing Framework AdvancedGuide
2013-12-25 23:10
447 查看
[u]My>Sign
in
Project Home Downloads Wiki Issues Source
Generates>
FAIL(); ADD_FAILURE(); ADD_FAILURE_AT("file_path", line_number); FAIL() generates>switch(expression) {
case 1: ... some checks ...
case 2: ... some other checks
...
default: FAIL() << "We shouldn't get here.";
}
Availability: Linux, Windows, Mac.
Fatal assertion Nonfatal assertion Verifies ASSERT_THROW(statement, exception_type); EXPECT_THROW(statement, exception_type); statement throws an exception of the given type ASSERT_ANY_THROW(statement); EXPECT_ANY_THROW(statement); statement throws an exception of any type ASSERT_NO_THROW(statement); EXPECT_NO_THROW(statement); statement doesn't>ASSERT_THROW(Foo(5), bar_exception);
EXPECT_NO_THROW({
int n = 5;
Bar(&n);
});
Availability: Linux, Windows, Mac;>Predicate Assertions for Better Error Messages
Even>Using an Existing Boolean Function
If>
Fatal assertion Nonfatal assertion Verifies ASSERT_PRED1(pred1, val1); EXPECT_PRED1(pred1, val1); pred1(val1) returns true ASSERT_PRED2(pred2, val1, val2); EXPECT_PRED2(pred2, val1, val2); pred2(val1, val2) returns true ... ... ... In>// Returns true iff m and n have no common divisors except 1.
bool MutuallyPrime(int m, int n) { ... }
const int a = 3;
const int b = 4;
const int c = 10;
the>this for
>
Availability: Linux, Windows, Mac
// Returns an AssertionResult object to indicate that an assertion has
// succeeded.
AssertionResult AssertionSuccess();
// Returns an AssertionResult object to indicate that an assertion has
// failed.
AssertionResult AssertionFailure();
}
You>::testing::AssertionResult IsEven(int n) {
if ((n % 2) == 0)
return ::testing::AssertionSuccess();
else
return ::testing::AssertionFailure() << n << " is odd";
}
instead>bool IsEven(int n) {
return (n % 2) == 0;
}
the>::testing::AssertionResult IsEven(int n) {
if ((n % 2) == 0)
return ::testing::AssertionSuccess() << n << " is even";
else
return ::testing::AssertionFailure() << n << " is odd";
}
Then>Using a Predicate-Formatter
If>
Fatal assertion Nonfatal assertion Verifies ASSERT_PRED_FORMAT1(pred_format1, val1); EXPECT_PRED_FORMAT1(pred_format1, val1`); pred_format1(val1) is successful ASSERT_PRED_FORMAT2(pred_format2, val1, val2); EXPECT_PRED_FORMAT2(pred_format2, val1, val2); pred_format2(val1, val2) is successful ... ... ... The>// Returns the smallest prime common divisor of m and n,
// or 1 when m and n are mutually prime.
int SmallestPrimeCommonDivisor(int m, int n) { ... }
// A predicate-formatter for asserting that two integers are mutually prime.
::testing::AssertionResult AssertMutuallyPrime(const char* m_expr,
const char* n_expr,
int m,
int n) {
if (MutuallyPrime(m, n))
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< m_expr << " and " << n_expr << " (" << m << " and " << n
<< ") are not mutually prime, " << "as they have a common divisor "
<< SmallestPrimeCommonDivisor(m, n);
}
With>EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c);
to>Floating-Point Comparison
Comparing>this article on float comparison.
By "almost equal", we mean the two values are within 4 ULP's from each other.
The following assertions allow you to choose the acceptable error bound:
in
googletest Google C++ Testing Framework |
Search Search within: All wiki pages Featured pages Current pages Deprecated pages for |
AdvancedGuide Updated Jul 23, 2013 by w...@google.com r#summary Advanced>More Assertions Explicit Success and Failure Exception Assertions Predicate Assertions for Better Error Messages Using an Existing Boolean Function Using a Function That Returns an AssertionResult Using a Predicate-Formatter Floating-Point Comparison Floating-Point Macros Floating-Point Predicate-Format Functions Windows HRESULT assertions Type Assertions Assertion Placement Teaching Google Test How to Print Your Values Death Tests How to Write a Death Test Regular Expression Syntax How It Works Death Tests And Threads Death Test Styles Caveats Using Assertions in Sub-routines Adding Traces to Assertions Propagating Fatal Failures Asserting on Subroutines Checking for Failures in the Current Test Logging Additional Information Sharing Resources Between Tests in the Same Test Case Global Set-Up and Tear-Down Value Parameterized Tests How to Write Value-Parameterized Tests Creating Value-Parameterized Abstract Tests Typed Tests Type-Parameterized Tests Testing Private Code Static Functions Private Class Members Catching Failures Getting the Current Test's Name Extending Google Test by Handling Test Events Defining Event Listeners Using Event Listeners Generating Failures in Listeners Running Test Programs: Advanced Options Selecting Tests Listing Test Names Running a Subset of the Tests Temporarily Disabling Tests Temporarily Enabling Disabled Tests Repeating the Tests Shuffling the Tests Controlling Test Output Colored Terminal Output Suppressing the Elapsed Time Generating an XML Report Controlling How Failures Are Reported Turning Assertion Failures into Break-Points Disabling Catching Test-Thrown Exceptions Letting Another Testing Framework Drive Distributing Test Functions to Multiple Machines Fusing Google Test Source Files Where to Go from Here Now>Primer and>More Assertions This>Explicit Success and Failure These> |
SUCCEED(); |
case 1: ... some checks ...
case 2: ... some other checks
...
default: FAIL() << "We shouldn't get here.";
}
Availability: Linux, Windows, Mac.
Exception Assertions
These>EXPECT_NO_THROW({
int n = 5;
Bar(&n);
});
Availability: Linux, Windows, Mac;>Predicate Assertions for Better Error Messages
Even>Using an Existing Boolean Function
If>
bool MutuallyPrime(int m, int n) { ... }
const int a = 3;
const int b = 4;
const int c = 10;
the>this for
>
Availability: Linux, Windows, Mac
Using a Function That Returns an AssertionResult
While EXPECT_PRED*() and>namespace testing {// Returns an AssertionResult object to indicate that an assertion has
// succeeded.
AssertionResult AssertionSuccess();
// Returns an AssertionResult object to indicate that an assertion has
// failed.
AssertionResult AssertionFailure();
}
You>::testing::AssertionResult IsEven(int n) {
if ((n % 2) == 0)
return ::testing::AssertionSuccess();
else
return ::testing::AssertionFailure() << n << " is odd";
}
instead>bool IsEven(int n) {
return (n % 2) == 0;
}
the>::testing::AssertionResult IsEven(int n) {
if ((n % 2) == 0)
return ::testing::AssertionSuccess() << n << " is even";
else
return ::testing::AssertionFailure() << n << " is odd";
}
Then>Using a Predicate-Formatter
If>
// or 1 when m and n are mutually prime.
int SmallestPrimeCommonDivisor(int m, int n) { ... }
// A predicate-formatter for asserting that two integers are mutually prime.
::testing::AssertionResult AssertMutuallyPrime(const char* m_expr,
const char* n_expr,
int m,
int n) {
if (MutuallyPrime(m, n))
return ::testing::AssertionSuccess();
return ::testing::AssertionFailure()
<< m_expr << " and " << n_expr << " (" << m << " and " << n
<< ") are not mutually prime, " << "as they have a common divisor "
<< SmallestPrimeCommonDivisor(m, n);
}
With>EXPECT_PRED_FORMAT2(AssertMutuallyPrime, b, c);
to>Floating-Point Comparison
Comparing>this article on float comparison.
Floating-Point Macros
Fatal assertion | Nonfatal assertion | Verifies |
ASSERT_FLOAT_EQ(expected, actual); | EXPECT_FLOAT_EQ(expected, actual); | the two float values are almost equal |
ASSERT_DOUBLE_EQ(expected, actual); | EXPECT_DOUBLE_EQ(expected, actual); | the two double values are almost equal |
The following assertions allow you to choose the acceptable error bound:
Fatal assertion | Nonfatal assertion | Verifies | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
ASSERT_NEAR(val1, val2, abs_error); | EXPECT_NEAR(val1, val2, abs_error); | the difference between val1 and val2 doesn't>Floating-Point Predicate-Format Functions Some>EXPECT_PRED_FORMAT2(::testing::FloatLE, val1, val2); EXPECT_PRED_FORMAT2(::testing::DoubleLE, val1, val2); Verifies>Windows HRESULT assertions These assertions test for HRESULT success or failure.
|