11

このコードを実行します

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>

#include <iostream>

using namespace boost::unit_test;
using namespace std;


void TestFoo()
{
    BOOST_CHECK(0==0);
}

test_suite* init_unit_test_suite( int argc, char* argv[] )
{
    std::cout << "Enter init_unit_test_suite" << endl;
    boost::unit_test::test_suite* master_test_suite = 
                        BOOST_TEST_SUITE( "MasterTestSuite" );
    master_test_suite->add(BOOST_TEST_CASE(&TestFoo));
    return master_test_suite;

}

しかし、実行時にそれは言う

テスト セットアップ エラー: テスト ツリーが空です

init_unit_test_suite 関数を実行しないのはなぜですか?

4

2 に答える 2

3

実際に boost_unit_test フレームワーク ライブラリに対して動的にリンクしましたか? さらに、手動テスト登録と の定義の組み合わせはBOOST_TEST_MAIN機能しません。ダイナミック ライブラリには、わずかに異なる初期化ルーチンが必要です。

このハードルを回避する最も簡単な方法は、自動テスト登録を使用することです

#define BOOST_TEST_MAIN
#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include <boost/test/unit_test_log.hpp>
#include <boost/filesystem/fstream.hpp>

#include <iostream>

using namespace boost::unit_test;
using namespace std;

BOOST_AUTO_TEST_SUITE(MasterSuite)

BOOST_AUTO_TEST_CASE(TestFoo)
{
    BOOST_CHECK(0==0);
}

BOOST_AUTO_TEST_SUITE_END()

これはより堅牢であり、テストを追加するほど拡張性が高くなります。

于 2013-06-10T13:10:43.207 に答える
3

I had exactly the same issue. Besides switching to automatic test registration, as suggested previously, you can also use static linking, i.e. by replacing

#define BOOST_TEST_DYN_LINK

with

#define BOOST_TEST_STATIC_LINK

This was suggested at the boost mailing list:

The easiest way to fix this is to [...] link with static library.

Dynamic library init API is slightly different since 1.34.1 and this is the cause of the error you see. init_unit_test_suite function is not called in this case.

于 2013-08-02T08:46:35.080 に答える