gtestのテストプログラムフレームワーク
3836 ワード
from gtest primer
基本概念
テストプログラムのフレームワーク:test program
-test case 1
-test1
-ASSERT_*
-EXPECT_*
-test2
-test case 2
-test1
-test3
-test case 3
-test4
test program
-test case 1
-test1
-ASSERT_*
-EXPECT_*
-test2
-test case 2
-test1
-test3
-test case 3
-test4
次はaddプログラムをテストするテストプログラムです.
#include
extern int add(int a, int b);
TEST(testCase, test0)
{
EXPECT_EQ(add(2, 3), 5);
}
TEST(testCase, test1)
{
EXPECT_EQ(add(2, 2), 4);
}
TEST(testCaseAnother, test1)
{
EXPECT_EQ(add(-2, 2), 0);
}
int main(int argc, char **argv)
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
アサーション
assertionには2つの断言が含まれています.
以下を参照してください.https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#assertions
EXPECT_EQ(add(2, 3), 5);
は断言です.テストケース
テスト例は関数と類似しており、文とassertionから構成されています.
テストケース
いくつかの機能が類似しているテスト例は、テストキットを構成します.
TEST(testCase, test0)
{
EXPECT_EQ(add(2, 3), 5);
}
testCase
というテストセットに属し、test 0という名前のテスト例です.試験固定装置(test fixtures)
いくつかの試験例が1セットの試験構成を共用したい場合は、試験固定装置、すなわち
::testing::Test
に割り当てられたクラスを使用することができる.以下を参照してください.https://github.com/google/googletest/blob/master/googletest/docs/Primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests
例
https://github.com/sunday1103/Notebook/tree/master/library/gtest/proj #include
#include
extern int add(int a, int b);
class addTest : public ::testing::Test {
protected:
addTest()
{
a = 10;
}
virtual void SetUp()
{
a++;
std::cout << "enter SetUp" << std::endl;
std::cout << "a is: "<< a << std::endl;
}
virtual void TearDown()
{
std::cout << "enter TearDown" << std::endl;
std::cout << "a is: "<< a << std::endl;
std::cout << std::endl;
}
int a;
};
TEST_F(addTest, test0)
{
EXPECT_EQ(add(2, 3), 5);
}
TEST_F(addTest, test1)
{
EXPECT_EQ(add(2, 2), 4);
}
TEST_F(addTest, test2)
{
EXPECT_EQ(add(2, 22), 24);
}
実行結果:huangyang@huangyang-desktop:~/Notebook$ cd library/gtest/proj/
huangyang@huangyang-desktop:~/Notebook/library/gtest/proj$ make
g++ *.cc -lgtest -lpthread
huangyang@huangyang-desktop:~/Notebook/library/gtest/proj$ ./a.out
[==========] Running 6 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from testCase
[ RUN ] testCase.test0
[ OK ] testCase.test0 (0 ms)
[ RUN ] testCase.test1
[ OK ] testCase.test1 (0 ms)
[----------] 2 tests from testCase (0 ms total)
[----------] 1 test from testCaseAnother
[ RUN ] testCaseAnother.test1
[ OK ] testCaseAnother.test1 (0 ms)
[----------] 1 test from testCaseAnother (0 ms total)
[----------] 3 tests from addTest
[ RUN ] addTest.test0
enter SetUp
a is: 11
enter TearDown
a is: 11
[ OK ] addTest.test0 (0 ms)
[ RUN ] addTest.test1
enter SetUp
a is: 11
enter TearDown
a is: 11
[ OK ] addTest.test1 (0 ms)
[ RUN ] addTest.test2
enter SetUp
a is: 11
enter TearDown
a is: 11
[ OK ] addTest.test2 (0 ms)
[----------] 3 tests from addTest (0 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 3 test cases ran. (0 ms total)
[ PASSED ] 6 tests.
huangyang@huangyang-desktop:~/Notebook/library/gtest/proj$
#include
#include
extern int add(int a, int b);
class addTest : public ::testing::Test {
protected:
addTest()
{
a = 10;
}
virtual void SetUp()
{
a++;
std::cout << "enter SetUp" << std::endl;
std::cout << "a is: "<< a << std::endl;
}
virtual void TearDown()
{
std::cout << "enter TearDown" << std::endl;
std::cout << "a is: "<< a << std::endl;
std::cout << std::endl;
}
int a;
};
TEST_F(addTest, test0)
{
EXPECT_EQ(add(2, 3), 5);
}
TEST_F(addTest, test1)
{
EXPECT_EQ(add(2, 2), 4);
}
TEST_F(addTest, test2)
{
EXPECT_EQ(add(2, 22), 24);
}
huangyang@huangyang-desktop:~/Notebook$ cd library/gtest/proj/
huangyang@huangyang-desktop:~/Notebook/library/gtest/proj$ make
g++ *.cc -lgtest -lpthread
huangyang@huangyang-desktop:~/Notebook/library/gtest/proj$ ./a.out
[==========] Running 6 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from testCase
[ RUN ] testCase.test0
[ OK ] testCase.test0 (0 ms)
[ RUN ] testCase.test1
[ OK ] testCase.test1 (0 ms)
[----------] 2 tests from testCase (0 ms total)
[----------] 1 test from testCaseAnother
[ RUN ] testCaseAnother.test1
[ OK ] testCaseAnother.test1 (0 ms)
[----------] 1 test from testCaseAnother (0 ms total)
[----------] 3 tests from addTest
[ RUN ] addTest.test0
enter SetUp
a is: 11
enter TearDown
a is: 11
[ OK ] addTest.test0 (0 ms)
[ RUN ] addTest.test1
enter SetUp
a is: 11
enter TearDown
a is: 11
[ OK ] addTest.test1 (0 ms)
[ RUN ] addTest.test2
enter SetUp
a is: 11
enter TearDown
a is: 11
[ OK ] addTest.test2 (0 ms)
[----------] 3 tests from addTest (0 ms total)
[----------] Global test environment tear-down
[==========] 6 tests from 3 test cases ran. (0 ms total)
[ PASSED ] 6 tests.
huangyang@huangyang-desktop:~/Notebook/library/gtest/proj$