gpt4 book ai didi

unit-testing - 如何从 QTestLib/QTest 获取测试失败的信息

转载 作者:行者123 更新时间:2023-12-01 12:53:49 25 4
gpt4 key购买 nike

我正在使用 QTestLib 库和 QTest 来运行我的单元测试。我在 Windows 7 上工作,并使用 Qt 4.8 和 MVSC 2010 编译器。当我使用以下方式运行测试时:

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 中为每个测试槽显示此消息。我做了一些研究,发现 qtestlog.cpp使用它自己的 messageHandler 来管理测试输出并将结果显示为 PASS 或 FAIL 以及失败消息、行号等。有什么方法可以为我的 GUI 应用程序处理 QTest 的这个 messageHandler?

最佳答案

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

您可以将 stdout 重定向到您的文本框,或者您可以在调用 QTest::qExec 时指定 -o filename 作为参数,并按原样读回文件写出来了。

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

使用 QProcess 的其他一些好处是单元测试中的崩溃或断言不会导致 GUI 崩溃,并且您将能够同时运行多个测试时间而不混淆你的输出。

关于unit-testing - 如何从 QTestLib/QTest 获取测试失败的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10772024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com