These assertions do basic true/false condition testing.ide
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_TRUE( condition) ; |
EXPECT_TRUE( condition) ; |
condition is true |
ASSERT_FALSE( condition) ; |
EXPECT_FALSE( condition) ; |
condition is false |
Remember, when they fail, ASSERT_*
yields a fatal failure and
returns from the current function, while EXPECT_*
yields a nonfatal
failure, allowing the function to continue running. In either case, an
assertion failure means its containing test fails.ui
Availability: Linux, Windows, Mac.this
This section describes assertions that compare two values.spa
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_EQ( val1, val2); |
EXPECT_EQ( val1, val2); |
val1 == val2 |
ASSERT_NE( val1, val2); |
EXPECT_NE( val1, val2); |
val1 != val2 |
ASSERT_LT( val1, val2); |
EXPECT_LT( val1, val2); |
val1 < val2 |
ASSERT_LE( val1, val2); |
EXPECT_LE( val1, val2); |
val1 <= val2 |
ASSERT_GT( val1, val2); |
EXPECT_GT( val1, val2); |
val1 > val2 |
ASSERT_GE( val1, val2); |
EXPECT_GE( val1, val2); |
val1 >= val2 |
In the event of a failure, Google Test prints both val1 and val2.code
The assertions in this group compare two C strings. If you want to compare
two string
objects, use EXPECT_EQ
, EXPECT_NE
, and etc instead.教程
Fatal assertion | Nonfatal assertion | Verifies |
---|---|---|
ASSERT_STREQ( str1, str2); |
EXPECT_STREQ( str1, _str_2); |
the two C strings have the same content |
ASSERT_STRNE( str1, str2); |
EXPECT_STRNE( str1, str2); |
the two C strings have different content |
ASSERT_STRCASEEQ( str1, str2); |
EXPECT_STRCASEEQ( str1, str2); |
the two C strings have the same content, ignoring case |
ASSERT_STRCASENE( str1, str2); |
EXPECT_STRCASENE( str1, str2); |
the two C strings have different content, ignoring case |
To create a test:ip
TEST()
macro to define and name a test function, These are ordinary C++ functions that don’t return a value.TEST(test_case_name, test_name) { ... test body ...}
A test case for this function might look like:ci
// Tests factorial of 0.TEST(FactorialTest, HandlesZeroInput) { EXPECT_EQ(1, Factorial(0));}// Tests factorial of positive numbers.TEST(FactorialTest, HandlesPositiveInput) { EXPECT_EQ(1, Factorial(1)); EXPECT_EQ(2, Factorial(2)); EXPECT_EQ(6, Factorial(3)); EXPECT_EQ(40320, Factorial(8));}
As an example, let’s write tests for a FIFO queue class named Queue
, which
has the following interface:element
template <typename E> // E is the element type.class Queue { public: Queue(); void Enqueue(const E& element); E* Dequeue(); // Returns NULL if the queue is empty. size_t size() const; ...};
First, define a fixture class. By convention, you should give it the name
FooTest
where Foo
is the class being tested.rem
class QueueTest : public ::testing::Test { protected: virtual void SetUp() { q1_.Enqueue(1); q2_.Enqueue(2); q2_.Enqueue(3); } // virtual void TearDown() {} Queue<int> q0_; Queue<int> q1_; Queue<int> q2_;};
In this case, TearDown()
is not needed since we don’t have to clean up after
each test, other than what’s already done by the destructor.
Now we’ll write tests using TEST_F()
and this fixture.
TEST_F(QueueTest, IsEmptyInitially) { EXPECT_EQ(0, q0_.size());}TEST_F(QueueTest, DequeueWorks) { int* n = q0_.Dequeue(); EXPECT_EQ(NULL, n); n = q1_.Dequeue(); ASSERT_TRUE(n != NULL); EXPECT_EQ(1, *n); EXPECT_EQ(0, q1_.size()); delete n; n = q2_.Dequeue(); ASSERT_TRUE(n != NULL); EXPECT_EQ(2, *n); EXPECT_EQ(1, q2_.size()); delete n;}
TEST()
and TEST_F()
implicitly register their tests with Google Test. So, unlike with many other C++ testing frameworks, you don’t have to re-list all your defined tests in order to run them.
After defining your tests, you can run them with RUN_ALL_TESTS()
, which returns 0
if all the tests are successful, or 1
otherwise. Note that RUN_ALL_TESTS()
runs all tests in your link unit – they can be from different test cases, or even different source files.
When invoked, the RUN_ALL_TESTS()
macro:
SetUp()
.TearDown()
.In addition, if the text fixture’s constructor generates a fatal failure in
step 2, there is no point for step 3 - 5 and they are thus skipped. Similarly,
if step 3 generates a fatal failure, step 4 will be skipped.
Important: You must not ignore the return value of RUN_ALL_TESTS()
, or gcc
will give you a compiler error. The rationale for this design is that the
automated testing service determines whether a test has passed based on its
exit code, not on its stdout/stderr output; thus your main()
function must
return the value of RUN_ALL_TESTS()
.
Also, you should call RUN_ALL_TESTS()
only once. Calling it more than once
conflicts with some advanced Google Test features (e.g. thread-safe death
tests) and thus is not supported.
You can start from this boilerplate:
#include "this/package/foo.h"#include "gtest/gtest.h"namespace {// The fixture for testing class Foo.class FooTest : public ::testing::Test { protected: // You can remove any or all of the following functions if its body // is empty. FooTest() { // You can do set-up work for each test here. } virtual ~FooTest() { // You can do clean-up work that doesn't throw exceptions here. } // If the constructor and destructor are not enough for setting up // and cleaning up each test, you can define the following methods: virtual void SetUp() { // Code here will be called immediately after the constructor (right // before each test). } virtual void TearDown() { // Code here will be called immediately after each test (right // before the destructor). } // Objects declared here can be used by all tests in the test case for Foo.};// Tests that the Foo::Bar() method does Abc.TEST_F(FooTest, MethodBarDoesAbc) { const string input_filepath = "this/package/testdata/myinputfile.dat"; const string output_filepath = "this/package/testdata/myoutputfile.dat"; Foo f; EXPECT_EQ(0, f.Bar(input_filepath, output_filepath));}// Tests that Foo does Xyz.TEST_F(FooTest, DoesXyz) { // Exercises the Xyz feature of Foo.}} // namespaceint main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();}
The ::testing::InitGoogleTest()
function parses the command line for Google
Test flags, and removes all recognized flags. This allows the user to control a
test program’s behavior via various flags, which we’ll cover in AdvancedGuide.
You must call this function before calling RUN_ALL_TESTS()
, or the flags
won’t be properly initialized.
On Windows, InitGoogleTest()
also works with wide strings, so it can be used
in programs compiled in UNICODE
mode as well.
But maybe you think that writing all those main() functions is too much work? We agree with you completely and that’s why Google Test provides a basic implementation of main(). If it fits your needs, then just link your test with gtest_main library and you are good to go.