如何使用谷歌测试编写cpp库的单元测试

时间:2016-02-18 10:43:23

标签: c++ visual-studio-2013 googletest

有一个类似于libxml2的库。它已经存在。我不打算创建一个新的库。我想使用谷歌测试对现有的库进行单元测试。我正在使用visual studio 2013.我想要很好的参考或任何帮助来开始。

1 个答案:

答案 0 :(得分:0)

使用gtest并不是很复杂well documented

设置构建

主要的惊喜是如何在您的项目中使用它。 "official" way是复制源中的整个存储库。

您可以通过克隆在项目中创建googletest子目录: https://github.com/google/googletest.git

然后你需要在项目的构建中添加它。 这取决于您使用的构建系统和您所使用的平台。我不了解Visual Studio,但这应该很容易适应。您需要扩展包含路径(您只能为测试对象执行此操作):

linkthree

在要与测试链接的对象中添加$(function() { $('.map').maphilight(); $('#linkone').mouseover(function() { $('#square1').mouseover(); }).mouseout(function() { $('#square1').mouseout(); }); $("#square1").on({ mouseover:function(){ $("#linkone").css("color","red");}, mouseout:function() { $('#linkone').css("color","green"); } }); $('#linktwo').mouseover(function() { $('#square2').mouseover(); }).mouseout(function() { $('#square2').mouseout(); }); $("#square2").on({ mouseover:function(){ $("#linktwo").css("color","red");}, mouseout:function() { $('#linktwo').css("color","green"); } }); $('#linkthree').mouseover(function() { $('#square3').mouseover(); }).mouseout(function() { $('#square3').mouseout(); }); $("#square3").on({ mouseover:function(){ $("#linkthree").css("color","red");}, mouseout:function() { $('#linkthree').css("color","green"); } }); $('#linkfour').mouseover(function() { $('#square4').mouseover(); }).mouseout(function() { $('#square4').mouseout(); }); $("#square4").on({ mouseover:function(){ $("#linkfour").css("color","red");}, mouseout:function() { $('#linkfour').css("color","green"); } }); }); (或Windows上的等效项)。

您还必须创建一个新目标,因为您的单元测试将由二进制文件启动。同样,我不确定如何使用Visual Studio处理它,但对于普通用户来说这应该是容易的。

编写测试

开始编写测试很容易。然而,编写好的测试很难,我不会覆盖它,有很多好的bookswebsites但最重要的是需要练习和时间。

要编写第一个测试,您需要一个非常容易编写的主文件(让我们说main.cc):

TEST_CFLAGS=-I$(GTEST_DIR)/include -I$(GTEST_DIR)

一个好的开始是查看examples。然后,您可以按照gtest doc中的primer来了解基本功能。如果您觉得自己需要更多,可以查看advanced guide

最后一句话

祝你在单元测试和(希望)测试驱动的开发过程中好运。这可能在开始时很难。不要放弃快速,奖励是巨大的。