QTest 名称空间

QTest 名称空间包含 Qt Test 相关的所有函数和声明。 更多...

头: #include <QTest>
qmake: QT += testlib

class QTouchEventSequence

类型

enum AttributeIndex { AI_Undefined, AI_Name, AI_Result, AI_Tests, AI_Failures, …, AI_Iterations }
enum KeyAction { Press, Release, Click, Shortcut }
enum LogElementType { LET_Undefined, LET_Property, LET_Properties, LET_Failure, LET_Error, …, LET_SystemError }
enum MouseAction { MousePress, MouseRelease, MouseClick, MouseDClick, MouseMove }
enum QBenchmarkMetric { FramesPerSecond, BitsPerSecond, BytesPerSecond, WalltimeMilliseconds, WalltimeNanoseconds, …, EmulationFaults }
enum TestFailMode { Abort, Continue }

函数

void addColumn (const char * name , T * dummy = 0)
QTestData & addRow (const char * format , ... )
const char * benchmarkMetricName (QTest::QBenchmarkMetric metric )
const char * benchmarkMetricUnit (QTest::QBenchmarkMetric metric )
QTouchDevice * createTouchDevice (QTouchDevice::DeviceType devType = QTouchDevice::TouchScreen)
const char * currentAppName ()
const char * currentDataTag ()
bool currentTestFailed ()
const char * currentTestFunction ()
void ignoreMessage (QtMsgType type , const char * message )
void ignoreMessage (QtMsgType type , const QRegularExpression & messagePattern )
void keyClick (QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClick (QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClick (QWindow * window , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClick (QWidget * widget , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyClicks (QWidget * widget , const QString & sequence , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyEvent (QTest::KeyAction action , QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyEvent (QTest::KeyAction action , QWindow * window , char ascii , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyEvent (QTest::KeyAction action , QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyEvent (QTest::KeyAction action , QWidget * widget , char ascii , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyPress (QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyPress (QWindow * window , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyPress (QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyPress (QWidget * widget , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyRelease (QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyRelease (QWindow * window , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyRelease (QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keyRelease (QWidget * widget , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)
void keySequence (QWindow * window , const QKeySequence & keySequence )
void keySequence (QWidget * widget , const QKeySequence & keySequence )
void mouseClick (QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
void mouseClick (QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)
void mouseDClick (QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
void mouseDClick (QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)
void mouseMove (QWidget * widget , QPoint pos = QPoint(), int delay = -1)
void mouseMove (QWindow * window , QPoint pos = QPoint(), int delay = -1)
void mousePress (QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
void mousePress (QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)
void mouseRelease (QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)
void mouseRelease (QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)
QTestData & newRow (const char * dataTag )
int qExec (QObject * testObject , int argc = 0, char ** argv = nullptr)
int qExec (QObject * testObject , const QStringList & arguments )
QSharedPointer<QTemporaryDir> qExtractTestData (const QString & dirName )
void qSleep (int ms )
void qWait (int ms )
bool qWaitFor (Functor predicate , int timeout = 5000)
bool qWaitForWindowActive (QWindow * window , int timeout = 5000)
bool qWaitForWindowActive (QWidget * widget , int timeout = 5000)
bool qWaitForWindowExposed (QWindow * window , int timeout = 5000)
bool qWaitForWindowExposed (QWidget * widget , int timeout = 5000)
void setBenchmarkResult (qreal result , QTest::QBenchmarkMetric metric )
char * toHexRepresentation (const char * ba , int length )
char * toString (const T & value )
char * toString (const QPair<T1, T2> & pair )
char * toString (QSizePolicy::Policy p )
char * toString (QSizePolicy::ControlTypes cts )
char * toString (QSizePolicy::ControlType ct )
char * toString (QSizePolicy sp )
char * toString (const std::pair<T1, T2> & pair )
char * toString (const std::tuple<Types...> & tuple )
char * toString (const QStringView & string )
char * toString (const QString & string )
char * toString (const QLatin1String & 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::QTouchEventSequence touchEvent (QWidget * widget , QTouchDevice * device , bool autoCommit )
QTest::QTouchEventSequence touchEvent (QWindow * window , QTouchDevice * device , bool autoCommit )

QBENCHMARK
QBENCHMARK_ONCE
QCOMPARE ( actual , expected )
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 )
QTRY_COMPARE_WITH_TIMEOUT ( actual , expected , timeout )
QTRY_VERIFY2 ( 条件 , message )
QTRY_VERIFY ( 条件 )
QTRY_VERIFY2_WITH_TIMEOUT ( 条件 , message , timeout )
QTRY_VERIFY_WITH_TIMEOUT ( 条件 , timeout )
QVERIFY2 ( 条件 , message )
QVERIFY ( 条件 )
QVERIFY_EXCEPTION_THROWN ( 表达式 , exceptiontype )
QWARN ( message )

详细描述

Qt Test 概述 for information about how to write unit tests.

class QTouchEventSequence

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

类型文档编制

enum QTest:: AttributeIndex

This enum numbers the different tests.

常量
QTest::AI_Undefined -1
QTest::AI_Name 0
QTest::AI_Result 1
QTest::AI_Tests 2
QTest::AI_Failures 3
QTest::AI_Errors 4
QTest::AI_Type 5
QTest::AI_Description 6
QTest::AI_PropertyValue 7
QTest::AI_QTestVersion 8
QTest::AI_QtVersion 9
QTest::AI_File 10
QTest::AI_Line 11
QTest::AI_Metric 12
QTest::AI_Tag 13
QTest::AI_Value 14
QTest::AI_Iterations 15

enum QTest:: KeyAction

This enum describes possible actions for key handling.

常量 描述
QTest::Press 0 键被按下。
QTest::Release 1 键被释放。
QTest::Click 2 键被点击 (按下并释放)。
QTest::Shortcut 3 快捷方式被激活。该值在 Qt 5.6 添加。

enum QTest:: LogElementType

The enum specifies the kinds of test log messages.

常量
QTest::LET_Undefined -1
QTest::LET_Property 0
QTest::LET_Properties 1
QTest::LET_Failure 2
QTest::LET_Error 3
QTest::LET_TestCase 4
QTest::LET_TestSuite 5
QTest::LET_Benchmark 6
QTest::LET_SystemError 7

enum QTest:: MouseAction

This enum describes possible actions for mouse handling.

常量 描述
QTest::MousePress 0 A mouse button is pressed.
QTest::MouseRelease 1 A mouse button is released.
QTest::MouseClick 2 A mouse button is clicked (pressed and released).
QTest::MouseDClick 3 A mouse button is double clicked (pressed and released twice).
QTest::MouseMove 4 The mouse pointer has moved.

enum QTest:: QBenchmarkMetric

This enum lists all the things that can be benchmarked.

常量 描述
QTest::FramesPerSecond 0 Frames per second
QTest::BitsPerSecond 1 Bits per second
QTest::BytesPerSecond 2 Bytes per second
QTest::WalltimeMilliseconds 3 Clock time in milliseconds
QTest::WalltimeNanoseconds 7 Clock time in nanoseconds
QTest::BytesAllocated 8 Memory usage in bytes
QTest::Events 6 Event count
QTest::CPUTicks 4 CPU time
QTest::CPUMigrations 9 Process migrations between CPUs
QTest::CPUCycles 10 CPU cycles
QTest::RefCPUCycles 30 Reference CPU cycles
QTest::BusCycles 11 Bus cycles
QTest::StalledCycles 12 Cycles stalled
QTest::InstructionReads 5 Instruction reads
QTest::Instructions 13 Instructions executed
QTest::BranchInstructions 14 Branch-type instructions
QTest::BranchMisses 15 Branch instructions that were mispredicted
QTest::CacheReferences 16 Cache accesses of any type
QTest::CacheMisses 20 Cache misses of any type
QTest::CacheReads 17 Cache reads / loads
QTest::CacheReadMisses 21 Cache read / load misses
QTest::CacheWrites 18 Cache writes / stores
QTest::CacheWriteMisses 22 Cache write / store misses
QTest::CachePrefetches 19 Cache prefetches
QTest::CachePrefetchMisses 23 Cache prefetch misses
QTest::ContextSwitches 24 Context switches
QTest::PageFaults 25 Page faults of any type
QTest::MinorPageFaults 26 Minor page faults
QTest::MajorPageFaults 27 Major page faults
QTest::AlignmentFaults 28 Faults caused due to misalignment
QTest::EmulationFaults 29 Faults that needed software emulation

注意, WalltimeNanoseconds and BytesAllocated are only provided for use via setBenchmarkResult (), and results in those metrics are not able to be provided automatically by the QTest 框架。

该枚举在 Qt 4.7 引入或被修改。

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

enum QTest:: TestFailMode

This enum describes the modes for handling an expected failure of the QVERIFY () 或 QCOMPARE () 宏。

常量 描述
QTest::Abort 1 Aborts the execution of the test. Use this mode when it doesn't make sense to execute the test any further after the expected failure.
QTest::Continue 2 Continues execution of the test after the expected failure.

另请参阅 QEXPECT_FAIL ().

函数文档编制

template <typename T> void QTest:: addColumn (const char * name , T * dummy = 0)

Adds a column with type T to the current test data. name is the name of the column. dummy is a workaround for buggy compilers and can be ignored.

To populate the column with values, newRow () can be used. Use QFETCH () to fetch the data in the actual test.

范例:

    QTest::addColumn<int>("intval");
    QTest::addColumn<QString>("str");
    QTest::addColumn<double>("dbl");
    QTest::newRow("row1") << 1 << "hello" << 1.5;
					

To add custom types to the testdata, the type must be registered with QMetaType 凭借 Q_DECLARE_METATYPE ().

注意: This macro can only be used in a test's data function that is invoked by the test framework.

数据驱动测试 了解更广泛范例。

另请参阅 QTest::newRow (), QFETCH (),和 QMetaType .

QTestData &QTest:: addRow (const char * format , ... )

Appends a new row to the current test data. The function's arguments are passed to qsnprintf () for formatting according to format 。见 qvsnprintf () documentation for caveats and limitations.

The formatted string will appear as the name of this test data in the test output.

返回 QTestData reference that can be used to stream in data.

范例:

    QTest::addColumn<int>("input");
    QTest::addColumn<QString>("output");
    QTest::addRow("%d", 0) << 0 << QString("0");
    QTest::addRow("%d", 1) << 1 << QString("1");
					

注意: This function can only be used in a test's data function that is invoked by the test framework.

数据驱动测试 了解更广泛范例。

该函数在 Qt 5.9 引入。

另请参阅 addColumn () 和 QFETCH ().

const char *QTest:: benchmarkMetricName ( QTest::QBenchmarkMetric metric )

返回枚举值 metric as a character string.

该函数在 Qt 4.7 引入。

const char *QTest:: benchmarkMetricUnit ( QTest::QBenchmarkMetric metric )

Retuns the units of measure for the specified metric .

该函数在 Qt 4.7 引入。

QTouchDevice *QTest:: createTouchDevice ( QTouchDevice::DeviceType devType = QTouchDevice::TouchScreen)

Creates a dummy touch device of type devType for simulation of touch events.

The touch device will be registered with the QPA window system interface, and deleted automatically when the QCoreApplication is deleted. So you should typically use createTouchDevice() to initialize a QTouchDevice member variable in your test case class, and use the same instance for all tests.

该函数在 Qt 5.8 引入。

另请参阅 QTest::QTouchEventSequence and touchEvent ().

const char *QTest:: currentAppName ()

Returns the name of the binary that is currently executed.

const char *QTest:: currentDataTag ()

Returns the name of the current test data. If the test doesn't have any assigned testdata, the function returns 0.

bool QTest:: currentTestFailed ()

返回 true if the current test function failed, otherwise false.

const char *QTest:: currentTestFunction ()

Returns the name of the test function that is currently executed.

范例:

void MyTestClass::cleanup()
{
    if (qstrcmp(QTest::currentTestFunction(), "myDatabaseTest") == 0) {
        // clean up all database connections
        closeAllDatabases();
    }
}
					

void QTest:: ignoreMessage ( QtMsgType type , const char * message )

Ignores messages created by qDebug (), qInfo () 或 qWarning (). If the message with the corresponding type is outputted, it will be removed from the test log. If the test finished and the message was not outputted, a test failure is appended to the test log.

注意: Invoking this function will only ignore one message. If the message you want to ignore is outputted twice, you have to call ignoreMessage() twice, too.

范例:

QDir dir;
QTest::ignoreMessage(QtWarningMsg, "QDir::mkdir: Empty or null file name(s)");
dir.mkdir("");
					

The example above tests that QDir::mkdir () outputs the right warning when invoked with an invalid file name.

void QTest:: ignoreMessage ( QtMsgType type , const QRegularExpression & messagePattern )

这是重载函数。

Ignores messages created by qDebug (), qInfo () 或 qWarning (). If the message matching messagePattern with the corresponding type is outputted, it will be removed from the test log. If the test finished and the message was not outputted, a test failure is appended to the test log.

注意: Invoking this function will only ignore one message. If the message you want to ignore is outputted twice, you have to call ignoreMessage() twice, too.

该函数在 Qt 5.3 引入。

void QTest:: keyClick ( QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

Simulates clicking of key 采用可选 modifier widget 。若 delay is larger than 0, the test will wait for delay milliseconds before clicking the key.

范例:

QTest::keyClick(myWidget, Qt::Key_Escape);
QTest::keyClick(myWidget, Qt::Key_Escape, Qt::ShiftModifier, 200);
					

The first example above simulates clicking the escape key on myWidget without any keyboard modifiers and without delay. The second example simulates clicking shift-escape on myWidget following a 200 ms delay of the test.

另请参阅 QTest::keyClicks ().

void QTest:: keyClick ( QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates clicking of key 采用可选 modifier window 。若 delay is larger than 0, the test will wait for delay milliseconds before clicking the key.

范例:

QTest::keyClick(&myWindow, Qt::Key_Escape);
QTest::keyClick(&myWindow, Qt::Key_Escape, Qt::ShiftModifier, 200);
					

The first example above simulates clicking the escape key on myWindow without any keyboard modifiers and without delay. The second example simulates clicking shift-escape on myWindow following a 200 ms delay of the test.

该函数在 Qt 5.0 引入。

另请参阅 QTest::keyClicks ().

void QTest:: keyClick ( QWindow * window , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates clicking of key 采用可选 modifier window 。若 delay is larger than 0, the test will wait for delay milliseconds before clicking the key.

范例:

QWidget myWindow;
QTest::keyClick(&myWindow, Qt::Key_Tab);
					

The example above simulates clicking a on myWindow without any keyboard modifiers and without delay of the test.

该函数在 Qt 5.0 引入。

另请参阅 QTest::keyClicks ().

void QTest:: keyClick ( QWidget * widget , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates clicking of key 采用可选 modifier widget 。若 delay is larger than 0, the test will wait for delay milliseconds before clicking the key.

范例:

QTest::keyClick(myWidget, 'a');
					

The example above simulates clicking a on myWidget without any keyboard modifiers and without delay of the test.

另请参阅 QTest::keyClicks ().

void QTest:: keyClicks ( QWidget * widget , const QString & sequence , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

Simulates clicking a sequence of keys on a widget . Optionally, a keyboard modifier can be specified as well as a delay (in milliseconds) of the test before each key click.

范例:

QTest::keyClicks(myWidget, "hello world");
					

The example above simulates clicking the sequence of keys representing "hello world" on myWidget without any keyboard modifiers and without delay of the test.

另请参阅 QTest::keyClick ().

void QTest:: keyEvent ( QTest::KeyAction action , QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

Sends a Qt key event to widget 采用给定 key and an associated action . Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test before sending the event.

void QTest:: keyEvent ( QTest::KeyAction action , QWindow * window , char ascii , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Sends a Qt key event to window with the given key ascii and an associated action . Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test before sending the event.

该函数在 Qt 5.0 引入。

void QTest:: keyEvent ( QTest::KeyAction action , QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Sends a Qt key event to window 采用给定 key and an associated action . Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test before sending the event.

该函数在 Qt 5.0 引入。

void QTest:: keyEvent ( QTest::KeyAction action , QWidget * widget , char ascii , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Sends a Qt key event to widget with the given key ascii and an associated action . Optionally, a keyboard modifier can be specified, as well as a delay (in milliseconds) of the test before sending the event.

void QTest:: keyPress ( QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

Simulates pressing a key 采用可选 modifier widget 。若 delay is larger than 0, the test will wait for delay milliseconds before pressing the key.

注意: At some point you should release the key using keyRelease ().

另请参阅 QTest::keyRelease () 和 QTest::keyClick ().

void QTest:: keyPress ( QWindow * window , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates pressing a key 采用可选 modifier window 。若 delay is larger than 0, the test will wait for delay milliseconds before pressing the key.

注意: At some point you should release the key using keyRelease ().

该函数在 Qt 5.0 引入。

另请参阅 QTest::keyRelease () 和 QTest::keyClick ().

void QTest:: keyPress ( QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates pressing a key 采用可选 modifier window 。若 delay is larger than 0, the test will wait for delay milliseconds before pressing the key.

注意: At some point you should release the key using keyRelease ().

该函数在 Qt 5.0 引入。

另请参阅 QTest::keyRelease () 和 QTest::keyClick ().

void QTest:: keyPress ( QWidget * widget , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates pressing a key 采用可选 modifier widget 。若 delay is larger than 0, the test will wait for delay milliseconds before pressing the key.

注意: At some point you should release the key using keyRelease ().

另请参阅 QTest::keyRelease () 和 QTest::keyClick ().

void QTest:: keyRelease ( QWidget * widget , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

Simulates releasing a key 采用可选 modifier widget 。若 delay is larger than 0, the test will wait for delay milliseconds before releasing the key.

另请参阅 QTest::keyPress () 和 QTest::keyClick ().

void QTest:: keyRelease ( QWindow * window , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates releasing a key 采用可选 modifier window 。若 delay is larger than 0, the test will wait for delay milliseconds before releasing the key.

该函数在 Qt 5.0 引入。

另请参阅 QTest::keyClick ().

void QTest:: keyRelease ( QWindow * window , Qt::Key key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates releasing a key 采用可选 modifier window 。若 delay is larger than 0, the test will wait for delay milliseconds before releasing the key.

该函数在 Qt 5.0 引入。

另请参阅 QTest::keyPress () 和 QTest::keyClick ().

void QTest:: keyRelease ( QWidget * widget , char key , Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay = -1)

这是重载函数。

Simulates releasing a key 采用可选 modifier widget 。若 delay is larger than 0, the test will wait for delay milliseconds before releasing the key.

另请参阅 QTest::keyClick ().

void QTest:: keySequence ( QWindow * window , const QKeySequence & keySequence )

这是重载函数。

Simulates typing of keySequence window .

该函数在 Qt 5.10 引入。

另请参阅 QTest::keyClick () 和 QTest::keyClicks ().

void QTest:: keySequence ( QWidget * widget , const QKeySequence & keySequence )

这是重载函数。

Simulates typing of keySequence widget .

该函数在 Qt 5.10 引入。

另请参阅 QTest::keyClick () 和 QTest::keyClicks ().

void QTest:: mouseClick ( QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates clicking a mouse button 采用可选 modifier widget . The position of the click is defined by pos ; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before pressing and before releasing the button.

另请参阅 QTest::mousePress () 和 QTest::mouseRelease ().

void QTest:: mouseClick ( QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)

这是重载函数。

Simulates clicking a mouse button 采用可选 stateKey modifier on a window . The position of the click is defined by pos ; the default position is the center of the window. If delay is specified, the test will wait for the specified amount of milliseconds before pressing and before releasing the button.

该函数在 Qt 5.0 引入。

另请参阅 QTest::mousePress () 和 QTest::mouseRelease ().

void QTest:: mouseDClick ( QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates double clicking a mouse button 采用可选 modifier widget . The position of the click is defined by pos ; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before each press and release.

另请参阅 QTest::mouseClick ().

void QTest:: mouseDClick ( QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)

这是重载函数。

Simulates double clicking a mouse button 采用可选 stateKey modifier on a window . The position of the click is defined by pos ; the default position is the center of the window. If delay is specified, the test will wait for the specified amount of milliseconds before each press and release.

该函数在 Qt 5.0 引入。

另请参阅 QTest::mouseClick ().

void QTest:: mouseMove ( QWidget * widget , QPoint pos = QPoint(), int delay = -1)

Moves the mouse pointer to a widget 。若 pos is not specified, the mouse pointer moves to the center of the widget. If a delay (in milliseconds) is given, the test will wait before moving the mouse pointer.

void QTest:: mouseMove ( QWindow * window , QPoint pos = QPoint(), int delay = -1)

这是重载函数。

Moves the mouse pointer to a window 。若 pos is not specified, the mouse pointer moves to the center of the window. If a delay (in milliseconds) is given, the test will wait before moving the mouse pointer.

该函数在 Qt 5.0 引入。

void QTest:: mousePress ( QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates pressing a mouse button 采用可选 modifier widget . The position is defined by pos ; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before the press.

另请参阅 QTest::mouseRelease () 和 QTest::mouseClick ().

void QTest:: mousePress ( QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)

这是重载函数。

Simulates pressing a mouse button 采用可选 stateKey modifier on a window . The position is defined by pos ; the default position is the center of the window. If delay is specified, the test will wait for the specified amount of milliseconds before the press.

该函数在 Qt 5.0 引入。

另请参阅 QTest::mouseRelease () 和 QTest::mouseClick ().

void QTest:: mouseRelease ( QWidget * widget , Qt::MouseButton button , Qt::KeyboardModifiers modifier = 0, QPoint pos = QPoint(), int delay = -1)

Simulates releasing a mouse button 采用可选 modifier widget . The position of the release is defined by pos ; the default position is the center of the widget. If delay is specified, the test will wait for the specified amount of milliseconds before releasing the button.

另请参阅 QTest::mousePress () 和 QTest::mouseClick ().

void QTest:: mouseRelease ( QWindow * window , Qt::MouseButton button , Qt::KeyboardModifiers stateKey = 0, QPoint pos = QPoint(), int delay = -1)

这是重载函数。

Simulates releasing a mouse button 采用可选 stateKey modifier on a window . The position of the release is defined by pos ; the default position is the center of the window. If delay is specified, the test will wait for the specified amount of milliseconds before releasing the button.

该函数在 Qt 5.0 引入。

另请参阅 QTest::mousePress () 和 QTest::mouseClick ().

QTestData &QTest:: newRow (const char * dataTag )

Appends a new row to the current test data. dataTag is the name of the testdata that will appear in the test output. Returns a QTestData reference that can be used to stream in data.

范例:

void MyTestClass::addSingleStringRows()
{
    QTest::addColumn<QString>("aString");
    QTest::newRow("just hello") << QString("hello");
    QTest::newRow("a null string") << QString();
}
					

注意: This macro can only be used in a test's data function that is invoked by the test framework.

数据驱动测试 了解更广泛范例。

另请参阅 addColumn () 和 QFETCH ().

int QTest:: qExec ( QObject * testObject , int argc = 0, char ** argv = nullptr)

Executes tests declared in testObject . In addition, the private slots initTestCase() , cleanupTestCase() , init() and cleanup() are executed if they exist. See 创建测试 了解更多细节。

Optionally, the command line arguments argc and argv can be provided. For a list of recognized arguments, read Qt Test 命令行自变量 .

The following example will run all tests in MyTestObject :

MyTestObject test1;
QTest::qExec(&test1);
					

This function returns 0 if no tests failed, or a value other than 0 if one or more tests failed or in case of unhandled exceptions. (Skipped tests do not influence the return value.)

For stand-alone test applications, the convenience macro QTEST_MAIN () can be used to declare a main() function that parses the command line arguments and executes the tests, avoiding the need to call this function explicitly.

The return value from this function is also the exit code of the test application when the QTEST_MAIN () macro is used.

For stand-alone test applications, this function should not be called more than once, as command-line options for logging test output to files and executing individual test functions will not behave correctly.

Note: This function is not reentrant, only one test can run at a time. A test that was executed with qExec() can't run another test via qExec() and threads are not allowed to call qExec() simultaneously.

If you have programatically created the arguments, as opposed to getting them from the arguments in main() , it is likely of interest to use QTest::qExec( QObject *, const QStringList &) since it is Unicode safe.

另请参阅 QTEST_MAIN ().

int QTest:: qExec ( QObject * testObject , const QStringList & arguments )

这是重载函数。

Behaves identically to qExec( QObject *, int, char**) but takes a QStringList of arguments 而不是 char** 列表。

该函数在 Qt 4.4 引入。

QSharedPointer < QTemporaryDir > QTest:: qExtractTestData (const QString & dirName )

Extracts a directory from resources to disk. The content is extracted recursively to a temporary folder. The extracted content is removed automatically once the last reference to the return value goes out of scope.

dirName is the name of the directory to extract from resources.

Returns the temporary directory where the data was extracted or null in case of errors.

void QTest:: qSleep ( int ms )

Sleeps for ms milliseconds, blocking execution of the test. qSleep() will not do any event processing and leave your test unresponsive. Network communication might time out while sleeping. Use QTest::qWait () to do non-blocking sleeping.

ms 必须大于 0。

注意: The qSleep() function calls either nanosleep() on unix or Sleep() on windows, so the accuracy of time spent in qSleep() depends on the operating system.

范例:

QTest::qSleep(250);
					

另请参阅 QTest::qWait ().

void QTest:: qWait ( int ms )

等待 ms milliseconds. While waiting, events will be processed and your test will stay responsive to user interface events or network communication.

范例:

    int i = 0;
    while (myNetworkServerNotResponding() && i++ < 50)
        QTest::qWait(250);
					

The code above will wait until the network server is responding for a maximum of about 12.5 seconds.

另请参阅 QTest::qSleep () 和 QSignalSpy::wait ().

template <typename Functor> bool QTest:: qWaitFor ( Functor predicate , int timeout = 5000)

等待 timeout milliseconds or until the predicate returns true.

返回 true predicate returned true at any point, otherwise returns false .

范例:

The code above will wait for the object to become ready, for a maximum of three seconds.

该函数在 Qt 5.10 引入。

bool QTest:: qWaitForWindowActive ( QWindow * window , int timeout = 5000)

返回 true ,若 window is active within timeout milliseconds. Otherwise returns false .

The method is useful in tests that call QWindow::show () and rely on the window actually being active (i.e. being visible and having focus) before proceeding.

注意: The method will time out and return false if another window prevents window from becoming active.

注意: Since focus is an exclusive property, window may loose its focus to another window at any time - even after the method has returned true .

该函数在 Qt 5.0 引入。

另请参阅 qWaitForWindowExposed () 和 QWindow::isActive ().

bool QTest:: qWaitForWindowActive ( QWidget * widget , int timeout = 5000)

返回 true if widget is active within timeout milliseconds. Otherwise returns false .

The method is useful in tests that call QWidget::show () and rely on the widget actually being active (i.e. being visible and having focus) before proceeding.

注意: The method will time out and return false if another window prevents widget from becoming active.

注意: Since focus is an exclusive property, widget may loose its focus to another window at any time - even after the method has returned true .

该函数在 Qt 5.0 引入。

另请参阅 qWaitForWindowExposed () 和 QWidget::isActiveWindow ().

bool QTest:: qWaitForWindowExposed ( QWindow * window , int timeout = 5000)

返回 true ,若 window is exposed within timeout milliseconds. Otherwise returns false .

The method is useful in tests that call QWindow::show () and rely on the window actually being being visible before proceeding.

注意: A window mapped to screen may still not be considered exposed, if the window client area is not visible, e.g. because it is completely covered by other windows. In such cases, the method will time out and return false .

该函数在 Qt 5.0 引入。

另请参阅 qWaitForWindowActive () 和 QWindow::isExposed ().

bool QTest:: qWaitForWindowExposed ( QWidget * widget , int timeout = 5000)

返回 true if widget is exposed within timeout milliseconds. Otherwise returns false .

The method is useful in tests that call QWidget::show () and rely on the widget actually being being visible before proceeding.

注意: A window mapped to screen may still not be considered exposed, if the window client area is not visible, e.g. because it is completely covered by other windows. In such cases, the method will time out and return false .

该函数在 Qt 5.0 引入。

另请参阅 qWaitForWindowActive (), QWidget::isVisible (),和 QWindow::isExposed ().

void QTest:: setBenchmarkResult ( qreal result , QTest::QBenchmarkMetric metric )

Sets the benchmark result for this test function to result .

Use this function if you want to report benchmark results without using the QBENCHMARK macro. Use metric to specify how Qt Test should interpret the results.

The context for the result will be the test function name and any data tag from the _data function. This function can only be called once in each test function, subsequent calls will replace the earlier reported results.

Note that the -iterations command line argument has no effect on test functions without the QBENCHMARK macro.

该函数在 Qt 4.7 引入。

char *QTest:: toHexRepresentation (const char * ba , int length )

Returns a pointer to a string that is the string ba represented as a space-separated sequence of hex characters. If the input is considered too long, it is truncated. A trucation is indicated in the returned string as an ellipsis at the end. The caller has ownership of the returned pointer and must ensure it is later passed to operator delete[].

length is the length of the string ba .

template <typename T> char *QTest:: toString (const T & value )

Returns a textual representation of value . This function is used by QCOMPARE () to output verbose information in case of a test failure.

You can add specializations or overloads of this function to your test to enable verbose output.

注意: Starting with Qt 5.5, you should prefer to provide a toString() function in the type's namespace instead of specializing this template. If your code needs to continue to work with the QTestLib from Qt 5.4 or earlier, you need to continue to use specialization.

注意: The caller of toString() must delete the returned data using delete[] . Your implementation should return a string created with new[] or qstrdup (). The easiest way to do so is to create a QByteArray or QString and call QTest::toString() on it (see second example below).

Example for specializing (Qt ≤ 5.4):

namespace QTest {
    template<>
    char *toString(const MyPoint &point)
    {
        const QByteArray ba("MyPoint("
                            + QByteArray::number(point.x()) + ", "
                            + QByteArray::number(point.y()) + ')');
        return qstrdup(ba.data());
    }
}
					

The example above defines a toString() specialization for a class called MyPoint . Whenever a comparison of two instances of MyPoint fails, QCOMPARE () will call this function to output the contents of MyPoint to the test log.

Same example, but with overloading (Qt ≥ 5.5):

namespace {
    char *toString(const MyPoint &point)
    {
        return QTest::toString("MyPoint(" +
                               QByteArray::number(point.x()) + ", " +
                               QByteArray::number(point.y()) + ')');
    }
}
					

另请参阅 QCOMPARE ().

template <typename T1, typename T2> char *QTest:: toString (const QPair < T1 , T2 > & pair )

这是重载函数。

Returns a textual representation of the pair .

该函数在 Qt 5.11 引入。

char *QTest:: toString ( QSizePolicy::Policy p )

这是重载函数。

Returns a textual representation of policy p .

该函数在 Qt 5.5 引入。

char *QTest:: toString ( QSizePolicy::ControlTypes cts )

这是重载函数。

Returns a textual representation of control types cts .

该函数在 Qt 5.5 引入。

char *QTest:: toString ( QSizePolicy::ControlType ct )

这是重载函数。

Returns a textual representation of control type ct .

该函数在 Qt 5.5 引入。

char *QTest:: toString ( QSizePolicy sp )

这是重载函数。

Returns a textual representation of size policy sp .

该函数在 Qt 5.5 引入。

template <typename T1, typename T2> char *QTest:: toString (const std::pair < T1 , T2 > & pair )

这是重载函数。

Returns a textual representation of the pair .

该函数在 Qt 5.11 引入。

template <typename Types> char *QTest:: toString (const std::tuple < 类型 ...> & tuple )

这是重载函数。

Returns a textual representation of the given tuple .

该函数在 Qt 5.12 引入。

char *QTest:: toString (const QStringView & string )

这是重载函数。

Returns a textual representation of the given string .

该函数在 Qt 5.11 引入。

char *QTest:: toString (const QString & string )

这是重载函数。

Returns a textual representation of the given string .

char *QTest:: toString (const QLatin1String & string )

这是重载函数。

Returns a textual representation of the given string .

char *QTest:: toString (const QByteArray & ba )

这是重载函数。

Returns a textual representation of the byte array ba .

另请参阅 QTest::toHexRepresentation ().

char *QTest:: toString (const QTime & time )

这是重载函数。

Returns a textual representation of the given time .

char *QTest:: toString (const QDate & date )

这是重载函数。

Returns a textual representation of the given date .

char *QTest:: toString (const QDateTime & dateTime )

这是重载函数。

Returns a textual representation of the date and time specified by dateTime .

char *QTest:: toString (const QCborError & c )

这是重载函数。

Returns a textual representation of the given CBOR error c .

该函数在 Qt 5.12 引入。

char *QTest:: toString (const QChar & character )

这是重载函数。

Returns a textual representation of the given character .

char *QTest:: toString (const QPoint & point )

这是重载函数。

Returns a textual representation of the given point .

char *QTest:: toString (const QSize & size )

这是重载函数。

Returns a textual representation of the given size .

char *QTest:: toString (const QRect & rectangle )

这是重载函数。

Returns a textual representation of the given rectangle .

char *QTest:: toString (const QPointF & point )

这是重载函数。

Returns a textual representation of the given point .

char *QTest:: toString (const QSizeF & size )

这是重载函数。

Returns a textual representation of the given size .

char *QTest:: toString (const QRectF & rectangle )

这是重载函数。

Returns a textual representation of the given rectangle .

char *QTest:: toString (const QUrl & url )

这是重载函数。

Returns a textual representation of the given url .

该函数在 Qt 4.4 引入。

char *QTest:: toString (const QUuid & uuid )

这是重载函数。

Returns a textual representation of the given uuid .

该函数在 Qt 5.11 引入。

char *QTest:: toString (const QVariant & variant )

这是重载函数。

Returns a textual representation of the given variant .

char *QTest:: toString ( std::nullptr_t )

这是重载函数。

Returns a string containing nullptr .

该函数在 Qt 5.8 引入。

char *QTest:: toString (const QVector2D & v )

这是重载函数。

Returns a textual representation of the 2D vector v .

该函数在 Qt 5.11 引入。

char *QTest:: toString (const QVector3D & v )

这是重载函数。

Returns a textual representation of the 3D vector v .

该函数在 Qt 5.11 引入。

char *QTest:: toString (const QVector4D & v )

这是重载函数。

Returns a textual representation of the 4D vector v .

该函数在 Qt 5.11 引入。

QTest::QTouchEventSequence QTest:: touchEvent ( QWidget * widget , QTouchDevice * device , bool autoCommit )

创建并返回 QTouchEventSequence device to simulate events for widget .

When adding touch events to the sequence, widget will also be used to translate the position provided to screen coordinates, unless another widget is provided in the respective calls to press(), move() etc.

The touch events are committed to the event system when the destructor of the QTouchEventSequence is called (ie when the object returned runs out of scope), unless autoCommit is set to false. When autoCommit is false, commit() has to be called manually.

createTouchDevice () can be called to create a test touch device for use with this function.

QTest::QTouchEventSequence QTest:: touchEvent ( QWindow * window , QTouchDevice * device , bool autoCommit )

创建并返回 QTouchEventSequence device to simulate events for window .

When adding touch events to the sequence, window will also be used to translate the position provided to screen coordinates, unless another window is provided in the respective calls to press(), move() etc.

The touch events are committed to the event system when the destructor of the QTouchEventSequence is called (ie when the object returned runs out of scope), unless autoCommit is set to false. When autoCommit is false, commit() has to be called manually.

createTouchDevice () can be called to create a test touch device for use with this function.

该函数在 Qt 5.0 引入。

宏文档编制

QBENCHMARK

This macro is used to measure the performance of code within a test. The code to be benchmarked is contained within a code block following this macro.

例如:

void TestBenchmark::simple()
{
    QString str1 = QLatin1String("This is a test string");
    QString str2 = QLatin1String("This is a test string");
    QCOMPARE(str1.localeAwareCompare(str2), 0);
    QBENCHMARK {
        str1.localeAwareCompare(str2);
    }
}
					

另请参阅 创建基准测试 and 编写基准测试 .

QBENCHMARK_ONCE

The QBENCHMARK_ONCE macro is for measuring performance of a code block by running it once.

This macro is used to measure the performance of code within a test. The code to be benchmarked is contained within a code block following this macro.

Unlike QBENCHMARK, the contents of the contained code block is only run once. The elapsed time will be reported as "0" if it's too short to be measured by the selected backend.

该函数在 Qt 4.6 引入。

另请参阅 创建基准测试 and 编写基准测试 .

QCOMPARE ( actual , expected )

The QCOMPARE() macro compares an actual value to an expected value using the equality operator. If actual and expected match, execution continues. If not, a failure is recorded in the test log and the test function returns without attempting any later checks.

Always respect QCOMPARE() parameter semantics. The first parameter passed to it should always be the actual value produced by the code-under-test, while the second parameter should always be the expected value. When the values don't match, QCOMPARE() prints them with the labels Actual and Expected . If the parameter order is swapped, debugging a failing test can be confusing and tests expecting zero may fail due to rounding errors.

When comparing floating-point types ( float , double ,和 qfloat16 ), qFuzzyCompare () is used for finite values. If qFuzzyIsNull () is true for both values, they are also considered equal. Infinities match if they have the same sign, and any NaN as actual value matches with any NaN as expected value (even though NaN != NaN, even when they're identical).

QCOMPARE() tries to output the contents of the values if the comparison fails, so it is visible from the test log why the comparison failed.

范例:

QCOMPARE(QString("hello").toUpper(), QString("HELLO"));
					

注意: This macro can only be used in a test function that is invoked by the test framework.

For your own classes, you can use QTest::toString () to format values for outputting into the test log.

范例:

char *toString(const MyType &t)
{
    char *repr = new char[t.reprSize()];
    t.writeRepr(repr);
    return repr;
}
					

The return from toString() 必须为 new char [] . That is, it shall be released with delete[] (rather than free() or plain delete ) once the calling code is done with it.

另请参阅 QVERIFY (), QTRY_COMPARE (), QTest::toString (),和 QEXPECT_FAIL ().

QEXPECT_FAIL ( dataIndex , comment , mode )

The QEXPECT_FAIL() macro marks the next QCOMPARE () 或 QVERIFY () as an expected failure. Instead of adding a failure to the test log, an expected failure will be reported.

QVERIFY () 或 QCOMPARE () is marked as an expected failure, but passes instead, an unexpected pass (XPASS) is written to the test log.

参数 dataIndex describes for which entry in the test data the failure is expected. Pass an empty string ( "" ) if the failure is expected for all entries or if no test data exists.

comment will be appended to the test log for the expected failure.

mode QTest::TestFailMode and sets whether the test should continue to execute or not.

注意: This macro can only be used in a test function that is invoked by the test framework.

Example 1:

QEXPECT_FAIL("", "Will fix in the next release", Continue);
QCOMPARE(i, 42);
QCOMPARE(j, 43);
					

In the example above, an expected fail will be written into the test output if the variable i is not 42. If the variable i is 42, an unexpected pass is written instead. The QEXPECT_FAIL() has no influence on the second QCOMPARE () statement in the example.

Example 2:

QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort);
QCOMPARE(i, 42);
					

The above testfunction will not continue executing for the test data entry data27 .

另请参阅 QTest::TestFailMode , QVERIFY (),和 QCOMPARE ().

QFAIL ( message )

This macro can be used to force a test failure. The test stops executing and the failure message is appended to the test log.

注意: This macro can only be used in a test function that is invoked by the test framework.

范例:

if (sizeof(int) != 4)
    QFAIL("This test has not been ported to this platform yet.");
					

QFETCH ( type , name )

The fetch macro creates a local variable named name with the type type on the stack. The name and type must match a column from the test's data table. This is asserted and the test will abort if the assertion fails.

Assuming a test has the following data:

void TestQString::toInt_data()
{
    QTest::addColumn<QString>("aString");
    QTest::addColumn<int>("expected");
    QTest::newRow("positive value") << "42" << 42;
    QTest::newRow("negative value") << "-42" << -42;
    QTest::newRow("zero") << "0" << 0;
}
					

The test data has two elements, a QString called aString and an integer called expected . To fetch these values in the actual test:

void TestQString::toInt()
{
     QFETCH(QString, aString);
     QFETCH(int, expected);
     QCOMPARE(aString.toInt(), expected);
}
					

aString and expected are variables on the stack that are initialized with the current test data.

注意: This macro can only be used in a test function that is invoked by the test framework. The test function must have a _data function.

QFETCH_GLOBAL ( type , name )

This macro fetches a variable named name with the type type from a row in the global data table. The name and type must match a column in the global data table. This is asserted and the test will abort if the assertion fails.

Assuming a test has the following data:

void TestQLocale::initTestCase_data()
{
    QTest::addColumn<QLocale>("locale");
    QTest::newRow("C") << QLocale::c();
    QTest::newRow("UKish") << QLocale("en_GB");
    QTest::newRow("USAish") << QLocale(QLocale::English, QLocale::UnitedStates);
}
void TestQLocale::roundTripInt_data()
{
    QTest::addColumn<int>("number");
    QTest::newRow("zero") << 0;
    QTest::newRow("one") << 1;
    QTest::newRow("two") << 2;
    QTest::newRow("ten") << 10;
}
					

The test's own data is a single number per row. In this case, initTestCase_data() also supplies a locale per row. Therefore, this test will be run with every combination of locale from the latter and number from the former. Thus, with four rows in the global table and three in the local, the test function is run for 12 distinct test-cases (4 * 3 = 12).

void TestQLocale::roundTripInt()
{
    QFETCH_GLOBAL(QLocale, locale);
    QFETCH(int, number);
    bool ok;
    QCOMPARE(locale.toInt(locale.toString(number), &ok), number);
    QVERIFY(ok);
}
					

The locale is read from the global data table using QFETCH_GLOBAL(), and the number is read from the local data table using QFETCH ().

注意: This macro can only be used in test methods of a class with an initTestCase_data() 方法。

QFINDTESTDATA ( filename )

返回 QString for the testdata file referred to by filename , or an empty QString if the testdata file could not be found.

This macro allows the test to load data from an external file without hardcoding an absolute filename into the test, or using relative paths which may be error prone.

The returned path will be the first path from the following list which resolves to an existing file or directory:

If the named file/directory does not exist at any of these locations, a warning is printed to the test log.

例如,在此代码中:

bool tst_MyXmlParser::parse()
{
    MyXmlParser parser;
    QString input = QFINDTESTDATA("testxml/simple1.xml");
    QVERIFY(parser.parse(input));
}
					

The testdata file will be resolved as the first existing file from:

  • /home/user/build/myxmlparser/tests/tst_myxmlparser/testxml/simple1.xml
  • /usr/local/Qt-5.0.0/tests/tst_myxmlparser/testxml/simple1.xml
  • /home/user/sources/myxmlparser/tests/tst_myxmlparser/testxml/simple1.xml

This allows the test to find its testdata regardless of whether the test has been installed, and regardless of whether the test's build tree is equal to the test's source tree.

注意: reliable detection of testdata from the source directory requires either that qmake is used, or the QT_TESTCASE_BUILDDIR macro is defined to point to the working directory from which the compiler is invoked, or only absolute paths to the source files are passed to the compiler. Otherwise, the absolute path of the source directory cannot be determined.

注意: For tests that use the QTEST_APPLESS_MAIN () macro to generate a main() function, QFINDTESTDATA will not attempt to find test data relative to QCoreApplication::applicationDirPath (). In practice, this means that tests using QTEST_APPLESS_MAIN() will fail to find their test data if run from a shadow build tree.

该函数在 Qt 5.0 引入。

QSKIP ( description )

If called from a test function, the QSKIP() macro stops execution of the test without adding a failure to the test log. You can use it to skip tests that wouldn't make sense in the current configuration. For example, a test of font rendering may call QSKIP() if the needed fonts are not installed on the test system.

文本 description is appended to the test log and should contain an explanation of why the test couldn't be executed.

If the test is data-driven, each call to QSKIP() in the test function will skip only the current row of test data, so an unconditional call to QSKIP() will produce one skip message in the test log for each row of test data.

If called from an _data function, the QSKIP() macro will stop execution of the _data function and will prevent execution of the associated test function. This entirely omits a data-driven test. To omit individual rows, make them conditional by using a simple if (condition) newRow(...) << ... _data function, instead of using QSKIP() in the test function.

If called from initTestCase_data() , the QSKIP() macro will skip all test and _data functions. If called from initTestCase() when there is no initTestCase_data() , or when it only sets up one row, QSKIP() will likewise skip the whole test. However, if initTestCase_data() contains more than one row, then initTestCase() is called (followed by each test and finally the wrap-up) once per row of it. Therefore, a call to QSKIP() in initTestCase() will merely skip all test functions for the current row of global data, set up by initTestCase_data() .

注意: This macro can only be used in a test function or _data function that is invoked by the test framework.

范例:

if (!QSqlDatabase::drivers().contains("SQLITE"))
    QSKIP("This test requires the SQLITE database driver");
					
					
跳过已知 Bug

If a test exposes a known bug that will not be fixed immediately, use the QEXPECT_FAIL () macro to document the failure and reference the bug tracking identifier for the known issue. When the test is run, expected failures will be marked as XFAIL in the test output and will not be counted as failures when setting the test program's return code. If an expected failure does not occur, the XPASS (unexpected pass) will be reported in the test output and will be counted as a test failure.

For known bugs, QEXPECT_FAIL () is better than QSKIP() because a developer cannot fix the bug without an XPASS result reminding them that the test needs to be updated too. If QSKIP() is used, there is no reminder to revise or re-enable the test, without which subsequent regressions will not be reported.

另请参阅 QEXPECT_FAIL () 和 选择适当机制以排除测试 .

QTEST ( actual , testElement )

QTEST() is a convenience macro for QCOMPARE () that compares the value actual with the element testElement from the test's data. If there is no such element, the test asserts.

Apart from that, QTEST() behaves exactly as QCOMPARE ().

Instead of writing:

QFETCH(QString, myString);
QCOMPARE(QString("hello").toUpper(), myString);
					

可以编写:

QTEST(QString("hello").toUpper(), "myString");
					

另请参阅 QCOMPARE ().

QTEST_APPLESS_MAIN ( TestClass )

Implements a main() function that executes all tests in TestClass .

Behaves like QTEST_MAIN (), but doesn't instantiate a QApplication object. Use this macro for really simple stand-alone non-GUI tests.

另请参阅 QTEST_MAIN ().

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 而不是 QApplication object. Use this macro if your test case doesn't need functionality offered by QApplication , but the event loop is still necessary.

该函数在 Qt 5.0 引入。

另请参阅 QTEST_MAIN ().

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.

QT_WIDGETS_LIB is defined, the application object will be a QApplication ,若 QT_GUI_LIB is defined, the application object will be a QGuiApplication , otherwise it will be a QCoreApplication . If qmake is used and the configuration includes QT += widgets ,那么 QT_WIDGETS_LIB will be defined automatically. Similarly, if qmake is used and the configuration includes QT += gui ,那么 QT_GUI_LIB will be defined automatically.

注意: On platforms that have keypad navigation enabled by default, this macro will forcefully disable it if QT_WIDGETS_LIB is defined. This is done to simplify the usage of key events when writing autotests. If you wish to write a test case that uses keypad navigation, you should enable it either in the initTestCase() or init() functions of your test case by calling QApplication::setNavigationMode ().

范例:

QTEST_MAIN(TestQString)
					

另请参阅 QTEST_APPLESS_MAIN (), QTEST_GUILESS_MAIN (), QTest::qExec (),和 QApplication::setNavigationMode ().

QTRY_COMPARE ( actual , expected )

Performs a comparison of the actual and expected values by invoking QTRY_COMPARE_WITH_TIMEOUT () with a timeout of five seconds.

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.0 引入。

另请参阅 QTRY_COMPARE_WITH_TIMEOUT (), QCOMPARE (), QVERIFY (), QTRY_VERIFY (),和 QEXPECT_FAIL ().

QTRY_COMPARE_WITH_TIMEOUT ( actual , expected , timeout )

The QTRY_COMPARE_WITH_TIMEOUT() macro is similar to QCOMPARE (), but performs the comparison of the actual and expected values repeatedly, until either the two values are equal or the timeout (in milliseconds) is reached. Between each comparison, events will be processed. If the timeout is reached, a failure is recorded in the test log and the test won't be executed further.

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.0 引入。

另请参阅 QTRY_COMPARE (), QCOMPARE (), QVERIFY (), QTRY_VERIFY (),和 QEXPECT_FAIL ().

QTRY_VERIFY2 ( 条件 , message )

校验 条件 通过援引 QTRY_VERIFY2_WITH_TIMEOUT () with a timeout of five seconds. If 条件 is then still false, message is output. The message is a plain C string.

范例:

QTRY_VERIFY2(list.size() > 2, QByteArray::number(list.size()).constData());
					

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.6 引入。

另请参阅 QTRY_VERIFY2_WITH_TIMEOUT (), QTRY_VERIFY2(), QVERIFY (), QCOMPARE (), QTRY_COMPARE (),和 QEXPECT_FAIL ().

QTRY_VERIFY ( 条件 )

校验 条件 通过援引 QTRY_VERIFY_WITH_TIMEOUT () with a timeout of five seconds.

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.0 引入。

另请参阅 QTRY_VERIFY_WITH_TIMEOUT (), QTRY_VERIFY2 (), QVERIFY (), QCOMPARE (), QTRY_COMPARE (),和 QEXPECT_FAIL ().

QTRY_VERIFY2_WITH_TIMEOUT ( 条件 , message , timeout )

The QTRY_VERIFY2_WITH_TIMEOUT macro is similar to QTRY_VERIFY_WITH_TIMEOUT () except that it outputs a verbose message 条件 is still false after the specified timeout (in milliseconds). The message is a plain C string.

范例:

QTRY_VERIFY2_WITH_TIMEOUT(list.size() > 2, QByteArray::number(list.size()).constData(), 10000);
					

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.6 引入。

另请参阅 QTRY_VERIFY (), QTRY_VERIFY_WITH_TIMEOUT (), QVERIFY (), QCOMPARE (), QTRY_COMPARE (),和 QEXPECT_FAIL ().

QTRY_VERIFY_WITH_TIMEOUT ( 条件 , timeout )

The QTRY_VERIFY_WITH_TIMEOUT() macro is similar to QVERIFY (), but checks the 条件 repeatedly, until either the condition becomes true or the timeout (in milliseconds) is reached. Between each evaluation, events will be processed. If the timeout is reached, a failure is recorded in the test log and the test won't be executed further.

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.0 引入。

另请参阅 QTRY_VERIFY (), QTRY_VERIFY2_WITH_TIMEOUT (), QVERIFY (), QCOMPARE (), QTRY_COMPARE (),和 QEXPECT_FAIL ().

QVERIFY2 ( 条件 , message )

The QVERIFY2() macro behaves exactly like QVERIFY (), except that it reports a message 条件 is false. The message is a plain C string.

The message can also be obtained from a function call that produces a plain C string, such as qPrintable () applied to a QString , which may be built in any of its usual ways, including applying .args() to format some data.

范例:

QVERIFY2(QFileInfo("file.txt").exists(), "file.txt does not exist.");
					

For example, if you have a file object and you are testing its open() function, you might write a test with a statement like:

bool opened = file.open(QIODevice::WriteOnly);
QVERIFY(opened);
					

If this test fails, it will give no clue as to why the file failed to open:

FAIL! : tst_QFile::open_write() 'opened' returned FALSE. ()

If there is a more informative error message you could construct from the values being tested, you can use QVERIFY2() to pass that message along with your test condition, to provide a more informative message on failure:

QVERIFY2(file.open(QIODevice::WriteOnly),
         qPrintable(QString("open %1: %2")
                    .arg(file.fileName()).arg(file.errorString())));
					

If this branch is being tested in the Qt CI system, the above detailed failure message will be inserted into the summary posted to the code-review system:

FAIL! : tst_QFile::open_write() 'opened' returned FALSE. (open /tmp/qt.a3B42Cd: No space left on device)

另请参阅 QVERIFY (), QCOMPARE (),和 QEXPECT_FAIL ().

QVERIFY ( 条件 )

The QVERIFY() macro checks whether the 条件 is true or not. If it is true, execution continues. If not, a failure is recorded in the test log and the test won't be executed further.

可以使用 QVERIFY2 () when it is practical and valuable to put additional information into the test failure report.

注意: This macro can only be used in a test function that is invoked by the test framework.

For example, the following code shows this macro being used to verify that a QSignalSpy object is valid:

QVERIFY(spy.isValid());
					

For more information about the failure, use QCOMPARE(x, y) 而不是 QVERIFY(x == y) , because it reports both the expected and actual value when the comparison fails.

另请参阅 QCOMPARE (), QTRY_VERIFY (), QSignalSpy ,和 QEXPECT_FAIL ().

QVERIFY_EXCEPTION_THROWN ( 表达式 , exceptiontype )

The QVERIFY_EXCEPTION_THROWN macro executes 表达式 and tries to catch an exception thrown from 表达式 .

There are several possible outcomes:

  • 表达式 throws an exception that is either the same as exceptiontype or derived from exceptiontype , then execution will continue.
  • 否则,若 表达式 throws no exception, or the exception thrown derives from std::exception , then a failure will be recorded in the test log and the macro returns early (from enclosing function).
  • If the thrown exception derives neither from std::exception nor from exceptiontype , a failure will be recorded in the test log, and the exception is re-thrown. This avoids problems with e.g. pthread cancellation exceptions.

注意: This macro can only be used in a test function that is invoked by the test framework.

该函数在 Qt 5.3 引入。

QWARN ( message )

追加 message 作为测试日志的警告。此宏可以在测试中的任何地方使用。

注意: 此函数是 thread-safe .