且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Google测试中打印其他输出

更新时间:2023-12-04 11:48:16

您可以为默认打印机 PrettyUnitTestResultPrinter 编写一个包装器以打印出测试属性。您可以使用 listeners.default_result_printer()(见下文)获取默认打印机。您必须实现 EmptyTestEventListener 并更改方法 PrettyUnitTestResultPrinter :: OnTestEnd() in gtest.cc )并使用结果属性: test_info。

You could write a wrapper for the default printer PrettyUnitTestResultPrinter to also print out test properties. You can get the default printer with listeners.default_result_printer()(see below). You would have to implement EmptyTestEventListener and change the method PrettyUnitTestResultPrinter::OnTestEnd() (in gtest.cc) and use the results properties: test_info.result()->GetTestProperty(i) like the printer for XML output:

String XmlUnitTestResultPrinter::TestPropertiesAsXmlAttributes(
    const TestResult& result) {
  Message attributes;
  for (int i = 0; i < result.test_property_count(); ++i) {
    const TestProperty& property = result.GetTestProperty(i);
    attributes << " " << property.key() << "="
        << "\"" << EscapeXmlAttribute(property.value()) << "\"";
  }
  return attributes.GetString();
}

然后你可以替换默认侦听器, a href =http://code.google.com/p/googletest/source/browse/trunk/samples/sample9_unittest.cc> google测试示例:

Then you can replace the default listener for your tests like it's done in the google test sample:

UnitTest& unit_test = *UnitTest::GetInstance();
if (terse_output) {
  TestEventListeners& listeners = unit_test.listeners();
  delete listeners.Release(listeners.default_result_printer());
  listeners.Append(new TersePrinter);
}
int ret_val = RUN_ALL_TESTS();