Friday, June 15, 2018

googletest

Locate GTEST_ROOT

after you (git) clone google test from master, your dir contains both google test and google mock. googletest is google's suggestion.

GTEST_ROOT may looks like ..\googletest-master\googletest

Locate gtest.sln or makefile to work on

googletest provides build files for some popular build systems: msvc/ for Visual Studio, xcode/ for Mac Xcode, make/ for GNU make, codegear/ for Borland C++ Builder, and the autotools script (deprecated) and CMakeLists.txt for CMake (recommended). In my case, it's under msvc\2010 .

test a project or build target

Created ConsoleApplication1 under the sln, its dir is msvc\ConsoleApplication1.

copy code for the target's int main() func form the project gtest_main.

config the tgt's runtime to "Multi-threaded Debug (/MTd)" //same as gtest

add dependency to gtest(note: in vs2017, add it from project properties menu), and add C:\Temp\Tasks\googletest\test2\googletest-master\googletest\msvc\2010\gtest\Win32-Debug as additional lib dir.

add testing code:

TEST(dummy_case, dummy_test)
{
 //use EXPECT_* when you want the test to continue to reveal more errors after the assertion failure,
 //and use ASSERT_* when continuing after failure doesn't make sense. 
 EXPECT_EQ(3, 3);
 ASSERT_EQ(3, 3);

}

For a c++ class tester

we can create c++ class tester(fixture) public on ::testing::Test; the fixture's tests can use TEST_F.

For each test defined with TEST_F(cls_name,test_name), Google Test will:

Create a fresh test fixture named cls_name 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 tests.

further readings

https://github.com/google/googletest/blob/master/googletest/docs/primer.md

No comments:

Post a Comment