提高单元测试 - 列表中可用的测试 [英] boost unit test - list available tests

查看:95
本文介绍了提高单元测试 - 列表中可用的测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写了一些脚本来自动的单元测试,使用升压单元测试框架编写的运行。我想补充的功能,让所有测试的一个子集的选择和后续运行。我知道我可以运行使用RUN_TEST参数测试的一个子集,但我不能找到一种方法来列出处于编译的二进制所有测试,即所有的参数值我可以传递给RUN_TEST。有没有一种方法来提取所有可用的测试,或者我会写一个自定义的测试运行?如果是这样,我从哪里开始?

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?

推荐答案

有关的boost ::测试的内部文件可以缺少一点,即所说的一切是可用的。

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

看一看了boost ::测试头文件,特别是在test_suite和test_unit类。有一个函数调用traverse_test_tree可用于通过注册测试行走。

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.

下面是一些samle code我已经写在一个特定的格式输出测试结果,它采用traverse_test_tree输出每个测试的结果,希望这会给你一个良好的开端......

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

这篇关于提高单元测试 - 列表中可用的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆