使用gtest对类进行白盒测试

gtest是一个很好用的测试框架,可以很方便地对函数进行单元测试。
主页:http://code.google.com/p/googletest/
一篇好文章,《玩转Google开源C++单元测试框架Google<wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">Test系列》:http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">在使用gtest的过程中,我发现使用gtest对类进行白盒测试有些麻烦——因为测试用例可能需要访问类的私有变量或者私有函数。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">最原始的解决方法有两个:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">1)为私有变量增加get函数,这个太麻烦了,而且会使得类的接口急剧膨胀。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">2)使用FRIEND_TEST宏,这个不算麻烦,假设测试</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">TEST(TestClassA,</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">WhiteBoxTesting)想要访问类A的私有变量,在类A中声明FRIEND_TEST(TestClassA,</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">WhiteBoxTesting)即可。但这样有两个问题:第一,如果有很多测试用例,类A中要声明得不亦说乎。第二,为了使源码不依赖于具体测试框架,我们希望不要在类A中包含gtest头文件。</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">目前的解决方法:使用friend</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">class</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">在类A中声明:friend</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">class</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">white_box_testing_classa;</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">在测试代码里面:</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">class</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">white_box_testing_classa</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">:</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">public</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">testing::Test</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">{</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">static</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">void</span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">test_method1(){// 这里可以自由访问A的私有成员}</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">static void test_method2(){// 这里可以自由访问A的私有成员}</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">// 根据需要定义SetUpTestCase() TearDownTestCase() SetUp() TearDown()</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">};</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">TEST_F(white_box_testing_classa, test_method1)</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">{</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">white_box_testing_classa::test_method1();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">}</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">TEST_F(white_box_testing_classa, test_method2)</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">{</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"></span><wbr style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">white_box_testing_classa::test_method2();</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">}</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">期间遇到的问题:c++里面如果类A属于命名中间ns_x,则声明的友类如果不加域修饰符则必须也属于ns_x。否则即使声明了友类,也会提示无权限访问私有成员!</span><br style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left"><span style="color:rgb(70,70,70); font-family:simsun; font-size:14px; line-height:21px; text-align:left">使用域修饰符,友类可以属于其他命名空间,但是比较麻烦,所以直接把测试代码放在同个命名空间比较省事。</span></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

你可能感兴趣的:(test)