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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • java 反射getmethod_Java 反射机制中 getMethod()和getDeclaredField()区别

    java 反射getmethod_Java 反射机制中 getMethod()和getDeclaredField()区别今天在程序中用到java反射机制时,遇到的问题记录一下:我当时遇到的问题是,我用反射getMethod()调用类方法时,发生NoSuchMethodException异常,后来上网发现getMethod()调用公共方法,不能反射调用私有方法,后来找到getDeclaredField()能够访问本类中定义的所有方法。后来用这个方法解决了我遇到的问题。我查了javaapi文档,其中详细说明如下:…

    2022年9月16日
    0
  • DB2错误代码_db2错误码57016

    DB2错误代码_db2错误码570161前言作为一个程序员,数据库是我们必须掌握的知识,经常操作数据库不可避免,but,在写SQL语句的时候,难免遇到各种问题。例如,当咱们看着数据库报出的一大堆错误代码时,是否有种两眼发蒙的感觉呢?咳咳,莫要否认,你有、我有,全都有啊!不过,值得庆幸的是,已经有人帮咱们整理出一份关于DB2的错误代码大全啦,以后再遇到数据库报错,直接拎出看看,岂不爽哉?当然,在此对原作者送上万分的感谢。2错误

    2022年9月17日
    0
  • android开发揭秘!全靠这份Android知识点PDF大全,附答案[通俗易懂]

    android开发揭秘!全靠这份Android知识点PDF大全,附答案[通俗易懂]Glide缓存简介Glide的缓存设计可以说是非常先进的,考虑的场景也很周全。在缓存这一功能上,Glide又将它分成了两个模块,一个是内存缓存,一个是硬盘缓存。这两个缓存模块的作用各不相同,内存缓存的主要作用是防止应用重复将图片数据读取到内存当中,而硬盘缓存的主要作用是防止应用重复从网络或其他地方重复下载和读取数据。内存缓存和硬盘缓存的相互结合才构成了Glide极佳的图片缓存效果,那么接下来我们就分别来分析一下这两种缓存的使用方法以及它们的实现原理。我认识很多优秀的Android工程师,他们丝毫

    2022年5月3日
    56
  • 去重 数据库_数据库数据怎么去重

    去重 数据库_数据库数据怎么去重setnamesutf8;selectcatidfromsupe_categorieswherename=’金融’;得出catid;createtablemultmpselect*fromsupe_spaceitemswherecatidin(selectcatidfromsupe_categorieswherename=’金融’)…

    2022年10月1日
    0
  • vue转json串_vue中怎么声明一个数组

    vue转json串_vue中怎么声明一个数组一些常用更多方法介绍文章目录前言一、vue对象转数组?二、JSON数据转换1、JSON.parse2、JSON.stringify2.1、JSON.stringify高级使用总结前言提示:这里可以添加本文要记录的大概内容:例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。提示:以下是本篇文章正文内容,下面案例可供参考一、vue对象转数组?示例:工作中我们经常会因为和接口收到数据类型不一致,这个时候需要我们自己手动转换.

    2022年9月10日
    0
  • 【智能家居篇】wifi网络接入原理(上)——扫描Scanning「建议收藏」

    【智能家居篇】wifi网络接入原理(上)——扫描Scanning

    2022年1月22日
    48

发表回复

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

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