gpt4 book ai didi

c++ - 没有 x 服务器的 QtTest 控制台应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:46:56 25 4
gpt4 key购买 nike

我使用 QtTest 框架对我的项目进行了一些单元测试。它是一个控制台应用程序,实际上除了 QtTest 之外,我没有使用任何来自 Qt 的东西。

在我的电脑上它工作正常,但是我在 Travis 上设置持续集成,我得到:

lib_testapp: cannot connect to X server 

我尝试使用 -platform offscreen 运行我的应用程序,就像我在另一个线程上看到的那样,但它似乎没有任何改变。

.pro 中有这些行:

TEMPLATE = app
CONFIG += console c++11
QT += testlib

我的 main.cpp 看起来像这样:

class WatermarkingTests: public QObject
{
Q_OBJECT
private slots:
void initTestCase() { }
void test1() { someTest(); }
void cleanupTestCase() { }
};

QTEST_MAIN(WatermarkingTests)
#include "main.moc"

someTest() 是一个纯数学函数,它对 QVERIFY 进行一些调用。谢谢!

最佳答案

您的问题是以下行:

QTEST_MAIN(WatermarkingTests)

来自文档:

QTEST_MAIN ( TestClass )

Implements a main() function that instantiates an application object and the TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables.

If QT_GUI_LIB is defined, the application object will be a QApplication, otherwise it will be a QCoreApplication. If qmake is used and the configuration includes QT += gui, then QT_GUI_LIB will be defined automatically.

这意味着,您将获得 QApplication 而不是 QCoreApplication,因为默认情况下 QT 变量包含 core 和 gui。您需要使用以下语句来绕过它:

QT = core testlib

请注意,对于 Qt 5,您可以只使用以下宏:

QTEST_GUILESS_MAIN( TestClass)

Implements a main() function that instantiates a QCoreApplication object and the TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables.

Behaves like QTEST_MAIN(), but instantiates a QCoreApplication instead of the QApplication object. Use this macro if your test case doesn't need functionality offered by QApplication, but the event loop is still necessary.

This function was introduced in Qt 5.0.

关于c++ - 没有 x 服务器的 QtTest 控制台应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20872982/

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