gtest测试框架使用详解_vstest和gtest比较

    很早就接触了google C/C++自动化测试框架gtest, 现在偶然用起有时候还需要简单温习下步骤。今天在这里做下简单使用记录。

1. 使用版本 

gtest-1.7.0

2. gtest 安装

./configure

make

3. 测试自带的用例

gtest工具自带的测试用例放在了目录samples下 [wln@localhost gtest-1.7.0]$ cd samples/

[wln@localhost samples]$ ll

总计 148

-rw-rw-r– 1 wln wln  887 09-25 16:57 libsamples.la

-r–r–r– 1 wln wln 4072 2013-09-19 prime_tables.h

-r–r–r– 1 wln wln 5053 2013-09-19 sample10_unittest.cc

-r–r–r– 1 wln wln 2502 2013-09-19 sample1.cc

-r–r–r– 1 wln wln 1937 2013-09-19 sample1.h

-rw-rw-r– 1 wln wln  303 09-25 16:57 sample1.lo

-rw-rw-r– 1 wln wln 3004 09-25 16:57 sample1.o

-r–r–r– 1 wln wln 5129 2013-09-19 sample1_unittest.cc

-r–r–r– 1 wln wln 2298 2013-09-19 sample2.cc

-r–r–r– 1 wln wln 3006 2013-09-19 sample2.h

-rw-rw-r– 1 wln wln  303 09-25 16:57 sample2.lo

-rw-rw-r– 1 wln wln 4236 09-25 16:57 sample2.o

-r–r–r– 1 wln wln 3926 2013-09-19 sample2_unittest.cc

-r–r–r– 1 wln wln 5365 2013-09-19 sample3-inl.h

-r–r–r– 1 wln wln 5351 2013-09-19 sample3_unittest.cc

-r–r–r– 1 wln wln 1927 2013-09-19 sample4.cc

-r–r–r– 1 wln wln 2083 2013-09-19 sample4.h

-rw-rw-r– 1 wln wln  303 09-25 16:57 sample4.lo

-rw-rw-r– 1 wln wln 5028 09-25 16:57 sample4.o

-r–r–r– 1 wln wln 1909 2013-09-19 sample4_unittest.cc

-r–r–r– 1 wln wln 6590 2013-09-19 sample5_unittest.cc

-r–r–r– 1 wln wln 8989 2013-09-19 sample6_unittest.cc

-r–r–r– 1 wln wln 5099 2013-09-19 sample7_unittest.cc

-r–r–r– 1 wln wln 6944 2013-09-19 sample8_unittest.cc

-r–r–r– 1 wln wln 5951 2013-09-19 sample9_unittest.cc

进入make目录下 [wln@localhost make]$ ll

总计 4

-r–r–r– 1 wln wln 2753 2013-09-19 Makefile
执行操作

[wln@localhost make]$ make

g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1.cc

g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/sample1_unittest.cc

g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \

            ../src/gtest-all.cc

In file included from ../src/gtest-all.cc:43:

../src/gtest.cc:370: 警告:成员‘testing::internal::MutexBase::owner_’缺少初始值设定

g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \

            ../src/gtest_main.cc

ar rv gtest_main.a gtest-all.o gtest_main.o

ar: creating gtest_main.a

a – gtest-all.o

a – gtest_main.o

g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest

[wln@localhost make]$ ll

总计 5192

-rw-rw-r– 1 wln wln 1797004 09-25 16:57 gtest-all.o

-rw-rw-r– 1 wln wln 1951104 09-25 16:57 gtest_main.a

-rw-rw-r– 1 wln wln   48284 09-25 16:57 gtest_main.o

-r–r–r– 1 wln wln    2753 2013-09-19 Makefile

-rw-rw-r– 1 wln wln    2684 09-25 16:57 sample1.o

-rwxrwxr-x 1 wln wln 1289739 09-25 16:57 sample1_unittest

-rw-rw-r– 1 wln wln  197680 09-25 16:57 sample1_unittest.o
进行测试

[wln@localhost make]$ ./sample1_unittest 

Running main) from gtest_main.cc

[==========] Running 6 tests from 2 test cases.

[———-] Global test environment set-up.

[———-] 3 tests from FactorialTest

[ RUN      ] FactorialTest.Negative

[       OK ] FactorialTest.Negative 0 ms)

[ RUN      ] FactorialTest.Zero

[       OK ] FactorialTest.Zero 0 ms)

[ RUN      ] FactorialTest.Positive

[       OK ] FactorialTest.Positive 0 ms)

[———-] 3 tests from FactorialTest 0 ms total)

[———-] 3 tests from IsPrimeTest

[ RUN      ] IsPrimeTest.Negative

[       OK ] IsPrimeTest.Negative 0 ms)

[ RUN      ] IsPrimeTest.Trivial

[       OK ] IsPrimeTest.Trivial 0 ms)

[ RUN      ] IsPrimeTest.Positive

[       OK ] IsPrimeTest.Positive 0 ms)

[———-] 3 tests from IsPrimeTest 0 ms total)

[———-] Global test environment tear-down

[==========] 6 tests from 2 test cases ran. 1 ms total)

[  PASSED  ] 6 tests.

ok, 测试自带用例完毕,具体测试内容请参考sample1.cc,sample1.h,sample1_unittest.cc

4. 手动编写测试用例

首先需要知道gtest测试框架所自带的函数等内容应该怎么使用,比如EXPECT_EQ),单元测试用例名称TESTfun1,fun1_test) 其中TEST是格式写法,fun1可以称为单元测试用例标识符,fun1_test则为要测试的函数名称。具体内容自行查看。

文件fun.h

#ifndef __FUN_H
#define __FUN_H
int fun_testchar *buf);
int addint a, int b);
#endif

文件fun.cc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fun.h"
int fun_testchar *buf)
{
    ifbuf == NULL)
    {
        printf"error occur.\n");
        abort);
    }
    printf"%s\n",buf);
    return strlenbuf);
}

int addint a, int b)
{
    return a+b;
}

文件fun_unittest.cc

#include "fun.h"
#include <gtest/gtest.h>
TESTfun,fun_test)
{
    //fun_testNULL);
    fun_test"hello world");
}


TESTfun3,add)
{
    EXPECT_EQ2,add1,2));
    
}

5. 手动编译测试

fun.o文件:

[wln@localhost samples]$ gcc -c fun.h fun.cc

可执行文件 fun_unittest 

[wln@localhost samples]$ g++ -g -Wall  -o fun_unittest   fun.o fun_unittest.cc  ../make/gtest_main.a  -I/home/wln/gtest/gtest-1.7.0/include -lpthread    
fun_unittest.cc: In member function ‘virtual void fun_fun_test_Test::TestBody)’:
fun_unittest.cc:6: 警告:不建议使用从字符串常量到‘char*’的转换
[wln@localhost samples]$ ll
总计 1876
-rw-r–r– 1 wln wln     278 09-25 17:12 fun.cc
-rw-r–r– 1 wln wln      87 09-25 17:19 fun.h
-rw-rw-r– 1 wln wln  635112 09-25 17:23 fun.h.gch
-rw-rw-r– 1 wln wln    1220 09-25 17:23 fun.o
-rwxrwxr-x 1 wln wln 1256752 09-25 17:26 fun_unittest
-rw-r–r– 1 wln wln     170 09-25 17:13 fun_unittest.cc
[wln@localhost samples]$ ./fun_unittest 
Running main) from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[———-] Global test environment set-up.
[———-] 1 test from fun
[ RUN      ] fun.fun_test
hello world
[       OK ] fun.fun_test 0 ms)
[———-] 1 test from fun 1 ms total)

[———-] 1 test from fun3
[ RUN      ] fun3.add
fun_unittest.cc:12: Failure
Value of: add1,2)
  Actual: 3
Expected: 2
[  FAILED  ] fun3.add 0 ms)
[———-] 1 test from fun3 0 ms total)

[———-] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. 1 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] fun3.add

 1 FAILED TEST

6. 利用Makefile

Makefile文件

# A sample Makefile for building Google Test and using it in user
# tests.  Please tweak it to suit your environment and project.  You
# may want to move it to your project's root directory.
#
# SYNOPSIS:
#
#   make [all]  - makes everything.
#   make TARGET - makes the given target.
#   make clean  - removes all files generated by make.

# Please tweak the following variable definitions as needed by your
# project, except GTEST_HEADERS, which you can use in your own targets
# but shouldn't modify.

# Points to the root of Google Test, relative to where this file is.
# Remember to tweak this if you move this file.
GTEST_DIR = ..

# Where to find user code.
USER_DIR = ../samples

# Flags passed to the preprocessor.
# Set Google Test's header directory as a system directory, such that
# the compiler doesn't generate warnings in Google Test headers.
CPPFLAGS += -isystem $GTEST_DIR)/include

# Flags passed to the C++ compiler.
CXXFLAGS += -g -Wall -Wextra -pthread

# All tests produced by this Makefile.  Remember to add new tests you
# created to the list.
TESTS = fun_unittest

# All Google Test headers.  Usually you shouldn't change this
# definition.
GTEST_HEADERS = $GTEST_DIR)/include/gtest/*.h \
                $GTEST_DIR)/include/gtest/internal/*.h

# House-keeping build targets.

all : $TESTS)

clean :
        rm -f $TESTS) gtest.a gtest_main.a *.o

# Builds gtest.a and gtest_main.a.

# Usually you shouldn't tweak such internal variables, indicated by a
# trailing _.
GTEST_SRCS_ = $GTEST_DIR)/src/*.cc $GTEST_DIR)/src/*.h $GTEST_HEADERS)

# For simplicity and to avoid depending on Google Test's
# implementation details, the dependencies specified below are
# conservative and not optimized.  This is fine as Google Test
# compiles fast and for ordinary users its source rarely changes.
gtest-all.o : $GTEST_SRCS_)
        $CXX) $CPPFLAGS) -I$GTEST_DIR) $CXXFLAGS) -c \
            $GTEST_DIR)/src/gtest-all.cc

gtest_main.o : $GTEST_SRCS_)
        $CXX) $CPPFLAGS) -I$GTEST_DIR) $CXXFLAGS) -c \
            $GTEST_DIR)/src/gtest_main.cc

gtest.a : gtest-all.o
        $AR) $ARFLAGS) $@ $^

gtest_main.a : gtest-all.o gtest_main.o
        $AR) $ARFLAGS) $@ $^

# Builds a sample test.  A test should link with either gtest.a or
# gtest_main.a, depending on whether it defines its own main)
# function.

fun.o : $USER_DIR)/fun.cc $USER_DIR)/fun.h $GTEST_HEADERS)
        $CXX) $CPPFLAGS) $CXXFLAGS) -c $USER_DIR)/fun.cc


fun_unittest.o : $USER_DIR)/fun_unittest.cc  \
                     $USER_DIR)/fun.h $GTEST_HEADERS)
        $CXX) $CPPFLAGS) $CXXFLAGS) -c $USER_DIR)/fun_unittest.cc

fun_unittest : fun.o fun_unittest.o gtest_main.a
        $CXX) $CPPFLAGS) $CXXFLAGS) -lpthread $^ -o $@

执行测试

[wln@localhost make]$ make
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/fun.cc
g++ -isystem ../include -g -Wall -Wextra -pthread -c ../samples/fun_unittest.cc
../samples/fun_unittest.cc: In member function ‘virtual void fun_fun_test_Test::TestBody)’:
../samples/fun_unittest.cc:6: 警告:不建议使用从字符串常量到‘char*’的转换
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest-all.cc
In file included from ../src/gtest-all.cc:43:
../src/gtest.cc:370: 警告:成员‘testing::internal::MutexBase::owner_’缺少初始值设定
g++ -isystem ../include -I.. -g -Wall -Wextra -pthread -c \
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
ar: creating gtest_main.a
a – gtest-all.o
a – gtest_main.o
g++ -isystem ../include -g -Wall -Wextra -pthread -lpthread fun.o fun_unittest.o gtest_main.a -o fun_unittest

[wln@localhost make]$ ./fun_unittest 
Running main) from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[———-] Global test environment set-up.
[———-] 1 test from fun
[ RUN      ] fun.fun_test
hello world
[       OK ] fun.fun_test 0 ms)
[———-] 1 test from fun 0 ms total)

[———-] 1 test from fun3
[ RUN      ] fun3.add
../samples/fun_unittest.cc:12: Failure
Value of: add1,2)
  Actual: 3
Expected: 2
[  FAILED  ] fun3.add 0 ms)
[———-] 1 test from fun3 0 ms total)

[———-] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. 0 ms total)
[  PASSED  ] 1 test.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] fun3.add

 1 FAILED TEST

gtest测试框架的使用粗略的介绍完了,达到能用的目的,用好还需要多阅读相关内容。

参考:

1.http://www.cnblogs.com/coderzh/archive/2009/03/31/1426758.html

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注