Qt 4.8
|
#include <QtTest/qtest_global.h>
#include <QtCore/qnamespace.h>
#include <QtCore/qmetatype.h>
Go to the source code of this file.
Namespaces | |
QTest | |
The QTest namespace contains all the functions and declarations that are related to the QTestLib tool. | |
Macros | |
#define | DEPENDS_ON(funcName) |
#define | QCOMPARE(actual, expected) |
The QCOMPARE macro compares an actual value to an expected value using the equals operator. More... | |
#define | QEXPECT_FAIL(dataIndex, comment, mode) |
The QEXPECT_FAIL() macro marks the next QCOMPARE() or QVERIFY() as an expected failure. More... | |
#define | QFAIL(message) |
This macro can be used to force a test failure. More... | |
#define | QFETCH(type, name) type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >())) |
The fetch macro creates a local variable named name with the type type on the stack. More... | |
#define | QFETCH_GLOBAL(type, name) type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >())) |
#define | QSKIP(statement, mode) |
The QSKIP() macro stops execution of the test without adding a failure to the test log. More... | |
#define | QTEST(actual, testElement) |
QTEST() is a convenience macro for QCOMPARE() that compares the value actual with the element testElement from the test's data. More... | |
#define | QTEST_COMPARE_DECL(KLASS) template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &); |
#define | QVERIFY(statement) |
The QVERIFY() macro checks whether the condition is true or not. More... | |
#define | QVERIFY2(statement, description) |
The QVERIFY2() macro behaves exactly like QVERIFY(), except that it outputs a verbose message when condition is false. More... | |
#define | QWARN(msg) QTest::qWarn(msg) |
Appends message as a warning to the test log. More... | |
Functions | |
template<typename T > | |
void | QTest::addColumn (const char *name, T *=0) |
Adds a column with type T to the current test data. More... | |
Q_TESTLIB_EXPORT void | QTest::addColumnInternal (int id, const char *name) |
Q_TESTLIB_EXPORT Qt::Key | QTest::asciiToKey (char ascii) |
Convert an ascii char key value to a Qt Key value. More... | |
Q_TESTLIB_EXPORT bool | QTest::compare_helper (bool success, const char *msg, const char *file, int line) |
Q_TESTLIB_EXPORT bool | QTest::compare_helper (bool success, const char *msg, char *val1, char *val2, const char *expected, const char *actual, const char *file, int line) |
bool | QTest::compare_ptr_helper (const void *t1, const void *t2, const char *actual, const char *expected, const char *file, int line) |
Q_TESTLIB_EXPORT bool | QTest::compare_string_helper (const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) |
Q_TESTLIB_EXPORT const char * | QTest::currentAppName () |
Returns the name of the binary that is currently executed. More... | |
Q_TESTLIB_EXPORT const char * | QTest::currentDataTag () |
Returns the name of the current test data. More... | |
Q_TESTLIB_EXPORT bool | QTest::currentTestFailed () |
Returns true if the current test function failed, otherwise false. More... | |
Q_TESTLIB_EXPORT const char * | QTest::currentTestFunction () |
Returns the name of the test function that is currently executed. More... | |
Q_TESTLIB_EXPORT void | QTest::ignoreMessage (QtMsgType type, const char *message) |
Ignores messages created by qDebug() or qWarning(). More... | |
Q_TESTLIB_EXPORT char | QTest::keyToAscii (Qt::Key key) |
Convert a Qt Key to an ascii char value. More... | |
Q_TESTLIB_EXPORT QTestData & | QTest::newRow (const char *dataTag) |
Appends a new row to the current test data. More... | |
template<typename T > | |
bool | QTest::qCompare (T const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line) |
template<typename T1 , typename T2 > | |
bool | QTest::qCompare (T1 const &, T2 const &, const char *, const char *, const char *, int) |
template<typename T > | |
bool | QTest::qCompare (const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line) |
template<typename T > | |
bool | QTest::qCompare (T *t1, T *t2, const char *actual, const char *expected, const char *file, int line) |
template<typename T1 , typename T2 > | |
bool | QTest::qCompare (const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line) |
template<typename T1 , typename T2 > | |
bool | QTest::qCompare (T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
bool | QTest::qCompare (char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
bool | QTest::qCompare (const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
bool | QTest::qCompare (bool const &t1, int const &t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
bool | QTest::qCompare< char > (const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
bool | QTest::qCompare< char > (char *t1, char *t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
Q_TESTLIB_EXPORT bool | QTest::qCompare< double > (double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line) |
template<> | |
Q_TESTLIB_EXPORT bool | QTest::qCompare< float > (float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line) |
Q_TESTLIB_EXPORT void * | QTest::qData (const char *tagName, int typeId) |
Q_TESTLIB_EXPORT void * | QTest::qElementData (const char *elementName, int metaTypeId) |
Q_TESTLIB_EXPORT int | QTest::qExec (QObject *testObject, int argc=0, char **argv=0) |
Executes tests declared in testObject. More... | |
Q_TESTLIB_EXPORT int | QTest::qExec (QObject *testObject, const QStringList &arguments) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. More... | |
Q_TESTLIB_EXPORT bool | QTest::qExpectFail (const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line) |
Q_TESTLIB_EXPORT void | QTest::qFail (const char *statementStr, const char *file, int line) |
Q_TESTLIB_EXPORT void * | QTest::qGlobalData (const char *tagName, int typeId) |
Q_TESTLIB_EXPORT void | QTest::qSkip (const char *message, SkipMode mode, const char *file, int line) |
Q_TESTLIB_EXPORT void | QTest::qSleep (int ms) |
Sleeps for ms milliseconds, blocking execution of the test. More... | |
template<class T > | |
bool | QTest::qTest (const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line) |
Q_TESTLIB_EXPORT bool | QTest::qVerify (bool statement, const char *statementStr, const char *description, const char *file, int line) |
Q_TESTLIB_EXPORT void | QTest::qWarn (const char *message) |
Q_TESTLIB_EXPORT QObject * | QTest::testObject () |
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. More... | |
template<typename T > | |
char * | QTest::toString (const T &) |
Returns a textual representation of value. More... | |
Q_TESTLIB_EXPORT char * | QTest::toString (const char *) |
Q_TESTLIB_EXPORT char * | QTest::toString (const void *) |
#define DEPENDS_ON | ( | funcName | ) |
Definition at line 103 of file qtestcase.h.
#define QCOMPARE | ( | actual, | |
expected | |||
) |
The QCOMPARE macro compares an actual value to an expected value using the equals operator.
If actual and expected are identical, execution continues. If not, a failure is recorded in the test log and the test won't be executed further.
In the case of comparing floats and doubles, qFuzzyCompare() is used for comparing. This means that comparing to 0 will likely fail. One solution to this is to compare to 1, and add 1 to the produced output.
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 is very strict on the data types. Both actual and expected have to be of the same type, otherwise the test won't compile. This prohibits unspecified behavior from being introduced; that is behavior that usually occurs when the compiler implicitly casts the argument.
If you use QCOMPARE() to compare two QStringList objects, it will start comparing the objects from the end of the lists.
For your own classes, you can use QTest::toString() to format values for outputting into the test log.
Example:
Definition at line 79 of file qtestcase.h.
#define QEXPECT_FAIL | ( | dataIndex, | |
comment, | |||
mode | |||
) |
The QEXPECT_FAIL() macro marks the next QCOMPARE() or QVERIFY() as an expected failure.
Instead of adding a failure to the test log, an expected failure will be reported.
If a QVERIFY() or QCOMPARE() is marked as an expected failure, but passes instead, an unexpected pass (XPASS) is written to the test log.
The parameter 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 is a QTest::TestFailMode and sets whether the test should continue to execute or not.
Note: This macro can only be used in a test function that is invoked by the test framework.
Example 1:
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:
The above testfunction will not continue executing for the test data entry data27
.
Definition at line 91 of file qtestcase.h.
#define 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.
Note: This macro can only be used in a test function that is invoked by the test framework.
Example:
Definition at line 62 of file qtestcase.h.
#define QFETCH | ( | type, | |
name | |||
) | type name = *static_cast<type *>(QTest::qData(#name, ::qMetaTypeId<type >())) |
The fetch macro creates a local variable named name with the type type on the stack.
name has to match the element name from the test's data. If no such element exists, the test will assert.
Assuming a test has the following data:
The test data has two elements, a QString called aString
and an integer called expected
. To fetch these values in the actual test:
aString
and expected
are variables on the stack that are initialized with the current test data.
Note: 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.
Definition at line 97 of file qtestcase.h.
#define QFETCH_GLOBAL | ( | type, | |
name | |||
) | type name = *static_cast<type *>(QTest::qGlobalData(#name, ::qMetaTypeId<type >())) |
Definition at line 100 of file qtestcase.h.
#define QSKIP | ( | statement, | |
mode | |||
) |
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. The text description is appended to the test log and should contain an explanation why the test couldn't be executed. mode is a QTest::SkipMode and describes whether to proceed with the rest of the test data or not.
Note: This macro can only be used in a test function that is invoked by the test framework.
Example:
Definition at line 85 of file qtestcase.h.
#define 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:
you can write:
Definition at line 105 of file qtestcase.h.
#define QTEST_COMPARE_DECL | ( | KLASS | ) | template<> Q_TESTLIB_EXPORT char *toString<KLASS >(const KLASS &); |
Definition at line 117 of file qtestcase.h.
Referenced by QTest::compare_ptr_helper().
#define QVERIFY | ( | statement | ) |
The QVERIFY() macro checks whether the condition 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.
Note: This macro can only be used in a test function that is invoked by the test framework.
Example:
Definition at line 56 of file qtestcase.h.
#define QVERIFY2 | ( | statement, | |
description | |||
) |
The QVERIFY2() macro behaves exactly like QVERIFY(), except that it outputs a verbose message when condition is false.
The message is a plain C string.
Example:
Definition at line 68 of file qtestcase.h.
#define QWARN | ( | msg | ) | QTest::qWarn(msg) |
Appends message as a warning to the test log.
This macro can be used anywhere in your tests.
Definition at line 111 of file qtestcase.h.