What is the best way to generate a test report in a file using BOOST.Test?

I know that the report defaults to a standard error, so you need to redirect it to a file. My question is: will we do this inside a global fixture? It doesn't seem to work for me in any way.

This is what I tried -

struct MyConfig
{
 MyConfig()
  : testReport("fileName.log")
  {
    if(!testReport.fail())
     original = std::cerr.rdbuf(testReport.rdbuf());
  }
  ~MyConfig()
    {        
        cerr.rdbuf(original);        
        testReport.close();
    }
    ofstream testReport;
    streambuf* original;

 };

 BOOST_GLOBAL_FIXTURE(MyConfig);

After starting the test, display reports only on the console, although the 0kb file is created with the specified name.

+3
source share
3 answers

You can try this alternative, adapted from here and the alleged work on Boost 1.34.1. This is similar to what Boost intends - see Using a Results Stream Overloader.

//
// run_tests.cc
//

#define BOOST_AUTO_TEST_MAIN

#include <iostream>
#include <fstream>
#include <cassert>
#include <boost/test/auto_unit_test.hpp>
#include <boost/test/results_reporter.hpp>

std::ofstream ReportRedirector::out;

struct ReportRedirector
{
    ReportRedirector()
    {
        out.open("fileName.log");
        assert( out.is_open() );
        boost::unit_test::results_reporter::set_stream(out);
    }
private:
    static std::ofstream out;
};

BOOST_GLOBAL_FIXTURE(ReportRedirector)
+4

Boost 1.44.0 (, , ) , , testuite, (. Boost Documentation)

#include <boost/test/unit_test.hpp>
#include <string>
#include <fstream>

struct LogToFile
{
    LogToFile()
    {
        std::string logFileName(boost::unit_test::framework::master_test_suite().p_name);
        logFileName.append(".xml");
        logFile.open(logFileName.c_str());
        boost::unit_test::unit_test_log.set_stream(logFile);
    }
    ~LogToFile()
    {
        boost::unit_test::unit_test_log.test_finish();
        logFile.close();
        boost::unit_test::unit_test_log.set_stream(std::cout);
    }
    std::ofstream logFile;
};

BOOST_GLOBAL_FIXTURE(LogToFile);

logFile , , logFile XML fixture .

, , Boost 1.44.0 , XML, logFile (, , ), , logFile << "</TestLog>" << std::flush;, XML.
@Wracky ( ) logFile << "</TestLog>" << std::flush; boost::unit_test::unit_test_log.test_finish();, , .

. : --output_format=XML --log_level=all --report_level=no. XML xUnit Jenkins.

+5

:

You do not need to redirect input yourself if you do not want to. You can also specify the log file through command line arguments:

C:\MyTest.exe --log_sink=fileName.log

It's time to find. Hope this helps!

+3
source

Source: https://habr.com/ru/post/1766383/


All Articles