boost unit test - list available tests

送分小仙女□ 提交于 2019-12-05 16:21:47

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!