测试自动化 2011/06/04 - 1

1. 再次捡起GTest

以前的老资料:http://blog.csdn.net/printf1998/archive/2011/01/02/6112922.aspx

新的资源:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html

 


2. 运行GoogleTest Sample在VMware/Redhat enterprise V5

 

* make

[root@localhost make]# make
make: Warning: File `Makefile' has modification time 2.3e+07 s in the future
g++ -I../include -g -Wall -Wextra -c ../samples/sample1.cc
g++ -I../include -g -Wall -Wextra -c ../samples/sample1_unittest.cc
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest-all.cc
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
r - gtest-all.o
r - gtest_main.o
g++ -I../include -g -Wall -Wextra -lpthread sample1.o sample1_unittest.o gtest_main.a -o sample1_unittest
make: 警告:检测到时钟错误。您的创建可能是不完整的。

 

* run test

[root@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 (10 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 (8 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (40 ms total)
[  PASSED  ] 6 tests.

3. 写一个自己的test

 

* 源文件Foo.cpp and FooTest.cpp

[root@localhost frank-study]# cat Foo.cpp
int Foo(int a, int b) {
        if (a==0 || b==0) {
                throw "don't do that";
        }
        int c=a%b;
        if(c==0) {
                return b;
        }
        return Foo(b,c);
}

 

[root@localhost frank-study]# cat FooTest.cpp
#include
extern int Foo(int, int);

TEST(FooTest, HandleNoneZeroInput) {
        EXPECT_EQ(2, Foo(4,10));
        //EXPECT_EQ(6, Foo(30,18));
}

TEST(FooTest2, HandleNoneZeroInput) {
        EXPECT_EQ(2, Foo(4,10));
        EXPECT_EQ(6, Foo(30,18));
}

 

* Makefile

 

[root@localhost frank-study]# cat 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 = .

# Flags passed to the preprocessor.
CPPFLAGS += -I$(GTEST_DIR)/include

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

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

# 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.

Foo.o : $(USER_DIR)/Foo.cpp $(GTEST_HEADERS)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/Foo.cpp

FootTest.o : $(USER_DIR)/FooTest.cpp $(GTEST_HEADERS)
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $(USER_DIR)/FooTest.cpp

FooTest : Foo.o  FooTest.o gtest_main.a
        $(CXX) $(CPPFLAGS) $(CXXFLAGS) -lpthread $^ -o $@

 

注:Makefile的预定义变量

 

GNU make 的主要预定义变量:
预定义变量                      含义
$*              不包含扩展名的目标文件名称。
$+              所有的依赖文件,以空格分开,并以出现的先后为序,可能包含重复的依赖文件。
$<              第一个依赖文件的名称。
$?              所有的依赖文件,以空格分开,这些依赖文件的修改日期比目标的创建日期晚。
$@              目标的完整名称。
$^              所有的依赖文件,以空格分开,不包含重复的依赖文件。
$%              如果目标是归档成员,则该变量表示目标的归档成员名称。例如,如果目标名称
                为 mytarget.so(image.o),则 $@ 为 mytarget.so,而 $% 为 image.o。
AR              归档维护程序的名称,默认值为 ar。
ARFLAGS         归档维护程序的选项。
AS              汇编程序的名称,默认值为 as。
ASFLAGS         汇编程序的选项。
CC              C 编译器的名称,默认值为 cc。
CCFLAGS         C 编译器的选项。
CPP             C 预编译器的名称,默认值为 $(CC) -E。
CPPFLAGS        C 预编译的选项。
CXX             C++ 编译器的名称,默认值为 g++。
CXXFLAGS        C++ 编译器的选项。
FC              FORTRAN 编译器的名称,默认值为 f77。
FFLAGS          FORTRAN 编译器的选项。

 

* make及运行

[root@localhost frank-study]# make
make: Warning: File `../include/gtest/gtest-death-test.h' has modification time 2.3e+07 s in the future
g++ -I../include -g -Wall -Wextra -c ./Foo.cpp
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest-all.cc
g++ -I../include -I.. -g -Wall -Wextra -c /
            ../src/gtest_main.cc
ar rv gtest_main.a gtest-all.o gtest_main.o
r - gtest-all.o
r - gtest_main.o
g++ -I../include -g -Wall -Wextra -lpthread Foo.o FooTest.o gtest_main.a -o FooTest
make: 警告:检测到时钟错误。您的创建可能是不完整的。
[root@localhost frank-study]# ./FooTest
Running main() from gtest_main.cc
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from FooTest
[ RUN      ] FooTest.HandleNoneZeroInput
[       OK ] FooTest.HandleNoneZeroInput (0 ms)
[----------] 1 test from FooTest (3 ms total)

[----------] 1 test from FooTest2
[ RUN      ] FooTest2.HandleNoneZeroInput
[       OK ] FooTest2.HandleNoneZeroInput (0 ms)
[----------] 1 test from FooTest2 (2 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (18 ms total)
[  PASSED  ] 2 tests

 

Q:如何每次不用再build gtest的源代码???

 

 

你可能感兴趣的:(testing,gtest/gmock)