禁用从数据集定义的特定测试实例

Disabling specific test instance defined from a dataset

假设我有以下测试代码:

struct MyData
{
    MyData( int in_, double out_ )
    : m_in{ in_ }
    , m_out{ out_ }
    {}

    int    m_in;
    double m_out;
};

std::ostream& operator<<( std::ostream& os_, MyData data_ )
{
    os_ << data_.m_in << " " << data_.m_out << std::endl;
    return os_;
}

std::vector< MyData > DataSetGet()
{
    return
        {
            { 1, 1.0 },
            { 2, 2.0 }, // I would like to disable this...
            { 3, 3.0 },
        };
}

BOOST_DATA_TEST_CASE( CastIntToDouble, DataSetGet() )
{
    BOOST_TEST( static_cast< double >( sample.m_in ) == sample.m_out );
}

我想禁用第二个测试实例。我可以像这样注释掉第二种情况:

std::vector< MyData > DataSetGet()
{
    return
        {
            { 1, 1.0 },
            //{ 2, 2.0 }, // I would like to disable this...
            { 3, 3.0 },
        };
}

但是这个案例将不再被编译,这不是我要找的。在我的真实场景中,测试用例更复杂,我希望它们被编译,但不是 运行.

我搜索了有关此主题的 boost 文档,发现了 disable decorator,它看起来正是我需要的。但是,我没有找到在数据测试用例中使用它的方法。这可能吗?

你可以supply a command line argument,比如:

./sotest -l all -t "!*/*_1"

输出:

更多Advanced/Dynamic

您还可以根据 enable_if 提供自己的装饰器,您可以使用自己的自定义命令行选项(您需要提供自己的 test_main 来解析这些选项) .

此类自定义装饰器的示例:https://www.boost.org/doc/libs/1_78_0/libs/test/doc/html/boost_test/tests_organization/enabling.html#boost_test.tests_organization.enabling.runtime_run_status

现场演示

Live On Coliru

#define BOOST_TEST_MODULE Whosebug
#include <boost/test/unit_test.hpp>
#include <boost/test/data/dataset.hpp>
#include <boost/test/data/test_case.hpp>
#include <boost/test/unit_test_parameters.hpp>

namespace but = boost::unit_test;
namespace utf = boost::unit_test_framework;

struct MyData {
    MyData(int in_, double out_) : m_in{in_}, m_out{out_} {}

    int    m_in;
    double m_out;
};

std::ostream& operator<<(std::ostream& os_, MyData data_) {
    return os_ << data_.m_in << " " << data_.m_out << std::endl;
}

std::vector<MyData> DataSetGet()
{
    return {
        {1, 1.0},
        {2, 2.0}, // I would like to disable this...
        {3, 3.0},
    };
}

BOOST_DATA_TEST_CASE(CastIntToDouble, DataSetGet())
{
    BOOST_TEST( static_cast< double >( sample.m_in ) == sample.m_out );
}

运行 和

g++ -std=c++20 -O2 -Wall -pedantic -pthread main.cpp -lboost_unit_test_framework
./a.out -l all -t "!*/*_1" --no_color_output

版画

Running 2 test cases...
Entering test module "Whosebug"
main.cpp(30): Entering test suite "CastIntToDouble"
main.cpp(30): Entering test case "_0"
main.cpp(32): info: check static_cast< double >( sample.m_in ) == sample.m_out has passed
Assertion occurred in a following context:
    sample = 1 1
; 
main.cpp(30): Leaving test case "_0"; testing time: 443us
main.cpp(30): Test case "CastIntToDouble/_1" is skipped because disabled
main.cpp(30): Entering test case "_2"
main.cpp(32): info: check static_cast< double >( sample.m_in ) == sample.m_out has passed
Assertion occurred in a following context:
    sample = 3 3
; 
main.cpp(30): Leaving test case "_2"; testing time: 404us
main.cpp(30): Leaving test suite "CastIntToDouble"; testing time: 967us
Leaving test module "Whosebug"; testing time: 1016us

*** No errors detected