googletest.docx

上传人:b****5 文档编号:12119291 上传时间:2023-04-17 格式:DOCX 页数:20 大小:28.80KB
下载 相关 举报
googletest.docx_第1页
第1页 / 共20页
googletest.docx_第2页
第2页 / 共20页
googletest.docx_第3页
第3页 / 共20页
googletest.docx_第4页
第4页 / 共20页
googletest.docx_第5页
第5页 / 共20页
点击查看更多>>
下载资源
资源描述

googletest.docx

《googletest.docx》由会员分享,可在线阅读,更多相关《googletest.docx(20页珍藏版)》请在冰豆网上搜索。

googletest.docx

googletest

GoogleTest(GoogleC++TestingFramework)简介

GoogleTest是GoogleC++TestingFramework的一种非正式的称谓,是google最近发布的一个开源C++测试框架。

具有以下特点:

Google'sframeworkforwritingC++testsonavarietyofplatforms(Linux,MacOSX,Windows,Cygwin,WindowsCE,andSymbian).BasedonthexUnitarchitecture.Supportsautomatictestdiscovery,arichsetofassertions,user-definedassertions,deathtests,fatalandnon-fatalfailures,type-parameterizedtests,variousoptionsforrunningthetests,andXMLtestreportgeneration.

Google的测试框架是在不同平台上(Linux,MacOSX,Windows,Cygwin,Windows CE和Symbian)为编写C++测试而生成的。

它是基于xUnit架构的测试框架,支持自动发现测试,丰富的断言集,用户定义的断言,death测试,致命与非致命的失败,类型参数化测试,各类运行测试的选项和XML的测试报告。

更多可参考它的主页:

补充说明:

      对比cppunit,boost.test,GoogleTest使用更加简便。

     cppunit的mfc输出需要修改好些地方,头文件以及实现文件均需要修改,并且在不同的地方使用不同的宏名称,需要包含不同的头文件,比较繁琐。

 

     boost.test继承boost库的传统,虽然没有仔细使用过,但是从文档中可以看到还是比较复杂的。

    googletest相对比较容易,可能为了符合JUnit移植过来的思想:

程序员在最简单的情况下就能够使用JUnit,不用修改现有代码,也不用去了解很多库相关的内容以及语言特性。

   googletest的单元测试仅需要TEST()和TEST_F()两个宏,真的佩服它的简洁。

GoogleTestPrimer(入门)

(一)——测试框架目标

GoogleTestPrimer 

GettingstartedwithGoogleC++TestingFramework

开始GoogleC++测试框架

GoogleC++TestingFrameworkPrimer

GoogleC++测试框架入门

Introduction:

WhyGoogleC++TestingFramework?

介绍:

为什么是GoogleC++测试框架

GoogleC++TestingFrameworkhelpsyouwritebetterC++tests.

GoogleC++测试框架帮助你编写更好的C++测试。

NomatterwhetheryouworkonLinux,Windows,oraMac,ifyouwriteC++code,GoogleTestcanhelpyou.

无论你是在Linux,windows还是Mac上编写C++代码,GoogleTest都可以帮助你。

Sowhatmakesagoodtest,andhowdoesGoogleC++TestingFrameworkfitin?

Webelieve:

因此,怎样才能作为一个好的测试,GoogleC++测试框架如何满足呢?

我们相信:

1.Testsshouldbeindependentandrepeatable.It'sapaintodebugatestthatsucceedsorfailsasaresultofothertests.GoogleC++TestingFrameworkisolatesthetestsbyrunningeachofthemonadifferentobject.Whenatestfails,GoogleC++TestingFrameworkallowsyoutorunitinisolationforquickdebugging.Tests应该是独立的和可复现的,以其他测试成功或失败的结果来debug测试是痛苦的。

GoogleC++测试框架通过在不同的对象上运行每个测试将测试隔离起来。

当一个测试失败时,GoogleC++测试框架允许你在快速debugging时继续运行隔离测试。

2.Testsshouldbewellorganizedandreflectthestructureofthetestedcode.GoogleC++TestingFrameworkgroupsrelatedtestsintotestcasesthatcansharedataandsubroutines.Thiscommonpatterniseasytorecognizeandmakestestseasytomaintain.Suchconsistencyisespeciallyhelpfulwhenpeopleswitchprojectsandstarttoworkonanewcodebase.Tests应该被很好的组织,应该反射测试代码的结构。

GoogleC++测试框架组合相关测试为能够共享数据与子过程的测试案例,当人们改变工程,开始在新的代码基础上工作时,这种一致性特别有用。

3.Testsshouldbeportableandreusable.Theopen-sourcecommunityhasalotofcodethatisplatform-neutral,itstestsshouldalsobeplatform-neutral.GoogleC++TestingFrameworkworksondifferentOSes,withdifferentcompilers(gcc,MSVC,andothers),withorwithoutexceptions,soGoogleC++TestingFrameworktestscaneasilyworkwithavarietyofconfigurations.(NotethatthecurrentreleaseonlycontainsbuildscriptsforLinux-weareactivelyworkingonscriptsforotherplatforms.)测试应该是可移植的和可重用的。

开源社区拥大量中性平台下的代码,相关测试也需要中性平台下的测试。

GoogleC++测试框架可在不同系统上使用或不使用异常的不同编译器(gcc,MSVC和其他编译器)上运行,因此GoogleC++测试框架在各种配置下可以很容易运行(注意当前发布平台仅包含Linux下的构建脚本——我们正积极编写在其他平台上的构建脚本)。

4.Whentestsfail,theyshouldprovideasmuchinformationabouttheproblemaspossible.GoogleC++TestingFrameworkdoesn'tstopatthefirsttestfailure.Instead,itonlystopsthecurrenttestandcontinueswiththenext.Youcanalsosetupteststhatreportnon-fatalfailuresafterwhichthecurrenttestcontinues.Thus,youcandetectandfixmultiplebugsinasinglerun-edit-compilecycle.当测试失败时,测试应该对问题提供尽可能多的信息。

GoogleC++测试框架不在第一个测试失败时停止测试。

相反,测试仅停止当前测试,然后继续下一个测试。

你可以构建非致命失败报告的测试,当前测试在非致命失败产生后仍继续运行。

因此,你可以在一次运行-编辑-编译循环中发现和修订大量bugs。

5.Thetestingframeworkshouldliberatetestwritersfromhousekeepingchoresandletthemfocusonthetestcontent.GoogleC++TestingFrameworkautomaticallykeepstrackofalltestsdefined,anddoesn'trequiretheusertoenumeratetheminordertorunthem.Testingframework应该把测试编写者从大量杂务事情中解放出来,使测试编写者更加关注测试内容。

GoogleC++测试框架自动跟踪所有定义的测试,不需要用户为运行测试而去枚举测试。

6.Testsshouldbefast.WithGoogleC++TestingFramework,youcanreusesharedresourcesacrosstestsandpayfortheset-up/tear-downonlyonce,withoutmakingtestsdependoneachother.测试应该是快速的,使用GoogleC++测试框架,你可以在跨测试中重复使用共享资源,仅需一次构建/拆毁的开销,而并不需要在测试之间发生相互依赖。

SinceGoogleC++TestingFrameworkisbasedonthepopularxUnitarchitecture,you'llfeelrightathomeifyou'veusedJUnitorPyUnitbefore.Ifnot,itwilltakeyouabout10minutestolearnthebasicsandgetstarted.Solet'sgo!

由于GoogleC++测试框架是基于流行的xUnit测试构架,因此如果之前使用过JUnit或PyUnit,你将会感到非常熟悉。

如果之前没有用过JUnit或PyUnit,花费大约10分钟去学习基础概念和开始使用。

好的,我们开始吧!

(我读完这篇文档花费了足足花了好几个小时,10分钟对于我们这些非英语为母语的技术员是不太可能的,估计是google为了表明自己的测试框架是多么的简单,吸引更多的技术员而采取的一种夸张的说法。

Note:

WeoccasionallyrefertoGoogleC++TestingFrameworkinformallyasGoogleTest.

注意:

我们偶尔采用非正式的GoogleTest名称来表示GoogleC++测试框架

GoogleTestPrimer(入门)

(二)——基本概念

BasicConcepts

基本概念

WhenusingGoogleTest,youstartbywriting assertions,whicharestatementsthatcheckwhetheraconditionistrue.Anassertion'sresultcanbe success, nonfatalfailure,or fatalfailure.Ifafatalfailureoccurs,itabortsthecurrentfunction;otherwisetheprogramcontinuesnormally.

当使用GoogleTest时,从编写测试条件是否为真断言的语句开始。

一个断言的结果可以成功,非致命的失败和致命的失败。

如果致命的失败发生了,测试将终止当前函数,否则程序将继续运行。

Tests useassertionstoverifythetestedcode'sbehavior.Ifatestcrashesorhasafailedassertion,thenit fails;otherwiseit succeeds.

测试使用断言确保测试代码行为,如果测试crash了或者断言失败,测试将失败,否则测试成功。

A testcase containsoneormanytests.Youshouldgroupyourtestsintotestcasesthatreflectthestructureofthetestedcode.Whenmultipletestsinatestcaseneedtosharecommonobjectsandsubroutines,youcanputthemintoa testfixture class.

一个testcase(测试案例)包含一个或多个测试。

应该组合你的测试到反射出测试代码结构的测试案例中,当一个测试案例中有多个测试需要共享对象和子过程时,应该将它们放置在一个testfixture类中。

A testprogram cancontainmultipletestcases.

一个测试程序可以包含多个测试案例

We'llnowexplainhowtowriteatestprogram,startingattheindividualassertionlevelandbuildinguptotestsandtestcases.

我们将描述如何编写测试程序,开始使用独立的断言条件和构建测试以及测试案例。

GoogleTest(Primer)(三)——断言

Assertions

断言

GoogleTestassertionsaremacrosthatresemblefunctioncalls.Youtestaclassorfunctionbymakingassertionsaboutitsbehavior.Whenanassertionfails,GoogleTestprintstheassertion'ssourcefileandlinenumberlocation,alongwithafailuremessage.YoumayalsosupplyacustomfailuremessagewhichwillbeappendedtoGoogleTest'smessage.

GoogleTest断言通过类似于函数调用的宏来实现,通过断言行为来测试一个类或函数。

当断言失败时,GoogleTest打印出断言源文件名和定位行号,并打印失败信息。

可以提供自定义的失败信息,这些信息将添加到GoogleTest信息的后面。

(如果你看过GoogleTest的FAQ,你就会知道,这些自定义的输出内容是输出到stdout中的,而GoogleTest的断言信息是输出到stderr中的,可以通过重定向到文件中将stdout与stderr分离,具体查看FAQ)

Theassertionscomeinpairsthattestthesamethingbuthavedifferenteffectsonthecurrentfunction. ASSERT_* versionsgeneratefatalfailureswhentheyfail,and abortthecurrentfunction. EXPECT_* versionsgeneratenonfatalfailures,whichdon'tabortthecurrentfunction.Usually EXPECT_* arepreferred,astheyallowmorethanonefailurestobereportedinatest.However,youshoulduse ASSERT_* ifitdoesn'tmakesensetocontinuewhentheassertioninquestionfails.

对同一个测试,断言是成对出现的,但是对当前函数有不同的影响。

当测试失败时ASSERT_*版本产生一个致命失败,并终止当前函数。

EXPECT_*版本产生一个非致命失败,但并不终止当前函数。

通常使用EXPECT_*更好一些,EXPECT_*允许在一次测试中报告更多的失败。

但是,如果当断言查询失败时,继续运行变得无意义时,应该使用ASSERT_*。

Sinceafailed ASSERT_* returnsfromthecurrentfunctionimmediately,possiblyskippingclean-upcodethatcomesafterit,itmaycauseaspaceleak.Dependingonthenatureoftheleak,itmayormaynotbeworthfixing-sokeepthisinmindifyougetaheapcheckererrorinadditiontoassertionerrors.

由于ASSERT_*失败立即从当前函数返回,可能跳过了之后的清除代码,这可能导致资源泄漏。

依赖泄漏的类型,因此记住如果在添加断言错误时获取一个堆检测错误,可能或可能不值得修复

Toprovideacustomfailuremessage,simplystreamitintothemacrousingthe << operator,orasequenceofsuchoperators.Anexample:

为提供自定义失败信息,通过使用<<操作符或一系列该操作符,简单地将自定义失败信息流向宏。

例如:

ASSERT_EQ(x.size(), y.size()) << "Vectorsxandyareofunequallength";

 

for (int i = 0; i < x.size(); ++i) {

 EXPECT_EQ(x[i], y[i]) << "Vectorsxandydifferatindex" << i;

}

Anythingthatcanbestreamedtoan ostream canbestreamedtoanassertionmacro--inparticular,Cstringsand string objects.Ifawidestring(wchar_t*, TCHAR* in UNICODE modeonWindows,or std:

:

wstring)isstreamedtoanassertion,itwillbetranslatedtoUTF-8whenprinted.

任何可以流到ostream中的流均可以流向断言宏中——尤其是C字符串和string对象。

如果宽字符串(在Windows中的UNICODE模式下wchar_t*,TCHAR*或是std:

:

wstring)流向一个断言,打印(输出)时被转换为UTF-8

BasicAssertions

基础断言

Theseassertionsdobasictrue/falseconditiontesting.

下面断言是做基本的true/false条件测试

Fatalassertion

致命断言

Nonfatalassertion

非致命断言

Verifies

举例说明

ASSERT_TRUE(condition);

EXPECT_TRUE(condition);

condition istrue

ASSERT_FALSE(condition);

EXPECT_FALSE(condition);

condition isfalse

Remember,whentheyfail, ASSERT_* yieldsafatalfailureandreturnsfromthecurrentfunction,while EXPECT_* yieldsanonfatalfailure,allowingthefunctiontocontinuerunning.Ineithercase,anassertionfailuremeansitscontainingtestfails.

记住,当断言失败时,ASSERT_*产生一个致命失败,从当前函数返回,而EXPECT_*产生一个非致命失败,允许函数继续运行。

在两个测试中,一个断言失败意味着包含的测试条件失败。

Availability:

Linux,Windows,Mac.

 

BinaryComparison

二进制比较

Thissectiondescribesassertionsthatcomparetwovalues.

下面描述的断言为比较两个值

Fatalassertion

致命断言

Nonfatalassertion

非致命断言

Verifies

举例说明

ASSERT_EQ(expected, actual);

EXPECT_EQ(expected, actual);

expected == actual

ASSERT_NE(val1, val2);

EXPECT_NE(val1, val2);

val1 !

= val2

ASSERT_LT(val1, val2);

EXPECT_LT(val1, val2);

val1 < val2

ASSERT_LE(val1, val2);

EXPECT_LE(val1, va

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1