boost unit test - list available tests

I've written some scripts to automate the running of our unit tests, written using the boost unit testing framework. I'd like to add functionality to allow the selection and subsequent running of a subset of all tests. I know I can run a subset of tests using the run_test argument, but I can't find a way to list all tests that are in a compiled binary, i.e. all the argument values I can pass to run_test. Is there a way to extract all available tests, or will I have to write a custom test runner? If so, where do I start?

Answers


Documentation for the internals of boost::test can be a bit lacking, that said everything is available.

Have a look at the boost::test header files, specifically at the test_suite and test_unit classes. There is a function called traverse_test_tree which can be used to walk through the registered tests.

Below is some samle code I have written to output test results in a specific format, it uses traverse_test_tree to output the result of each test, hopefully it will give you a head start....

/**
 * Boost test output formatter to output test results in a format that
 * mimics cpp unit.
 */
class CppUnitOpFormatter : public boost::unit_test::output::plain_report_formatter
{
public:
    /**
     * Overidden to provide output that is compatible with cpp unit.
     *
     * \param tu the top level test unit.
     * \param ostr the output stream
     */
    virtual void do_confirmation_report( boost::unit_test::test_unit const& tu, 
                                         std::ostream& ostr );
};


class CppUnitSuiteVisitor : public test_tree_visitor
{
public:
    explicit CppUnitSuiteVisitor( const string& name ) : name_( name )
    {}

    virtual void visit( const test_case& tu )
    {
        const test_results& tr = results_collector.results( tu.p_id );
        cout << name_ << "::" << tu.p_name << " : " << ( tr.passed() ? "OK\n" : "FAIL\n" );
    }
private:
    string name_;
};

// ---------------------------------------------------------------------------|
void CppUnitOpFormatter::do_confirmation_report( 
        test_unit const& tu, std::ostream& ostr )
{
    using boost::unit_test::output::plain_report_formatter;

    CppUnitSuiteVisitor visitor( tu.p_name );
    traverse_test_tree( tu, visitor );

    const test_results& tr = results_collector.results( tu.p_id );
    if( tr.passed() ) 
    {
        ostr << "Test Passed\n";
    }
    else
    {
        plain_report_formatter::do_confirmation_report( tu, ostr );
    }
}

Trunk version of Boost.Test have command line argument to get what you need.


Need Your Help

C# can't convert string to byte array to desired result?

c# string bytearray

I have string which is storing only 1's and 0's .. now i need to convert it to a byte array. I tried ..

jQuery, how to capture when text input has changed as a result of another event

javascript jquery

I have a text input that is updated as a result of a button event. I would like to detect when the value in the text input has changed. See the example below:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.