0. 杂问杂记
Very good articles about GoogleTest/Hudson/GCC:
http://meekrosoft.wordpress.com/2010/06/02/continuous-code-coverage-with-gcc-googletest-and-hudson/
Q:如何指定某些Tests被执行?(比如,类似TestNG中的自定义Suite概念?)
1. Primer: Getting started with Google Test
* Basic Concepts
Test
Assertions
Test Case
Fixture
*Binary Comparison
可对自定义类使用,前提是定义相关的操作符,如,operation ==() , operator <() 等等。
Arguments are always evaluated exactly once. Therefore, it's OK for the arguments to have side effects. However, as with any ordinary C/C++ function, the arguments' evaluation order is undefined (i.e. the compiler is free to choose any order) and your code should not depend on any particular argument evaluation order.
ASSERT_EQ() does pointer equality on pointers. If used on two C strings, it tests if they are in the same memory location, not if they have the same value. Therefore, if you want to compare C strings (e.g. const char*) by value, use ASSERT_STREQ()
* Fixture
将数个Test共用数据放在一起
class XXXFixture : public ::testing::Test {
public:
SetUp() {}
TearDown() {}
...
};
TEST_F(XXXFixture, test-name)
What's happending inside GTest ?
- Create a fresh test fixture at runtime
- Immediately initialize it via SetUp() ,
- Run the test
- Clean up by calling TearDown()
- Delete the test fixture. Note that different tests in the same test case have different test fixture objects, and Google Test always deletes a test fixture before it creates the next one. Google Test does not reuse the same test fixture for multiple tests. Any changes one test makes to the fixture do not affect other test
* Invoking testings
TEST()/TEST_F()被隐式注册,RUN_ALL_TEST()会发现它们
RUN_ALL_TEST()返回值需要作为main()函数的反回值
Q:如何指定某些Tests被执行?(比如,类似TestNG中的自定义Suite概念?)
* 一个样例测试
#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.
}
} // namespace
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}