QTest 命名空间

The QTest 命名空间包含与 Qt 测试相关的所有函数和声明。 更多...

头文件 #include <QTest>
CMakefind_package(Qt6 REQUIRED COMPONENTS Test)
target_link_libraries(mytarget PRIVATE Qt6::Test)
qmakeQT += testlib

类型

枚举KeyAction { 按压, 释放, 点击, 快捷键 }
枚举MouseAction { 鼠标按下, 鼠标释放, 鼠标点击, 鼠标双击, 鼠标移动 }
枚举QBenchmarkMetric { 每秒帧数, 每秒位数, 每秒字节数, 实时时间毫秒, 实时时间纳秒, …, 模拟故障 }
枚举TestFailMode { 中断, 继续 }

函数

voidaddColumn(const char *name, T *dummy = 0)
QTestData &addRow(const char *format, ...)
const char *benchmarkMetricName(QTest::QBenchmarkMetric metric)
const char *benchmarkMetricUnit(QTest::QBenchmarkMetric metric)
QPointingDevice *createTouchDevice(QInputDevice::DeviceType devType = QInputDevice::DeviceType::TouchScreen, QInputDevice::Capabilities caps = QInputDevice::Capability::Position)
const char *currentAppName()
const char *currentDataTag()
boolcurrentTestFailed()
const char *currentTestFunction()
(since 6.5) boolcurrentTestResolved()
(since 6.3) voidfailOnWarning(const QRegularExpression &messagePattern)
(since 6.3) voidfailOnWarning(const char *message)
voidignoreMessage(QtMsgType type, const char *message)
voidignoreMessage(QtMsgType type, const QRegularExpression &messagePattern)
voidkeyClick(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
voidkeyClick(QWindow *window, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
voidkeyClick(QWindow *window, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
voidkeyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
voidkeyClicks(QWidget *widget, const QString &sequence, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
voidkeyEvent(QTest::KeyAction action, QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
voidkeyEvent(QTest::KeyAction action, QWindow *窗口, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyEvent(QTest::KeyAction action, QWindow *窗口, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyEvent(QTest::KeyAction action, QWidget *widget, char ascii, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyPress(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier =Qt::NoModifier, int 延时 = -1)
voidkeyPress(QWindow *窗口, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyPress(QWindow *窗口, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyPress(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyRelease(QWidget *widget, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyRelease(QWindow *窗口, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyRelease(QWindow *窗口, Qt::Key key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeyRelease(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int 延时 = -1)
voidkeySequence(QWindow *窗口, const QKeySequence &keySequence)
voidkeySequence(QWidget *widget, const QKeySequence &keySequence)
voidmouseClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmouseClick(QWindow *窗口, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmouseDClick(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmouseDClick(QWindow *窗口, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmouseMove(QWidget *widget, QPoint pos = QPoint(), int 延时 = -1)
voidmouseMove(QWindow *窗口, QPoint pos = QPoint(), int 延时 = -1)
voidmousePress(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmousePress(QWindow *窗口, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmouseRelease(QWidget *widget, Qt::MouseButton button, Qt::KeyboardModifiers modifier = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
voidmouseRelease(QWindow *窗口, Qt::MouseButton button, Qt::KeyboardModifiers stateKey = Qt::KeyboardModifiers(), QPoint pos = QPoint(), int 延时 = -1)
QTestData &newRow(const char *dataTag)
intqExec(QObject *testObject, int argc = 0, char **argv = nullptr)
intqExec(QObject *testObject, const QStringList &arguments)
QSharedPointer<QTemporaryDir>qExtractTestData(const QString &dirName)
(since 6.5) voidqRegisterTestCase(const QString &name, QTest::TestEntryFunction entryFunction)
(自6.7版本起)voidqSleep(std::chrono::milliseconds msecs)
voidqSleep(int ms)
(自6.7版本起)voidqWait(std::chrono::milliseconds msecs)
voidqWait(int msecs)
(自6.7版本起)boolqWaitFor(Functor predicate, QDeadlineTimer deadline = QDeadlineTimer(std::chrono::seconds{5}))
boolqWaitFor(Functor predicate, int timeout)
boolqWaitForWindowActive(QWindow *window, int timeout = 5000)
boolqWaitForWindowActive(QWidget *widget, int timeout = 5000)
boolqWaitForWindowExposed(QWindow *window, int timeout = 5000)
boolqWaitForWindowExposed(QWidget *widget, int timeout = 5000)
(自6.7版本起)boolqWaitForWindowFocused(QWindow *window, QDeadlineTimer timeout = std::chrono::seconds{5})
(自6.7版本起)boolqWaitForWindowFocused(QWidget *widget, QDeadlineTimer timeout = std::chrono::seconds{5})
voidsetBenchmarkResult(qreal result, QTest::QBenchmarkMetric metric)
char *toHexRepresentation(const char *ba, qsizetype length)
char *toString(const T &value)
char *toString(const std::pair<T1, T2> &pair)
(自6.5版本起)char *toString(const QKeySequence &ks)
char *toString(QSizePolicy::Policy p)
char *toString(QSizePolicy::ControlTypes cts)
char *toString(QSizePolicy::ControlType ct)
char *toString(QSizePolicy sp)
char *toString(const std::tuple<Types...> &tuple)
char *toString(const QStringView &string)
char *toString(const QString &string)
char *toString(const QLatin1StringView &string)
char *toString(const QByteArray &ba)
char *toString(const QTime &time)
char *toString(const QDate &date)
char *toString(const QDateTime &dateTime)
char *toString(const QCborError &c)
char *toString(const QChar &character)
char *toString(const QPoint &point)
char *toString(const QSize &size)
char *toString(const QRect &rectangle)
char *toString(const QPointF &point)
char *toString(const QSizeF &size)
char *toString(const QRectF &rectangle)
char *toString(const QUrl &url)
char *toString(const QUuid &uuid)
char *toString(const QVariant &variant)
char *toString(std::nullptr_t)
char *toString(const QVector2D &v)
char *toString(const QVector3D &v)
char *toString(const QVector4D &v)
QTest::QTouchEventWidgetSequencetouchEvent(QWidget *widget, QPointingDevice *device, bool autoCommit = true)
QTest::QTouchEventSequencetouchEvent(QWindow *window, QPointingDevice *device, bool autoCommit = true)

QBENCHMARK
QBENCHMARK_ONCE
QCOMPARE(actual, expected)
(自6.4版本起) QCOMPARE_EQ(computed, baseline)
(自6.4版本起) QCOMPARE_GE(computed, baseline)
(自6.4版本起) QCOMPARE_GT(computed, baseline)
(自6.4版本起) QCOMPARE_LE(computed, baseline)
(自6.4版本起) QCOMPARE_LT(computed, baseline)
(自6.4版本起) QCOMPARE_NE(computed, baseline)
QEXPECT_FAIL(dataIndex, comment, mode)
QFAIL(message)
QFETCH(type, name)
QFETCH_GLOBAL(type, name)
QFINDTESTDATA(filename)
QSKIP(description)
QTEST(actual, testElement)
QTEST_APPLESS_MAIN(TestClass)
QTEST_GUILESS_MAIN(TestClass)
QTEST_MAIN(TestClass)
QTRY_COMPARE(actual, expected)
(自6.4版本起) QTRY_COMPARE_EQ(computed, baseline)
(自6.4版本起) QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout)
(自6.4版本起) QTRY_COMPARE_GE(computed, baseline)
(自6.4版本起) QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout)
(自6.4版本起) QTRY_COMPARE_GT(computed, baseline)
(自6.4版本起) QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout)
(自6.4版本起) QTRY_COMPARE_LE(computed, baseline)
(自6.4版本起) QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout)
(自6.4版本起) QTRY_COMPARE_LT(computed, baseline)
(自6.4版本起) QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout)
(自6.4版本起) QTRY_COMPARE_NE(computed, baseline)
(自6.4版本起) QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout)
QTRY_COMPARE_WITH_TIMEOUT(actual, expected, timeout)
QTRY_VERIFY2(condition, message)
QTRY_VERIFY(condition)
QTRY_VERIFY2_WITH_TIMEOUT(condition, message, timeout)
QTRY_VERIFY_WITH_TIMEOUT(condition, timeout)
QVERIFY2(condition, message)
QVERIFY(condition)
(自6.3版本起) QVERIFY_THROWS_EXCEPTION(exceptiontype, ...)
(自6.3版本起) QVERIFY_THROWS_NO_EXCEPTION(...)

详细说明

有关如何编写单元测试的信息,请参阅Qt Test概述

class QTouchEventSequence

QTouchEventSequence类用于模拟一系列触摸事件。更多...

类型文档

enum QTest::KeyAction

此枚举描述了按键处理的可能操作。

常量说明
QTest::Press0按键被按下。
QTest::Release1按键被释放。
QTest::Click2键被点击(按下并释放)。
QTest::Shortcut3快捷键被激活。此值在Qt 5.6中被添加。

枚举 QTest::MouseAction

此枚举描述了鼠标处理的可能操作。

常量说明
QTest::MousePress0鼠标按钮被按下。
QTest::MouseRelease1鼠标按钮被释放。
QTest::MouseClick2鼠标按钮被点击(按下并释放)。
QTest::MouseDClick3鼠标按钮被双击(按下并释放两次)。
QTest::MouseMove4鼠标指针已经移动。

枚举 QTest::QBenchmarkMetric

此枚举列出了所有可进行基准测试的内容。

常量说明
QTest::FramesPerSecond0每秒帧率
QTest::BitsPerSecond1每秒比特数
QTest::BytesPerSecond2每秒字节数
QTest::WalltimeMilliseconds3毫秒时钟时间
QTest::WalltimeNanoseconds7纳秒时钟时间
QTest::BytesAllocated8字节内存使用量
QTest::Events6事件计数
QTest::CPUTicks4CPU时间
QTest::CPUMigrations9CPU之间的进程迁移
QTest::CPUCycles10CPU周期
QTest::RefCPUCycles30参考CPU周期
QTest::BusCycles11总线周期
QTest::StalledCycles12停滞周期
QTest::InstructionReads5指令读取
QTest::Instructions13执行的指令
QTest::BranchInstructions14分支类型指令
QTest::BranchMisses15被错误预测的分支指令
QTest::CacheReferences16任何类型的缓存访问
QTest::CacheMisses20任何类型的缓存未命中
QTest::CacheReads17缓存读取 / 加载
QTest::CacheReadMisses21缓存读取 / 加载未命中
QTest::CacheWrites18缓存写入 / 存储
QTest::CacheWriteMisses22缓存写入 / 存储未命中
QTest::CachePrefetches19缓存预取
QTest::CachePrefetchMisses23缓存预取未命中
QTest::ContextSwitches24上下文切换
QTest::PageFaults25任何类型的页面错误
QTest::MinorPage Faults26次要页面错误
QTest::MajorPageFaults27主要页面错误
QTest::AlignmentFaults28由于对齐错误引起的错误
QTest::EmulationFaults29需要软件模拟的错误

注意,WalltimeNanosecondsBytesAllocated仅可通过setBenchmarkResult()使用,并且这些指标的值不能由QTest框架自动提供。

另请参阅QTest::benchmarkMetricName()和QTest::benchmarkMetricUnit()。

枚举 QTest::TestFailMode

此枚举描述了处理已知将失败检查的模式,如通过QVERIFY()或QCOMPARE()宏。无论检查成功还是失败,该模式都会应用。

常量说明
QTest::Abort1终止测试的执行。当在问题检查后没有进一步执行测试的意义时,请使用此模式。
QTest::Continue2在问题检查后继续测试的执行。

另请参阅QEXPECT_FAIL()。

函数文档

模板