Printing additional output in Google Test

后端 未结 4 1432
深忆病人
深忆病人 2020-12-28 12:48

I\'m using the googletest C++ testing framework. Normally the textual output of running a test looks like this:

[ RUN      ] MyTest.Fuzz
[       OK ] MyTest.Fuzz          


        
相关标签:
4条回答
  • 2020-12-28 13:20

    Simply printing to stderr will work in the default test configuration.

    std::cerr << "[          ] random seed = " << random_seed << std::endl;
    
    0 讨论(0)
  • 2020-12-28 13:29

    I have just used std::cout with ansi color codes in linux but I believe the codes work in windows since win 10 anniversary update.

    std::cout << "\033[0;32m" << "[          ] " << "\033[0;0m" 
    << "random seed = " << random_seed << std::endl;
    

    or just create a header file and some #define statements and include it in my tests. I also like to format the text to stick out a little more too.

    #define ANSI_TXT_GRN "\033[0;32m"
    #define ANSI_TXT_MGT "\033[0;35m" //Magenta
    #define ANSI_TXT_DFT "\033[0;0m" //Console default
    #define GTEST_BOX "[     cout ] "
    #define COUT_GTEST ANSI_TXT_GRN << GTEST_BOX //You could add the Default
    #define COUT_GTEST_MGT COUT_GTEST << ANSI_TXT_MGT
    

    So my code would be:

    std::cout << COUT_GTEST_MGT << "random seed = " << random_seed << ANSI_TXT_DFT << std::endl;
    
    0 讨论(0)
  • 2020-12-28 13:31

    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();
    }
    

    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();
    
    0 讨论(0)
  • 2020-12-28 13:36

    Nope, searched through the headers and there is nothing about adding your own logs in the middle. Might be something to request. You could log an enhancement task if you want at http://code.google.com/p/googletest/issues/list

    Take care.

    0 讨论(0)
提交回复
热议问题