mardi 26 avril 2016

Calling a boost unit_test test within code

I'm trying to call a boost::unit_test from code.

In a number of files I've got

BOOST_AUTO_TEST_SUITE(DataAccessSuite)

BOOST_AUTO_TEST_CASE(DateAppender)
{
...
}
BOOST_AUTO_TEST_SUITE_END()

For my dialog box I have a visitor to gather the IDs and names of all the test cases/suites

namespace {
    unit_test::test_suite* init_unit_test_suite(int argc, char** argv) {
        return 0;
    }
    using namespace std::string_literals;

    struct test_visitor : unit_test::test_tree_visitor {
        test_visitor(std::vector<std::tuple<std::string, unit_test::test_unit_id>>& tests) : m_tests(tests) {}
        void visit(unit_test::test_case const& test) {
            m_tests.emplace_back(std::make_tuple(suite + "/"s + static_cast<std::string>(test.p_name),test.p_id));
        }
        virtual bool    test_suite_start(unit_test::test_suite const& ts) { 
            suite = ts.p_name;
            return true;
        }
        virtual void    test_suite_finish(unit_test::test_suite const&) {
            suite = std::string();
        }

        std::string suite;
        std::vector<std::tuple<std::string, unit_test::test_unit_id>>& m_tests;
    };
}

TestDialogImpl::TestDialogImpl(wxWindow* parent) : TestDialog(parent)
{
    // Make a list of test cases to show in my dialog box
    unit_test::traverse_test_tree(unit_test::framework::master_test_suite(), test_visitor(m_tests), true);
    for (auto& test : m_tests) {
        m_listBox2->Append(wxString(std::get<0>(test)));
    }
}

And here's my call to the test case

void TestDialogImpl::OnClick_RunButton(wxCommandEvent & event)
{
    auto selection = m_listBox2->GetStringSelection();
    char* argv[] = { "OptionModeller.exe","--run_test=DataAccessSuite/DateAppender" };
    unit_test::framework::init(init_unit_test_suite, 2, argv);
    auto finder = std::find_if(std::begin(m_tests), std::end(m_tests), [&selection](auto& v) { return std::get<0>(v) == selection; });
     // This fails with setup_error(), but I don't know why?
    unit_test::framework::run(std::get<1>(*finder), true);
}

Is there a way I could call the test and utilize the framework. I know I could alternatively call the free functions but that defeats the point of using BOOST_TEST

Aucun commentaire:

Enregistrer un commentaire