您的位置:首页 > 编程语言 > Go语言

尝试使用google test

2015-12-02 02:49 423 查看
google test 是一个开源代码测试框架,主要用于单元测试。代码可以从

https://github.com/google/googletest

获得。

下面以一个简单的例子来说明如何使用google test.

1. 编译源代码。

google test支持使用 cmake 编译和 autotools 编译。在使用后者的时候,需要先  运行

 autoreconf -fvi

生成 configure文件,然后就可以按照通常的步骤编译了。

google test代码包括两部分: google mock 和 google test.

基本上,使用 google mock就行了, 因为,编译 google mock的时候,也会生成 google test的库。

:~/code/googletest/googlemock/cmake$ find . -name "*.a"
./libgmock.a
./gtest/libgtest.a
./gtest/libgtest_main.a
./libgmock_main.a


2. 编写待测试的代码:

~/code/gtest_example/example$ tree src/
src/
├── Addition.cpp
├── Addition.hpp
├── ExampleApp.cpp
├── Makefile
├── Multiply.cpp
└── Multiply.hpp

0 directories, 6 files
$ cat Addition.hpp
#ifndef _ADDITION_HPP_
#define _ADDITION_HPP_
class Addition
{
public:
static int twoValues(const int x, const int y);
};
#endif


$ cat Addition.hpp
#ifndef _ADDITION_HPP_
#define _ADDITION_HPP_
class Addition
{
public:
static int twoValues(const int x, const int y);
};
#endifcharles@xiaotao:~/code/gtest_example/example/src$ cat Addition.cpp
#include "Addition.hpp"

int
Addition::twoValues(const int x, const int y)
{
return x + y;
}

$ cat Multiply.hpp
#ifndef _MULTIPLY_HPP_
#define _MULTIPLY_HPP_
class Multiply
{
public:
static int twoValues(const int x, const int y);
};
#endif

$ cat Multiply.cpp
#include "Multiply.hpp"

int
Multiply::twoValues(const int x, const int y)
{
return x * y;
}

$ cat ExampleApp.cpp
#include "Addition.hpp"
#include "Multiply.hpp"

#include <stdio.h>

int main()
{
int x = 4;
int y = 5;

int z1 = Addition::twoValues(x,y);
printf("\nAddition Result: %d\n", z1);

int z2 = Multiply::twoValues(x,y);
printf("Multiply Result: %d\n", z2);

return 0;
}

c$ cat Makefile
CXX = arm-linux-gnueabi-g++
CXXFLAGS = -g
INCS = -I./
OBJS = Addition.o Multiply.o

exampleapp: ExampleApp.cpp $(OBJS)
$(CXX) $(CXXFLAGS) -o exampleapp ExampleApp.cpp $(OBJS)

.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS)

clean:
rm *.o exampleapp gmon.out


3. 编写测试代码:

:~/code/gtest_example/example/test$ tree src/
src/
├── Addition_Test.cpp
├── Addition_Test.o
├── Main_TestAll.cpp
├── Makefile
├── Multiply_Test.cpp
├── Multiply_Test.o
└── testAll

0 directories, 7 files

$ cat Multiply_Test.cpp
#include <limits.h>
#include "gtest/gtest.h"
#include "Multiply.hpp"

class MultiplyTest : public ::testing::Test {
protected:
virtual void SetUp() {
}

virtual void TearDown() {
}
};

TEST_F(MultiplyTest,twoValues){
const int x = 4;
const int y = 5;
Multiply multiply;
EXPECT_EQ(20,multiply.twoValues(x,y));
EXPECT_EQ(6,multiply.twoValues(2,3));
}

$ cat Addition_Test.cpp
#include <limits.h>
#include "gtest/gtest.h"
#include "Addition.hpp"

class AdditionTest : public ::testing::Test {
protected:
virtual void SetUp() {
}

virtual void TearDown() {
// Code here will be called immediately after each test
// (right before the destructor).
}
};

TEST_F(AdditionTest,twoValues){
const int x = 4;
const int y = 5;
Addition addition;
EXPECT_EQ(9,addition.twoValues(x,y));
EXPECT_EQ(5,addition.twoValues(2,3));
}

$ cat Addition_Test.cpp
#include <limits.h>
#include "gtest/gtest.h"
#include "Addition.hpp"

class AdditionTest : public ::testing::Test {
protected:
virtual void SetUp() {
}

virtual void TearDown() {
// Code here will be called immediately after each test
// (right before the destructor).
}
};

TEST_F(AdditionTest,twoValues){
const int x = 4;
const int y = 5;
Addition addition;
EXPECT_EQ(9,addition.twoValues(x,y));
EXPECT_EQ(5,addition.twoValues(2,3));
}

$ cat Main_TestAll.cpp
#include <limits.h>
#include "gtest/gtest.h"

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

$ cat Makefile
CXX = arm-linux-gnueabi-g++
CXXFLAGS = -g -L/home/charles/code/googletest/googlemock/cmake -L/home/charles/code/googletest/googlemock/cmake/gtest -lgtest -lgtest_main -lpthread
INCS = -I../../src -I/home/charles/code/googletest/googletest/include
OBJS = ../../src/Addition.o Addition_Test.o ../../src/Multiply.o Multiply_Test.o

testAll: $(OBJS)
$(CXX)  $(INCS) -o testAll  Main_TestAll.cpp $(OBJS)  $(CXXFLAGS)

.cpp.o:
$(CXX) $(CXXFLAGS) -c $< -o $@ $(INCS)

clean:
rm testAll *.o testAll.xml


运行结果:

#./testAll
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from AdditionTest
[ RUN      ] AdditionTest.twoValues
[       OK ] AdditionTest.twoValues (7 ms)
[----------] 1 test from AdditionTest (11 ms total)

[----------] 1 test from MultiplyTest
[ RUN      ] MultiplyTest.twoValues
[       OK ] MultiplyTest.twoValues (1 ms)
[----------] 1 test from MultiplyTest (2 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (46 ms total)
[  PASSED  ] 2 tests.
按照下面的方式执行,会产生一个包含测试结果的xml文件:
testAll --gtest_output="xml:./testAll.xml"

googletest 支持  TEST(class,test_name) 和 TEST_F(class,test_name). 后一种方式是测试框架方式。

The framework employs a user written test class derived from ::testing::Test which supplies
SetUp() and TearDown() functions.Tests:

ASSERT_XXX(): If assertion fails then processing of test terminate.
EXPECT_XXX(): nonfatal failure, allowing processing to continue.

TestFatalNonFatal
TrueASSERT_TRUE(condition)EXPECT_TRUE(condition)
FalseASSERT_FALSE(condition)EXPECT_FALSE(condition)
EqualASSERT_EQ(arg1,arg2)EXPECT_EQ(arg1,arg2)
Not EqualASSERT_NE(arg1,arg2)EXPECT_NE(arg1,arg2)
Less ThanASSERT_LT(arg1,arg2)EXPECT_LT(arg1,arg2)
Less Than or EqualASSERT_LE(arg1,arg2)EXPECT_LE(arg1,arg2)
Greater ThanASSERT_GT(arg1,arg2)EXPECT_GT(arg1,arg2)
Greater Than or EqualASSERT_GE(arg1,arg2)EXPECT_GE(arg1,arg2)
C String EqualASSERT_STREQ(str1,str2)EXPECT_STREQ(str1,str2)
C String Not EqualASSERT_STRNE(str1,str2)EXPECT_STRNE(str1,str2)
C String Case EqualASSERT_STRCASEEQ(str1,str2)EXPECT_STRCASEEQ(str1,str2)
C String Case Not EqualASSERT_STRCASENE(str1,str2)EXPECT_STRCASENE(str1,str2)
Verify that exception is thrownASSERT_THROW(statement,exception_type)EXPECT_THROW(statement,exception_type)
Verify that exception is thrownASSERT_ANY_THROW(statement)EXPECT_ANY_THROW(statement)
Verify that exception is NOT thrownASSERT_NO_THROW(statement)EXPECT_NO_THROW(statement)
See the
GoogleTest advanced guide for more "EXPECT" and "ASSERT" options.

 References:

1. http://www.yolinux.com/TUTORIALS/Cpp-GoogleTest.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gtest