Skip to content

[cpp,c++ boost::unit_test] Using Data-Driven Test [DDT] with boost::unit_test

What Is Data-Driven Test

Data Driven Test (aka DDT) is a software test method in which test data is stored in dataset or a table. Data driven test let test programmers use a simple test interface that can execute test for all test data from dataset or a table and expect the test output there.

First, boost::unit_test, How To unit test

First, we design a customized simple string class new_string, then we test it using boost::unit_test. The class new_string might be stupid, but it can demonstrate how to unit test with boost::unit_test.

cpp,c++ code example:
#define BOOST_TEST_MODULE TEST_MODULE_01
#include <boost/test/unit_test.hpp>
#include <iostream>
#include <concepts>

class new_string: public std::string
{
public:
        new_string() = default;
        new_string(const std::string & str): std::string{str} {}
        using std::string::operator[];
        using std::string::empty;
        using std::string::data;
        using std::string::size;
        const std::string & str() const {
                return dynamic_cast<const std::string &>(*this);
        }
        template <typename any_type>
        bool operator==(const any_type & any_value) const {
                return operator==(this->str(), any_value);
        }
};

template <typename type1, typename type2>
concept same_as = std::same_as<std::decay_t<type1>, std::decay_t<type2>>;

BOOST_AUTO_TEST_CASE(test_case_01) {
        // Make an empty string for test.
        new_string nstr;
        BOOST_CHECK(nstr.size() == 0);  // test .size method.
        BOOST_CHECK(nstr.empty());      // test .empty method.

        // Make a non-empty string for test.
        new_string nstr2{"cpp"};
        BOOST_TEST(nstr2.str() == "cpp");       // test .operator== method.
        BOOST_CHECK_EQUAL(nstr2.str(), nstr2.data());   // expect the same string from two methods.

        // compile-time type test.
        static_assert(same_as<decltype(nstr.str()), std::string>);
        static_assert(std::convertible_to<decltype(nstr.data()), const char *>);
        static_assert(std::derived_from<decltype(nstr), std::string>);
        std::cout << "test_case_01 ends.\n";
}
How to compile the code:
g++ -std=c++23 hello.cpp -lboost_unit_test_framework -DBOOST_TEST_DYN_LINK
./a.out
./a.out --help

Then, boost::unit_test, DDT: Data-Driven Test

boost::unit_test library provides header <boost/test/data/test_case.hpp> and macro BOOST_DATA_TEST_CASE for DDT.

The DDT dataset of boost::unit_test has three operations: + * ^

cpp,c++ code example:
#define BOOST_TEST_MODULE TEST_MODULE_01
#include <boost/test/unit_test.hpp>
#include <boost/test/data/test_case.hpp>
#include <concepts>
#include <iostream>
#include <vector>
#include <array>
#include <string>

namespace ddt = boost::unit_test::data;

using namespace std::string_literals;

// dataset
const auto vector = std::vector{"cpp"s, "c++"s, "std::string"s};
// dataset
const auto array = std::array{123, 456, 789};
// dataset
const auto dindex = std::array{1,2,3};

BOOST_DATA_TEST_CASE(test_case_01, vector ^ array ^ dindex ^ ddt::xrange(vector.size()),
        v_data, a_data, di, xindex)
{
        // Print all dataset.
        std::cout << xindex << " => \t\t" << di << " \t\t" << v_data << " \t\t" << a_data << '\n';
        // Test dataset.
        BOOST_CHECK(v_data.size() <= 1024);  // All pass.
        BOOST_CHECK(a_data <= 500);  // two pass, one fails.
        BOOST_CHECK(di <= 10);       // All pass.
        std::cout << "test_case_01 ends.\n";
}
Compile the code:
g++ -std=c++23 hello.cpp -lboost_unit_test_framework -DBOOST_TEST_DYN_LINK
Run the code, test result:
Running 3 test cases...
0 =>            1               cpp             123
test_case_01 ends.
1 =>            2               c++             456
test_case_01 ends.
2 =>            3               std::string             789
hello.cpp(28): error: in "test_case_01/_2": check a_data <= 500 has failed
Failure occurred in a following context:
    v_data = std::string; a_data = 789; di = 3; xindex = 2;
test_case_01 ends.

*** 1 failure is detected in the test module "TEST_MODULE_01"

 

 

 

Trackbacks

No Trackbacks

Comments

Display comments as Linear | Threaded

No comments

Add Comment

E-Mail addresses will not be displayed and will only be used for E-Mail notifications.

To prevent automated Bots from commentspamming, please enter the string you see in the image below in the appropriate input box. Your comment will only be submitted if the strings match. Please ensure that your browser supports and accepts cookies, or your comment cannot be verified correctly.
CAPTCHA

Form options

Submitted comments will be subject to moderation before being displayed.

@cppfx.xyz