使用QT组合单元测试和图形输出



我想使用BOOST测试框架,我还需要一个QApplication用于可视化,因为我的一些测试非常清楚地可视化了。因此,我需要调用QApplication exec()作为我的主程序中的最后一件事,只要窗口没有关闭,exec就应该运行。

我尝试了以下代码,但它没有按预期工作(没有窗口),需要手动测试注册。

test_suite*
init_unit_test_suite( int argc, char* argv[] ) {
  QApplication app(argc, argv);
  MainWidget widget(0);
   test_suite* test= BOOST_TEST_SUITE( "Test case template example" );
   test->add(BOOST_TEST_CASE(&free_test_function2);
   //... many more tests
  widget.show();
  app.exec();
  return test;
}

如何使用BOOST_TEST与东西一起像QApplication ?是否也可以使用自动测试注册?

非常感谢,Martin

您可以考虑定义BOOST_TEST_NO_MAIN并编写自己的main函数,该函数调用

::boost::unit_test::unit_test_main( &init_unit_test, argc, argv );

在适当的时间。我怀疑这需要在子线程中完成,因为Qt很可能在exec()内部循环,直到所有窗口关闭。

多亏了thitons的评论和大量的谷歌搜索,我终于得到了它。下面是记录的代码(使用boost 1_44和动态测试库):

//Testing the lib
//###################################################################################
//Setting up boost testing framework
#define BOOST_TEST_NO_MAIN
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE "Unit test for libcommon"
#include <boost/test/unit_test.hpp>   //###################################################################################
#include <QApplication>
#include <QtGui>
using namespace boost::unit_test;
int main(int argc, char *argv[]) {
  (void) argc;
  (void) argv;
  QApplication app(argc, argv);
  QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
  ::boost::unit_test::unit_test_main( &init_unit_test, argc, argv );
  return app.exec();
}
从这一点开始,测试文件可以添加自动测试,也可以在这些测试中创建QWidgets和其他任何东西。例如,QWidgets可以在fixture中实例化,但如果对视觉图像感兴趣,显然不能在fixture销毁时销毁它们(显然不会看到任何东西,因为fixture的拆除是在该套件的所有测试后立即调用的)

最新更新