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

gtest测试框架使用详解_vstest和gtest比较很早就接触了googleC/C++自动化测试框架gtest,作为

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

    很早就接触了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(),单元测试用例名称TEST(fun1,fun1_test) 其中TEST是格式写法,fun1可以称为单元测试用例标识符,fun1_test则为要测试的函数名称。具体内容自行查看。

文件fun.h

#ifndef __FUN_H
#define __FUN_H
int fun_test(char *buf);
int add(int a, int b);
#endif

文件fun.cc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "fun.h"
int fun_test(char *buf)
{
    if(buf == NULL)
    {
        printf("error occur.\n");
        abort();
    }
    printf("%s\n",buf);
    return strlen(buf);
}

int add(int a, int b)
{
    return a+b;
}

文件fun_unittest.cc

#include "fun.h"
#include <gtest/gtest.h>
TEST(fun,fun_test)
{
    //fun_test(NULL);
    fun_test("hello world");
}


TEST(fun3,add)
{
    EXPECT_EQ(2,add(1,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: add(1,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: add(1,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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/187975.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • OFFSet_offtheroad

    OFFSet_offtheroad#defineoffsetof(s,m)(size_t)&reinterpret_cast<constvolatilechar&>((((s*)0)->m))该宏用于求结构体中一个成员在该结构体中的偏移量。第一个参数是结构体的名字,第二个参数是结构体成员的名字。该宏返回结构体structNames中成员memberName(m)的偏移量。偏移量…

    2022年8月22日
    6
  • Git详细教程(五):查看分支、创建分支、合并分支

    Git详细教程(五):查看分支、创建分支、合并分支一、查看分支查看的git命令如下:gitbranch:列出本地已经存在的分支,并且当前分支会用*标记gitbranch-r:查看远程版本库的分支列表gitbranch-a:查看所有分支列表(包括本地和远程,remotes/开头的表示远程分支)gitbranch-v查看一个分支的最后一次提交gitbranch–merged查看哪些分支已经合并到当前分支gitbranch–no-merged查看所有未合并工作的分支1、查看远程分支gitbr.

    2022年8月22日
    42
  • jdbc sql拼接字符串[通俗易懂]

    jdbc sql拼接字符串[通俗易懂]Listlist=this.getJtN().queryForList( "selectcpdmdm,cpmcmcfromt_cpxxwhere zt_dm=’1’andcpdmin("+cp_dm+")orderbydm");这样写是错误的,‘’单引号表示字符串,“”双引号表示字符串拼接,所以应改为"selectcpdmdm,cpmcmcfro…

    2022年6月29日
    24
  • springboot的启动流程及原理_精馏的原理及流程

    springboot的启动流程及原理_精馏的原理及流程1.springboot的启动类入口@SpringBootApplication@ComponentScan(basePackages={“cn”})publicclassSpringBootDemo{publicstaticvoidmain(String[]args){SpringApplication.run(SpringBootDemo.class);}}可以看出,Annotation定义(@SpringBootApplicati

    2022年8月21日
    3
  • js 字符串转数组,js 字符串截取

    js 字符串转数组,js 字符串截取js字符串转数组:varstring="20170524191718-173910&amp;135562983280&amp;1";varstr=string.split("&amp;");console.log(str);结果显示:console.log(str[2]);//1js字符串截取:varfilename=$("#"+fileid).v…

    2022年6月3日
    37
  • c#生成静态html文件,封装类

    由于这段时间比较轻松,于是想到很多的企业网站,新闻网站需要将页面静态化,于是写了个封装类来实现静态文件的生成,思路比较简单,但未完善,网友可根据自己的思路将此类扩展,运用了简单工厂模式(本来刚开始看设

    2021年12月20日
    38

发表回复

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

关注全栈程序员社区公众号