2014-02-11 65 views
2

我有一個問題涉及到BOOST.Test框架,並採取下面的例子來說明我的問題: 我建立一個TestClass庫合併所有的測試類,和庫可以是靜態的或動態的。在該庫中的一個典型的功能如下:靜態boost.test庫和動態boost.test庫

__declspec(dllexport) HelloWorld() 
{ 

     int i= 2; 
     int j= 1; 
      BOOST_CHECK(i == j); 
     BOOST_CHECK_EQUAL(i,j); 
} 

然後,我建立一個可執行程序(main.cpp中例如)將調用該庫:

test_suite* init_unit_test_suite(int argc, char* argv[]) 
{ 
    framework::master_test_suite().add(BOOST_TEST_CASE(&HelloWorld)); 

    return 0; 
} 

對於這兩種識別TestClass庫和可執行程序,他們需要BOOST.Test庫。我建立的這個BOOST.Test庫是靜態的。然後我發現:

  • 如果TestClass庫是靜態的,那麼一切都會很順利。

  • 然而,如果識別TestClass庫是動態的,然後我收到以下錯誤:

    未知地點(0)的std :: runtime_error:在 「HelloWorld」 的嚴重錯誤就不能我們 Ë測試框架初始化之前的工具 任何想法?由於

+0

* *爲什麼你把你的測試用例在一個單獨的DLL呢?這有什麼用途? –

+0

@MartinBa感謝您的評論。測試用例放入分離的DLL的原因是分離的DLL可能包含一些可用於非測試目的的函數。 – feelfree

+0

將非測試(即實用程序或「生產」代碼)與測試代碼混合通常是一個糟糕的主意。保持兩個分開,你的生活將會更加容易。這只是簡單地遵循[包裝設計原則](http://en.wikipedia.org/wiki/Package_Principles),我在C++中調用一個庫作爲「包」。 – legalize

回答

2

確保您定義的BOOST_TEST_DYN_LINK:

If you opt to link a test module with the prebuilt dynamic library, this usage is called the dynamic library variant of the UTF. This variant requires you to define the flag BOOST_TEST_DYN_LINK either in a makefile or before the header boost/test/unit_test.hpp inclusion.
The dynamic library variant of the UTF

#define BOOST_TEST_DYN_LINK 
#include <boost/test/unit_test.hpp>