📄 qtestcase.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the tools applications of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "QtTest/qtestcase.h"#include "QtTest/qtestassert.h"#include <QtCore/qobject.h>#include <QtCore/qmetaobject.h>#include "QtTest/private/qtestlog_p.h"#include "QtTest/private/qtesttable_p.h"#include "QtTest/qtestdata.h"#include "QtTest/private/qtestresult_p.h"#include "QtTest/private/qsignaldumper_p.h"#include <stdarg.h>#include <stdio.h>#include <stdlib.h>#ifdef Q_OS_WIN#include <windows.h> // for Sleep#endif#ifdef Q_OS_UNIX#include <time.h>#endif/*! \namespace QTest \inmodule QtTest \brief The QTest namespace contains all the functions and declarations that are related to the QTestLib tool. Please refer to the \l{QTestLib Manual} documentation for information on how to write unit tests.*//*! \macro QVERIFY(condition) \relates QTest The QVERIFY() macro checks whether the \a 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. \bold {Note:} This macro can only be used in a test function that is invoked by the test framework. Example: \code QVERIFY(1 + 1 == 2); \endcode \sa QCOMPARE()*//*! \macro QCOMPARE(actual, expected) \relates QTest The QCOMPARE macro compares an \a actual value to an \a expected value using the equals operator. If \a actual and \a expected are identical, execution continues. If not, a failure is recorded in the test log and the test won't be executed further. 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. \bold {Note:} QCOMPARE is very strict on the data types. Both \a actual and \a 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 implicitely casts the argument. Note that, for your own classes, you can use \l QTest::toString() to format values for outputting into the test log. \bold {Note:} This macro can only be used in a test function that is invoked by the test framework. Example: \code QCOMPARE(QString("hello").toUpper(), QString("HELLO")); \endcode \sa QVERIFY(), QTest::toString()*//*! \macro QFETCH(type, name) \relates QTest The fetch macro creates a local variable named \a name with the type \a type on the stack. \a 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: \code 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; } \endcode The test data has two elements, a QString called \c aString and an integer called \c expected. To fetch these values in the actual test: \code void TestQString::toInt() { QFETCH(QString, aString); QFETCH(int, expected); QCOMPARE(aString.toInt(), expected); } \endcode \c aString and \c expected are variables on the stack that are initialized with the current test data. \bold {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.*//*! \macro QWARN(message) \relates QTest \threadsafe Appends \a message as a warning to the test log. This macro can be used anywhere in your tests.*//*! \macro QFAIL(message) \relates QTest This macro can be used to force a test failure. The test stops executing and the failure \a message is appended to the test log. \bold {Note:} This macro can only be used in a test function that is invoked by the test framework. Example: \code if (sizeof(int) != 4) QFAIL("This test has not been ported to this platform yet."); \endcode*//*! \macro QTEST(actual, testElement) \relates QTest QTEST() is a convenience macro for \l QCOMPARE() that compares the value \a actual with the element \a testElement from the test's data. If there is no such element, the test asserts. Apart from that, QTEST() behaves exactly as \l QCOMPARE(). Instead of writing: \code QFETCH(QString, myString); QCOMPARE(QString("hello").toUpper(), myString); \endcode you can write: \code QTEST(QString("hello").toUpper(), "myString"); \endcode \sa QCOMPARE()*//*! \macro QSKIP(description, mode) \relates QTest 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 \a description is appended to the test log and should contain an explanation why the test couldn't be executed. \a mode is a QTest::SkipMode and describes whether to proceed with the rest of the test data or not. \bold {Note:} This macro can only be used in a test function that is invoked by the test framework. Example: \code if (!QSqlDatabase::drivers().contains("SQLITE")) QSKIP("This test requires the SQLITE database driver", SkipAll); \endcode \sa QTest::SkipMode*//*! \macro QEXPECT_FAIL(dataIndex, comment, mode) \relates QTest The QEXPECT_FAIL() macro marks the next \l QCOMPARE() or \l QVERIFY() as an expected failure. Instead of adding a failure to the test log, an expected failure will be reported. If a \l QVERIFY() or \l QCOMPARE() is marked as an expected failure, but passes instead, an unexpected pass (XPASS) is written to the test log. The parameter \a dataIndex describes for which entry in the test data the failure is expected. Pass an empty string (\c{""}) if the failure is expected for all entries or if no test data exists. \a comment will be appended to the test log for the expected failure. \a mode is a \l QTest::TestFailMode and sets whether the test should continue to execute or not. \bold {Note:} This macro can only be used in a test function that is invoked by the test framework. Example 1: \code QEXPECT_FAIL("", "Will fix in the next release", Continue); QCOMPARE(i, 42); QCOMPARE(j, 43); \endcode In the example above, an expected fail will be written into the test output if the variable \c i is not 42. If the variable \c 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: \code QEXPECT_FAIL("data27", "Oh my, this is soooo broken", Abort); QCOMPARE(i, 42); \endcode The above testfunction will not continue executing for the test data entry \c{data27}. \sa QTest::TestFailMode, QVERIFY(), QCOMPARE()*//*! \macro QTEST_MAIN(TestClass) \relates QTest Implements a main() function that instanciates a QApplication object and the \a TestClass, and executes all tests in the order they were defined. Use this macro to build stand-alone executables. Example: \code class TestQString: public QObject { ... }; QTEST_MAIN(TestQString) \endcode \sa QTEST_APPLESS_MAIN(), QTest::qExec()*//*! \macro QTEST_APPLESS_MAIN(TestClass) \relates QTest Implements a main() function that executes all tests in \a TestClass. Behaves like \l QTEST_MAIN(), but doesn't instanciate a QApplication object. Use this macro for really simple stand-alone non-GUI tests. \sa QTEST_MAIN()*//*! \macro QTEST_NOOP_MAIN() \relates QTest Implements a main() function with a test class that does absolutely nothing. Use this macro to create a test that produces valid test output but just doesn't execute any test, for example in conditional compilations: \code #ifdef Q_WS_X11 QTEST_MAIN(MyX11Test) #else // do nothing on non-X11 platforms QTEST_NOOP_MAIN #endif \endcode \sa QTEST_MAIN()*//*! \enum QTest::SkipMode This enum describes the modes for skipping tests during execution of the test data. \value SkipSingle Skips the current entry in the test table; continues execution of all the other entries in the table. \value SkipAll Skips all the entries in the test table; the test won't be executed further. \sa QSKIP()*//*! \enum QTest::TestFailMode This enum describes the modes for handling an expected failure of the \l QVERIFY() or \l QCOMPARE() macros. \value Abort 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. \value Continue Continues execution of the test after the expected failure. \sa QEXPECT_FAIL()*//*! \enum QTest::KeyAction This enum describes possible actions for key handling. \value Press The key is pressed. \value Release The key is released. \value Click The key is clicked (pressed and released).*//*! \enum QTest::MouseAction This enum describes possible actions for mouse handling. \value MousePress A mouse button is pressed. \value MouseRelease A mouse button is released. \value MouseClick A mouse button is clicked (pressed and released). \value MouseDClick A mouse button is double clicked (pressed and released twice). \value MouseMove The mouse pointer has moved.*//*! \fn void QTest::keyClick(QWidget *widget, char key, Qt::KeyboardModifiers modifier = Qt::NoModifier, int delay=-1) \overload Simulates clicking of \a key with an optional \a modifier on a \a widget. If \a delay is larger than 0, the test will wait for \a delay milliseconds. Example: \code QTest::keyClick(myWidget, 'a'); \endcode The example above simulates clicking \c a on \c myWidget without
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -