﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-独望枫</title><link>http://www.cppblog.com/LT450196683/</link><description>人在尘世间，有缘自相见，变化千千万，未开窍，已迷恋</description><language>zh-cn</language><lastBuildDate>Mon, 06 Apr 2026 14:37:34 GMT</lastBuildDate><pubDate>Mon, 06 Apr 2026 14:37:34 GMT</pubDate><ttl>60</ttl><item><title>python在C++单元测试中的应用</title><link>http://www.cppblog.com/LT450196683/archive/2023/01/22/229637.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Sat, 21 Jan 2023 17:15:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2023/01/22/229637.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229637.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2023/01/22/229637.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229637.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229637.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: C/C++在进行单元测试的时候，总会遇到一些例如访问权限之类的问题，GCC下有-fno-access-control,但window下的主流VS系列并没找到相似功能的编译参数可选，导致window下单元测试时如果遇到private&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2023/01/22/229637.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229637.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2023-01-22 01:15 <a href="http://www.cppblog.com/LT450196683/archive/2023/01/22/229637.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>SVN下pre-commit使用</title><link>http://www.cppblog.com/LT450196683/archive/2022/11/28/229545.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Mon, 28 Nov 2022 15:09:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/11/28/229545.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229545.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/11/28/229545.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229545.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229545.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 由于之前使用git作为版本管理，当时基于git hook脚本做了一些提交前的检测处理，使用起来还是挺多便利的。但目前更换了SVN作为版本管理，一番查阅下，发现其也有类似的svn hook机制，于是想将之前用到的一些提交前的脚本处理像git那样集成到svn中。&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/11/28/229545.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229545.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-11-28 23:09 <a href="http://www.cppblog.com/LT450196683/archive/2022/11/28/229545.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何生成单元覆盖率之覆盖率生成脚本化处理</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/29/229263.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Tue, 29 Mar 2022 15:21:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/29/229263.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229263.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/29/229263.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229263.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229263.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 本文将生成覆盖率脚本化处理过程。<br><br>设计思路：通过脚本对qt工程进行编译并运行，然后再通过脚本收集必要的受关注的覆盖率信息进行生成覆盖率报告，最后查看报告。<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/03/29/229263.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229263.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-29 23:21 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/29/229263.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何生成单元覆盖率之生成单元测试覆盖率</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/29/229262.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Tue, 29 Mar 2022 15:16:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/29/229262.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229262.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/29/229262.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229262.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229262.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 前文提到添加了编译参数 -fprofile-arcs 、 -ftest-coverage 已经生成了gcno文件，单元测试运行后也产生了gcda文件。<br><br>并且我们已经安装好lcov，那么该如何使用lcov来生成覆盖率报告呢？<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/03/29/229262.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229262.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-29 23:16 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/29/229262.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何生成单元覆盖率之lcov的介绍及安装</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/29/229261.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Tue, 29 Mar 2022 15:11:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/29/229261.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229261.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/29/229261.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229261.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229261.html</trackback:ping><description><![CDATA[<p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><a href="http://ltp.sourceforge.net/coverage/lcov.php"><span style="font-family: Calibri">lcov</span></a><span style="font-family:SimSun">的页面简介如下：</span></p>  <div style="direction:ltr">  <table border="0" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr;  border-collapse:collapse;border-style:solid;border-color:#A3A3A3;border-width:  0pt">  <tbody><tr>   <td style="border-width:0pt;background-color:#E2F5FE;vertical-align:top;   width:8.4791in;padding:4pt 4pt 4pt 4pt">   <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-family:Calibri;   font-size:10.5pt">LCOV is a graphical front-end for GCC's coverage   testing tool&nbsp;<a href="http://gcc.gnu.org/onlinedocs/gcc/Gcov.html">gcov</a>.   It collects gcov data for multiple source files and creates HTML pages   containing the source code annotated with coverage information. </p>   </td>  </tr> </tbody></table>  </div>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><span style="font-family:SimSun">第一句就很明确的告诉我们，</span><span style="font-family:Calibri">lcov</span><span style="font-family:SimSun">是</span><span style="font-family:Calibri">GCC</span><span style="font-family:SimSun">覆盖率测试工具</span><span style="font-family:Calibri">gcov</span><span style="font-family:SimSun">的前端图形工具。</span></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><span style="font-family:SimSun">有些</span><span style="font-family:Calibri">Linux</span><span style="font-family:SimSun">版本的源提供了包直接安装，可以尝试使用对应包管理命令行直接安装</span></p>  <div style="direction:ltr">  <table border="0" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr;  border-collapse:collapse;border-style:solid;border-color:#A3A3A3;border-width:  0pt">  <tbody><tr>   <td style="border-width:0pt;background-color:#E2F5FE;vertical-align:top;   width:8.759in;padding:4pt 4pt 4pt 4pt">   <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><span style="font-family:SimSun">一般情况下为</span> <span style="font-weight:bold;font-family:Calibri;background:yellow;">sudo apt install lcov</span> <span style="font-family:SimSun">或者</span> <span style="font-weight:bold;font-family:Calibri;background:yellow;">yum install lcov<br /></span><span style="font-family: Calibri; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial;"><em>(ps:直接用包安装的貌似没有firefox命令)</em></span></p>   </td>  </tr> </tbody></table>  </div>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><span style="font-family:SimSun">如果源没有对应的</span><span style="font-family:Calibri">lcov</span><span style="font-family:SimSun">版本可以直接安装，那么就需要我们通过源码进行编译安装了。</span></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><span style="font-family:Calibri">lcov</span><span style="font-family:SimSun">项目可以从</span><a href="http://ltp.sourceforge.net/coverage/lcov.php"><span style="font-family: Calibri">sourceforge</span></a><span style="font-family:SimSun">下载，也可以从</span><a href="https://github.com/linux-test-project/lcov.git"><span style="font-family: Calibri">github</span></a><span style="font-family:SimSun">上下载。当前最新的版本是</span><span style="font-family:Calibri">1.15</span><span style="font-family:SimSun">，上述两个网址中，</span><span style="font-family:Calibri">sourceforge</span><span style="font-family:SimSun">只提供到</span><span style="font-family:Calibri">1.14</span><span style="font-family:SimSun">版本</span><span style="font-family:Calibri">(rpm</span><span style="font-family:SimSun">包及源码</span><span style="font-family:Calibri">)</span><span style="font-family:SimSun">的下载，</span><span style="font-family:Calibri">GitHub</span><span style="font-family:SimSun">则提供了最新</span><span style="font-family:Calibri">1.15</span><span style="font-family:SimSun">版本</span><span style="font-family:Calibri">(rpm</span><span style="font-family:SimSun">包及源码</span><span style="font-family:Calibri">)</span><span style="font-family:SimSun">。</span></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:16pt;font-size:10.5pt"><span style="font-family:SimSun">可以直接下载</span><span style="font-family:Calibri">rpm</span><span style="font-family:SimSun">包进行安装，但技术人本着生命不息折腾不止的研</span><span style="font-family: Calibri">(zuo)</span><span style="font-family:SimSun">究</span><span style="font-family:Calibri">(si)</span><span style="font-family:SimSun">精神，偏向虎山行&#8212;&#8212;咱源码编译！</span></p>  <ol type="1" style="margin-left: 0.5833in; direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in; font-family: SimSun; font-size: 10.5pt;">  <li value="1" style="margin-top:0;margin-bottom:0;vertical-align:middle;      margin-top:5pt;margin-bottom:5pt;line-height:16pt;color:#767676"><span style="font-size: 10.5pt; color: black;">下载源码完成，解压后参照</span><span style="font-family: Calibri; font-size: 10.5pt; color: black;">README</span><span style="font-size: 10.5pt; color: black;">文档，进入到</span><span style="font-family: Calibri; font-size: 10.5pt; color: black;">lcov</span><span style="font-size: 10.5pt; color: black;">的目录<br /><img src="http://www.cppblog.com/images/cppblog_com/lt450196683/img/frame/gtest/coverage/lcov/lcov_dir.png" border="0" alt="" /><br /></span></li> </ol>  <ol type="1" style="margin-left: 0.5833in; direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in; font-family: Calibri; font-size: 10.5pt;">  <li value="2" style="margin-top:0;margin-bottom:0;vertical-align:middle;      margin-top:5pt;margin-bottom:5pt;line-height:16pt"><span style="font-family: SimSun; font-size: 10.5pt;">直接</span><span style="font-size: 10.5pt;">make install</span><span style="font-family: SimSun; font-size: 10.5pt;">一下<br /><img src="http://www.cppblog.com/images/cppblog_com/lt450196683/img/frame/gtest/coverage/lcov/make_install_failed.png" border="0" alt="" /><br /></span></li> </ol>  <ol type="1" style="margin-left: 0.5833in; direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in; font-family: Calibri; font-size: 10.5pt;">  <li value="3" style="margin-top:0;margin-bottom:0;vertical-align:middle;      margin-top:5pt;margin-bottom:5pt;line-height:16pt"><span style="font-family: SimSun; font-size: 10.5pt;">增加权限再</span><span style="font-size: 10.5pt;">make install</span><span style="font-family: SimSun; font-size: 10.5pt;">一下<br /><img src="http://www.cppblog.com/images/cppblog_com/lt450196683/img/frame/gtest/coverage/lcov/sudo_make_install_ok.png" border="0" alt="" /><br /></span></li> </ol>  <ol type="1" style="margin-left: 0.5833in; direction: ltr; unicode-bidi: embed; margin-top: 0in; margin-bottom: 0in; font-family: Calibri; font-size: 10.5pt;">  <li value="4" style="margin-top:0;margin-bottom:0;vertical-align:middle;      margin-top:5pt;margin-bottom:5pt;line-height:16pt"><span style="font-weight: bold; font-size: 10.5pt;">Done.</span>&nbsp;<span style="font-family: SimSun; font-size: 10.5pt;">成功了！</span><span style="font-size: 10.5pt;">so easy</span><span style="font-family: SimSun; font-size: 10.5pt;">。全文&#8230;&#8230;卒</span></li></ol><img src ="http://www.cppblog.com/LT450196683/aggbug/229261.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-29 23:11 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/29/229261.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>如何生成单元覆盖率之gcov的介绍及启用</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/29/229260.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Tue, 29 Mar 2022 15:07:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/29/229260.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229260.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/29/229260.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229260.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229260.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: gcov是一个代码覆盖率统计的工具。不用单独下载，伴随GCC一起发布(即你安装有GCC，也就有了gcov这个工具)。与GCC协同使用、分析被测代码，有助于创建更稳健的代码，并能发现程序中未经测试的部分。<br><br>基于gcov是隶属于GCC，所以其仅适用于使用GCC编译的代码……<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/03/29/229260.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229260.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-29 23:07 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/29/229260.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Gtest场景案例之函数有其他对象非虚函数如何单元测试</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/27/229255.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Sun, 27 Mar 2022 14:47:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/27/229255.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229255.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/27/229255.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229255.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229255.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 随着事物的接触越来越多，了解的越来越深入，我们总会发现一些新的问题或者不足。<br>就像前文提到的一样，我们在面对有对象的虚函数依赖的时候，可以使用gmock框架来为我们提供方便的模拟期望值，以便我们能撇除外界的影响(依赖)从逻辑上设计单元测试并持续的进行，但是并非所有对象的函数都设计成了虚函数，那么我们在面对依赖对象的非虚函数这个问题时，又该如何解决？<br>这个问题，已经有先行者遇到并且提出了解决方案：手动打桩、使用hook技术。<br>&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/03/27/229255.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229255.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-27 22:47 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/27/229255.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Gtest场景案例之函数有其他对象虚函数依赖如何单元测试</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/27/229254.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Sun, 27 Mar 2022 14:37:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/27/229254.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229254.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/27/229254.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229254.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229254.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 在面向对象的开发过程中，由于需要将各种属性或者事物按一定的规律抽象为独立的一个对象，然后按需进行调用，如此一来，对象之间的依赖便无可避免，设计不好更会产生双向依赖、交叉依赖等困境，那么我们在面对这种对象间依赖的情况下，该如何进行单元测试呢？&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/03/27/229254.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229254.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-27 22:37 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/27/229254.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Gtest场景案例之private成员函数如何单元测试</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/27/229253.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Sun, 27 Mar 2022 14:31:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/27/229253.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229253.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/27/229253.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229253.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229253.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: 类的private函数相比public函数，测试套件定义的对象无法直接访问；相比protected函数，亦无法通过前文提到的FRIENT_TEST来使用，那么我们要怎么才能解决这个问题呢？&nbsp;&nbsp;<a href='http://www.cppblog.com/LT450196683/archive/2022/03/27/229253.html'>阅读全文</a><img src ="http://www.cppblog.com/LT450196683/aggbug/229253.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-27 22:31 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/27/229253.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Gtest场景案例之protected成员函数如何单元测试</title><link>http://www.cppblog.com/LT450196683/archive/2022/03/27/229252.html</link><dc:creator>小菜枫</dc:creator><author>小菜枫</author><pubDate>Sun, 27 Mar 2022 14:04:00 GMT</pubDate><guid>http://www.cppblog.com/LT450196683/archive/2022/03/27/229252.html</guid><wfw:comment>http://www.cppblog.com/LT450196683/comments/229252.html</wfw:comment><comments>http://www.cppblog.com/LT450196683/archive/2022/03/27/229252.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/LT450196683/comments/commentRss/229252.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/LT450196683/services/trackbacks/229252.html</trackback:ping><description><![CDATA[<p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">类的</span><span style="font-family:Calibri">protected</span><span style="font-family:SimSun">函数相比</span><span style="font-family:Calibri">public</span><span style="font-family:SimSun">函数，测试套件定义的对象无法直接访问，</span><span style="font-family: Calibri">gtest</span><span style="font-family:SimSun">提供了一个宏</span><span style="font-family:Calibri">FRIENT_TEST</span><span style="font-family:SimSun">，这个宏定义如下：</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#C586C0">#define</span><span style="color:#569CD6"> FRIEND_TEST(</span><span style="color:#72A1BA">test_case_name</span><span style="color:#569CD6">, </span><span style="color:#72A1BA">test_name</span><span style="color:#569CD6">)</span><span style="color:#AB9464">\</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#569CD6">friend</span> <span style="color:#569CD6">class</span> <span style="color:#47B7A0">test_case_name</span><span style="color:#72A1BA">##_##test_name##_Test</span></strong></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">可以见到其实现，主要是将测试用例名及测试名链接起来当成是被测类的友元类</span><span style="font-family:Calibri">[gtest</span><span style="font-family:SimSun">在运行时会自动将每个用例生成一个类进行运行</span><span style="font-family:Calibri">]</span><span style="font-family:SimSun">。</span></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:Calibri; font-size:11.0pt;color:#767676">&nbsp;</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">继续使用</span><a href="http://www.cppblog.com/LT450196683/archive/2022/03/27/229251.html"><span style="font-family:SimSun">前文</span></a><span style="font-family: SimSun">使用到的</span><span style="font-family:Calibri">model</span><span style="font-family:SimSun">作为例子，此处增加一个</span><span style="font-family: Calibri">protected</span><span style="font-family:SimSun">的成员方法</span><span style="font-family:Calibri">F</span><span style="font-family:SimSun">actorial(</span><span style="font-family: SimSun">该方法同样借用自</span><span style="font-family:SimSun">gtest</span><span style="font-family:SimSun">官方示例</span><span style="font-family:SimSun">)</span><span style="font-family:SimSun">：</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">class</span> <span style="color:#47B7A0">model</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">{</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">private:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#47B7A0">device</span><span style="color:#979797">* </span><span style="color:#72A1BA">_pdevice</span><span style="color:#979797">;</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#47B7A0">network</span><span style="color:#979797">* </span><span style="color:#72A1BA">_pnetwork</span><span style="color:#979797">;</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">public:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#989875">model</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#989875">~model</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodelinfo</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodelverion</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodeldeviceinfo</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodeldeviceversion</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodeldeviceserial</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printallinfo</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">&nbsp; &nbsp; </p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">bool</span> <span style="color:#989875">showImg</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">bool</span> <span style="color:#989875">showData</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">bool</span> <span style="color:#989875">IsPrime</span><span style="color:#979797">(</span><span style="color:#569CD6">int</span> <span style="color:#72A1BA">n</span><span style="color:#979797">);</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6"><strong>protected:</strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">int</span> <span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#569CD6">int</span> <span style="color:#72A1BA">n</span><span style="color:#979797">);</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">};</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:Calibri; font-size:11.0pt;color:#767676">&nbsp;</p>  <p style="margin:0in;font-family:&quot;Microsoft YaHei&quot;;font-size:11.0pt">下面来看看具体如何实施。</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:Calibri">1.</span><span style="font-family:SimSun">首先构建测试套件，定义一个继承自</span><span style="font-family:Calibri">testing::Test</span><span style="font-family:SimSun">的类</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">class</span> <span style="color:#47B7A0">modelTest</span><span style="color:#979797"> : </span><span style="color:#569CD6">public</span> <span style="color:#47B7A0">testing</span><span style="color:#979797">::</span><span style="color:#47B7A0">Test</span><span style="color:#979797"> {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">protected:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">};</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:Calibri">2.</span><span style="font-family:SimSun">同样需要复写</span><span style="font-family:Calibri">SetUp/TearDown</span><span style="font-family:SimSun">两个函数</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">class</span> <span style="color:#47B7A0">modelTest</span><span style="color:#979797"> : </span><span style="color:#569CD6">public</span> <span style="color:#47B7A0">testing</span><span style="color:#979797">::</span><span style="color:#47B7A0">Test</span><span style="color:#979797"> {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">protected:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><strong><span style="color:#569CD6">void</span> <span style="color:#989875">SetUp</span><span style="color:#979797">() </span><span style="color:#569CD6">override</span><span style="color:#979797"> {</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797"><strong>&nbsp; &nbsp; &nbsp; &nbsp; </strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797"><strong>&nbsp; &nbsp; }</strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">TearDown</span><span style="color:#979797">() </span><span style="color:#569CD6">override</span><span style="color:#979797"> {</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797"><strong>&nbsp; &nbsp; &nbsp; &nbsp; </strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797"><strong>&nbsp; &nbsp; }</strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">};</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:Calibri">3.</span><span style="font-family:SimSun">定义想要测试单元测试需要用到的数据，并在</span><span style="font-family:Calibri">SetUp</span><span style="font-family:SimSun">进行初始化，在</span><span style="font-family:Calibri">TearDown</span><span style="font-family:SimSun">中进行清理或析构。</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">class</span> <span style="color:#47B7A0">modelTest</span><span style="color:#979797"> : </span><span style="color:#569CD6">public</span> <span style="color:#47B7A0">testing</span><span style="color:#979797">::</span><span style="color:#47B7A0">Test</span><span style="color:#979797"> {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">protected:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">SetUp</span><span style="color:#979797">() </span><span style="color:#569CD6">override</span><span style="color:#979797"> {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; &nbsp; &nbsp; </span><strong><span style="color:#72A1BA">pm</span><span style="color:#979797"> = </span><span style="color:#C586C0">new</span> <span style="color:#47B7A0">model</span><span style="color:#979797">;</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">&nbsp; &nbsp; }</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">TearDown</span><span style="color:#979797">() </span><span style="color:#569CD6">override</span><span style="color:#979797"> {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; &nbsp; &nbsp; </span><strong><span style="color:#C586C0">if</span><span style="color:#979797"> (</span><span style="color:#72A1BA">pm</span><span style="color:#979797">) {</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#979797">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#C586C0">delete</span> <span style="color:#72A1BA">pm</span><span style="color:#979797">;</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#979797">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><span style="color:#72A1BA">pm</span><span style="color:#979797"> = </span><span style="color:#569CD6">NULL</span><span style="color:#979797">;</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797"><strong>&nbsp; &nbsp; &nbsp; &nbsp; }</strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">&nbsp; &nbsp; }</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><strong><span style="color:#47B7A0">model</span><span style="color:#979797">* </span><span style="color:#72A1BA">pm</span><span style="color:#979797">;</span></strong></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:Calibri; font-size:11.0pt">};</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:Calibri">4.</span><span style="font-family:SimSun">通过</span><span style="font-family:Calibri">TEST_F</span><span style="font-family:SimSun">宏定义想要测试的类的</span><span style="font-family:Calibri">protected</span><span style="font-family:SimSun">函数的测试用例</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#6A9955">// Tests factorial of negative numbers.</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">TEST_F</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, Negative) {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#6A9955">&nbsp; // This test is named "Negative", and belongs to the "FactorialTest"</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#6A9955">&nbsp; // test case.</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">1</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(-</span><span style="color:#899C7F">5</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">1</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(-</span><span style="color:#899C7F">1</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_GT</span><span style="color:#979797">(</span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(-</span><span style="color:#899C7F">10</span><span style="color:#979797">), </span><span style="color:#899C7F">0</span><span style="color:#979797">);</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">}</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#6A9955">// Tests factorial of 0.</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">TEST_F</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, Zero) {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">1</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#899C7F">0</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">}</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#6A9955">// Tests factorial of positive numbers.</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">TEST_F</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, Positive) {</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">1</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#899C7F">1</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">2</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#899C7F">2</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">6</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#899C7F">3</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;<span style="color:#569CD6">EXPECT_EQ</span><span style="color:#979797">(</span><span style="color:#899C7F">40320</span><span style="color:#979797">, </span><span style="color:#72A1BA">pm</span><span style="color:#979797">-&gt;</span><span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#899C7F">8</span><span style="color:#979797">));</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">}</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:SimSun; font-size:11.0pt">此时运行的话，系统会报错：</p>  <p style="margin:0in;font-family:&quot;Microsoft YaHei&quot;;font-size:11.0pt"><img src="http://www.cppblog.com/images/cppblog_com/lt450196683/img/frame/gtest/protected_func/err_func_is_protected.png" border="0" alt="" />&nbsp;</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">面对这个问题，第一，我们可以先尝试应用</span><span style="font-family: Calibri">gtest</span><span style="font-family:SimSun">提供的宏</span><span style="font-family:Calibri">FRIEND_TEST</span><span style="font-family:SimSun">：</span></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">首先，我们需要在被测类</span><span style="font-family:Calibri">model</span><span style="font-family:SimSun">中，使用</span><span style="font-family:Calibri">FRIEND_TEST</span><span style="font-family:SimSun">将我们需要使用到的</span><span style="font-family:Calibri">testname</span><span style="font-family:SimSun">注册进来</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#569CD6">class</span> <span style="color:#47B7A0">model</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">{</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">private:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#47B7A0">device</span><span style="color:#979797">* </span><span style="color:#72A1BA">_pdevice</span><span style="color:#979797">;</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#47B7A0">network</span><span style="color:#979797">* </span><span style="color:#72A1BA">_pnetwork</span><span style="color:#979797">;</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">public:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#989875">model</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#989875">~model</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodelinfo</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodelverion</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodeldeviceinfo</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodeldeviceversion</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printmodeldeviceserial</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">void</span> <span style="color:#989875">printallinfo</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">&nbsp; &nbsp; </p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">bool</span> <span style="color:#989875">showImg</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">bool</span> <span style="color:#989875">showData</span><span style="color:#979797">();</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">bool</span> <span style="color:#989875">IsPrime</span><span style="color:#979797">(</span><span style="color:#569CD6">int</span> <span style="color:#72A1BA">n</span><span style="color:#979797">);</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#569CD6">protected:</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#979797">&nbsp; &nbsp; </span><span style="color:#569CD6">int</span> <span style="color:#989875">Factorial</span><span style="color:#979797">(</span><span style="color:#569CD6">int</span> <span style="color:#72A1BA">n</span><span style="color:#979797">);</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt">&nbsp;</p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#989875">FRIEND_TEST</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, </span><span style="color:#47B7A0">Negative</span><span style="color:#979797">);</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#989875">FRIEND_TEST</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, </span><span style="color:#47B7A0">Zero</span><span style="color:#979797">);</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><strong><span style="color:#989875">FRIEND_TEST</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, </span><span style="color:#47B7A0">Positive</span><span style="color:#979797">);</span></strong></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#979797">};</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">其次，我们需要将</span><span style="font-family:Calibri">FRIEND_TEST</span><span style="font-family:SimSun">宏定义的头文件包含进来</span></p>  <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#include</span> <span style="color:#CB8F76">"gtest/gtest_prod.h"</span></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:SimSun; font-size:11.0pt">最后，运行测试用例，成功运行<br /><br /><img src="http://www.cppblog.com/images/cppblog_com/lt450196683/img/frame/gtest/protected_func/protected_func_unittest_runok.png" border="0" alt="" /></p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">由于需要改动到非测试的源码，而我们工程源码中不一定有包含到</span><span style="font-family:SimSun">gtest</span><span style="font-family: SimSun">相关的内容，很有可能会导致源工程编译出错，所以我们可以在</span><span style="font-family: Calibri">*.pro</span><span style="font-family:SimSun">文件中，增加一个宏定义</span></p>  <div style="direction:ltr">  <table border="0" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr;  border-collapse:collapse;border-style:solid;border-color:#A3A3A3;border-width:  0pt">  <tbody><tr>   <td style="border-width:0pt;background-color:#E2F5FE;vertical-align:top;   width:8.4791in;padding:4pt 4pt 4pt 4pt">   <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:Calibri;   font-size:11.0pt">DEFINES += UT </p>   </td>  </tr> </tbody></table>  </div>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-size:11.0pt"><span style="font-family:SimSun">然后再在被测试类</span><span style="font-family:Calibri">model</span><span style="font-family:SimSun">中，对新增的</span><span style="font-family:Calibri">FRIEND_TEST</span><span style="font-family:SimSun">及其头文件，可以使用预编译处理</span></p>  <div style="direction:ltr">  <table border="0" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr;  border-collapse:collapse;border-style:solid;border-color:#A3A3A3;border-width:  0pt">  <tbody><tr>   <td style="border-width:0pt;background-color:#E2F5FE;vertical-align:top;   width:8.7729in;padding:4pt 4pt 4pt 4pt">   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#ifdef</span><span style="color:#569CD6"> UT</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#include</span> <span style="color:#CB8F76">"gtest/gtest_prod.h"</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#endif</span><span style="color:#6A9955"> &nbsp;// UT</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt;color:#6A9955">&nbsp;</p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#ifdef</span><span style="color:#569CD6"> UT</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#989875">FRIEND_TEST</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, </span><span style="color:#47B7A0">Negative</span><span style="color:#979797">);</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#989875">FRIEND_TEST</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, </span><span style="color:#47B7A0">Zero</span><span style="color:#979797">);</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#989875">FRIEND_TEST</span><span style="color:#979797">(</span><span style="color:#47B7A0">modelTest</span><span style="color:#979797">, </span><span style="color:#47B7A0">Positive</span><span style="color:#979797">);</span></p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#endif</span><span style="color:#6A9955"> &nbsp;// UT</span></p>   </td>  </tr> </tbody></table>  </div>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:SimSun; font-size:11.0pt">这样就可以使得源工程编译不受单元测试新增处理的影响。但是</p>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:SimSun; font-size:11.0pt">这里要说但是了&#8230;&#8230;这样的测试会改动到我们的源码，是侵入式的，很令人不爽。所以这里可以使用一种更取巧的方式：</p>  <div style="direction:ltr">  <table border="0" cellpadding="0" cellspacing="0" valign="top" style="direction:ltr;  border-collapse:collapse;border-style:solid;border-color:#A3A3A3;border-width:  0pt">  <tbody><tr>   <td style="border-width:0pt;background-color:#E2F5FE;vertical-align:top;   width:9.0666in;padding:4pt 4pt 4pt 4pt">   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#ifdef</span><span style="color:#569CD6"> UT</span></p>   <p style="margin:0in;font-family:Calibri;font-size:10.5pt;color:#C586C0">#define protected public</p>   <p style="margin:0in;font-family:Calibri;font-size:10.5pt;color:#C586C0">#define private public</p>   <p style="margin:0in;font-family:SimSun;font-size:10.5pt"><span style="color:#C586C0">#endif</span><span style="color:#6A9955"> &nbsp;// UT</span></p>   </td>  </tr> </tbody></table>  </div>  <p style="margin-top:5pt;margin-bottom:5pt;line-height:17pt;font-family:SimSun; font-size:11.0pt">简单、直接、粗暴&#8230;&#8230;但有效，而且修改不影响到原有类，可以统一在某个都引用到的头文件中，那么一次添加，后人乘凉啊</p>  <p style="margin:0in;font-family:Calibri;font-size:11.0pt;color:#767676">&nbsp;</p>  <span style="font-family:SimSun">对应的</span><span style="font-family:Calibri">demo</span><span style="font-family:SimSun">源码</span><span style="font-family:Calibri">,</span><span style="font-family:SimSun">请点击</span><a href="https://github.com/git45016683/utdemo/tree/protected"><span style="font-family:Calibri">protectedFunc</span></a><img src ="http://www.cppblog.com/LT450196683/aggbug/229252.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/LT450196683/" target="_blank">小菜枫</a> 2022-03-27 22:04 <a href="http://www.cppblog.com/LT450196683/archive/2022/03/27/229252.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>