我们的自动化测试框架是基于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
Expected: 3
 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
Expected: 3
 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的数量那部分并没有修改。
本文介绍了在将基于gtest的自动化测试框架移植到Wince时遇到的问题,由于gtest在Wince上不支持`testing::internal::CaptureStdout()`,导致无法获取测试输出。通过使gtest输出到XML文件并修改gtest源码,添加`should_run()`判断,实现了只输出运行的测试用例,提高了效率。
1987





