Qtest Private Slots
How to write a unit test.
In this first chapter we will see how to write a simple unit test for a class, and how to execute it.
In addition, the private slots initTestCase, cleanupTestCase, init and cleanup are executed if they exist. See Creating a Test for more details. Optionally, the command line arguments argc and argv can be provided. For a list of recognized arguments, read Qt Test Command Line Arguments. I have found more convenient way to do this. First, all private methods should be private slots. Then you create an instance of the class: Foo a; Then we can use QMetaObject::invokeMethod to call any slot that method has (public or private). So if we want to call method Test, we can do it like this.
Writing a Test
Let's assume you want to test the behavior of our QString class. First, you need a class that contains your test functions. This class has to inherit from QObject:
Note: You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it.
Then you need to implement the test function itself. The implementation could look like this:
The QVERIFY() macro evaluates the expression passed as its argument. If the expression evaluates to true, the execution of the test function continues. Otherwise, a message describing the failure is appended to the test log, and the test function stops executing.
But if you want a more verbose output to the test log, you should use the QCOMPARE() macro instead:
If the strings are not equal, the contents of both strings are appended to the test log, making it immediately visible why the comparison failed.
Finally, to make our test case a stand-alone executable, the following two lines are needed:
The QTEST_MAIN() macro expands to a simple main()
method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a .cpp
file, we also need to include the generated moc file to make Qt's introspection work.
Executing a Test
Now that we finished writing our test, we want to execute it. Assuming that our test was saved as testqstring.cpp
in an empty directory, we build the test using qmake to create a project and generate a makefile.
Note: If you're using windows, replace make
with nmake
or whatever build tool you use.
Running the resulting executable should give you the following output:
Congratulations! You just wrote and executed your first unit test using the Qt Test framework.
Qtest Private Slots Online
© 2020 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.
testname [options] [testfunctions [: testdata] ] ...Qtest Private Slots Casino
[options] Will refer to later.[testfunctions [: testdata] ]
Specify if you want to run some test methods. Format below.
Qtest Private Slots Free
Method name: The name ※ If you do not enter the test pattern test pattern names are all variations of the test method specified.
Qtest Private Slots Games
- -help
outputs the possible command line arguments and give some useful help. - -functions
outputs all test functions available in the test. - -o filename
write output to the specified file, rather than to standard output - -silent
silent output, only shows warnings, failures and minimal status messages - -v1
verbose output; outputs information on entering and exiting test functions. - -v2
extended verbose output; also outputs each QCOMPARE () and QVERIFY () - -vs
outputs every signal that gets emitted - -xml
outputs XML formatted results instead of plain text - -lightxml
outputs results as a stream of XML tags - -eventdelay ms
if no delay is specified for keyboard or mouse simulation (QTest:: keyClick (), QTest:: mouseClick () etc.), the value from this parameter (in milliseconds) is substituted. - -keydelay ms
like-eventdelay, but only influences keyboard simulation and not mouse simulation. - -mousedelay ms
like-eventdelay, but only influences mouse simulation and not keyboard simulation. - -keyevent-verbose
output more verbose output for keyboard simulation - -maxwarnings numberBR sets the maximum number of warnings to output. 0 for unlimited, defaults to 2000.