Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing 测试失败时如何从QTestLib/QTest获取信息_Unit Testing_Qt_Qtestlib - Fatal编程技术网

Unit testing 测试失败时如何从QTestLib/QTest获取信息

Unit testing 测试失败时如何从QTestLib/QTest获取信息,unit-testing,qt,qtestlib,Unit Testing,Qt,Qtestlib,我正在使用QTestLib库和QTest来运行单元测试。我在Windows7上工作,并使用Qt4.8和MVSC2010编译器。 当我使用以下命令运行测试时: QTest::qExec(TestDateDD/whateverTestClass); 我在控制台中获得输出: ********* Start testing of TestDateDD ********* Config: Using QTest library 4.8.0, Qt 4.8.0 PASS : TestD

我正在使用QTestLib库和QTest来运行单元测试。我在Windows7上工作,并使用Qt4.8和MVSC2010编译器。 当我使用以下命令运行测试时:

QTest::qExec(TestDateDD/whateverTestClass);
我在控制台中获得输出:

********* Start testing of TestDateDD *********
    Config: Using QTest library 4.8.0, Qt 4.8.0
    PASS   : TestDateDD::initTestCase()
    PASS   : TestDateDD::testValidity()
    FAIL!  : TestDateDD::testMonth(2012/7/10) Compared values are not the same
    Actual (date.longMonthName(date.month())): July
    Expected (monthname): June
    ..\UnitTestingPlugiin\TestDateDD.cpp(38) : failure location
    PASS   : TestDateDD::cleanupTestCase()
    Totals: 3 passed, 1 failed, 0 skipped
    ********* Finished testing of TestDateDD *********

但是,我的要求是在GUI中为每个测试槽显示此消息。我做了一些研究,发现使用了自己的messageHandler来管理测试输出,并将结果显示为通过或失败,以及失败消息、行号等。有什么方法可以为我的GUI应用程序处理QTest的messageHandler吗?

无法更改记录器的功能。QAbstractTestLogger硬编码为输出到文件或标准输出,您不能向QTestLog提供自己的实现

您可以将stdout重定向到文本框,或者在调用
QTest::qExec
并在写入文件时读回文件时,可以将
-o filename
指定为参数

但是,我建议您将测试编译成单独的可执行文件,并使用
QProcess
运行它们。它继承了
QIODevice
,将其数据发送到您的窗口应该不难


使用
QProcess
的另外两个好处是,单元测试中的崩溃或断言不会破坏GUI,并且您可以同时运行多个测试,而不会混淆输出。

感谢您的建议,我现在可以将测试结果写入xml文件,然后从中读取。