让gtest输出xml文件时只输出运行的用例

本文介绍了在将基于gtest的自动化测试框架移植到Wince时遇到的问题,由于gtest在Wince上不支持`testing::internal::CaptureStdout()`,导致无法获取测试输出。通过使gtest输出到XML文件并修改gtest源码,添加`should_run()`判断,实现了只输出运行的测试用例,提高了效率。

我们的自动化测试框架是基于gtest的框架的,目前在向Wince移植的时候,对gtest的输出重定向那块出了问题,由于gtest在Wince上不支持   testing::internal::CaptureStdout(); 函数,导致我们无法获得输出信息,以至于无法解析运行结果。

后来的思路是想如果gtest的输出可以输出到文件里,那么我们直接解析文件就好了,经过查找发现gtest 可以输出到xml文件里,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="2" failures="1" disabled="0" errors="0" time="0.067" name="AllTests">
  <testsuite name="FooTest" tests="2" failures="1" disabled="0" errors="0" time="0.028">
    <testcase name="HandleZeroInput" status="run" time="0.01" classname="FooTest">
      <failure message="Value of: 4&#x0A;Expected: 3&#x0A; 3 is not equal 4" type=""><![CDATA[.\testGtest.cpp:39
Value of: 4
Expected: 3
 3 is not equal 4]]></failure>
    </testcase>
    <testcase name="HandleZeroInput1" status="notrun" time="0" classname="FooTest" />
  </testsuite>
</testsuites>

实现测试发现,gtest会输出所有的测试用例到文件里,而实际上我设置 testing::FLAGS_gtest_filter,只运行了一个,我们的测试用例有上千条了,这样的话严重影响了效率。怎么办?由于gtest是开源的嘛,所以查看了xml输出这块的代码实现,修改了gtest.cc中的如下部分:

void XmlUnitTestResultPrinter::OutputXmlTestInfo(::std::ostream* stream,  const char* test_case_name, const TestInfo& test_info)

在这个函数加上了  if (test_info.should_run()) 的判断,只有should_run()为true的时候才给xml输出。这样重新编译库文件,最后实现的结果如下:

<?xml version="1.0" encoding="UTF-8"?>
<testsuites tests="2" failures="1" disabled="0" errors="0" time="0.068" name="AllTests">
  <testsuite name="FooTest" tests="2" failures="1" disabled="0" errors="0" time="0.028">
    <testcase name="HandleZeroInput" status="run" time="0.011" classname="FooTest">
      <failure message="Value of: 4&#x0A;Expected: 3&#x0A; 3 is not equal 4" type=""><![CDATA[.\testGtest.cpp:39
Value of: 4
Expected: 3
 3 is not equal 4]]></failure>
    </testcase>
  </testsuite>
</testsuites>

可以看到只有run的那条用例有输出,当然作为我只关心执行的那条testcase,所以总的tests的数量那部分并没有修改。

 

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值