In this case, we are using QCOMPARE, which checks that the conditions are equal. Similarly, lines 19 to 27 provide another test, which checks a setter, and a couple of accessor routines. QVERIFY is similar to ASSERT in other test suites. So if date.isValid() returns true, then the test will pass, otherwise the test will fail. Note the use of the QVERIFY macro, which checks that the condition is true. Lines 12 to 17 provide our first test, which checks that a date is valid. Note that testDate inherits from QObject and has the Q_OBJECT macro - QtTestLib requires specific Qt functionality that is present in QObject. Lines 4 to 10 give us the test class, testData. The second line imports the headers for the QDate class. Stepping through the code, the first line imports the header files for the QtTest namespace. Save as autotests/tutorial1.cpp in your project's autotests directory following the example of Okular #include #include class testDate : public QObject QTEST_MAIN ( testDate ) #include "tutorial1.moc" In a normal unittest, you would more likely be testing code that you've written yourself. To avoid too much detail on how the date class works, we'll just use the QDate class that comes with Qt. In this tutorial, we will build a simple test for a class that represents a date, using QtTestLib as the test framework. Tutorial 1: A simple test of a date class Instead, each testcase is an executable which is simply run. Unlike CppUnit or JUnit, there is no Runner type class. This allows you to test GUI widgets, but is not generally suitable for testing full applications.Įach testcase is a standalone test application. In addition to normal unit testing capabilities, QtTestLib also offers basic GUI testing, based on sending QEvents. It is provided as part of the tools included in Qt. It is written in C++, and is cross-platform. QtTestlib is a lightweight testing library developed by the Qt Project and released under the LGPL (a commercial version is also available, for those who need alternative licensing). In addition, the need to make the code testable is a very useful driver to ensure that classes do not suffer from close coupling.Īnyway, enough of the conceptual stuff - lets talk about a specific tool that can reduce some of the effort and let us get on with the job. Writing a unit test requires a slightly different approach to coding up a class, and thinking about what inputs need to be tested can help to identify logic flaws in the code (even before the tests get run). While it is true that unit tests do help to ensure correct functionality and behaviour, they also assist with other aspects of code quality. Note that it is sometimes tempting to treat the unit test as a pure verification tool. To make that work a little easier, we can use test frameworks. If that code was cleaned up a little, and built into the development system, then it could be used over and over to check that the implementation is still OK. they run, using the compiled code) rather than static analysis tests (which operate on the source or some intermediate representation).Įven if they don't call them "unit tests", most programmers have written some "throwaway" code that they use to check an implementation. Note that unit tests are dynamic tests (i.e. Even a single test is a useful step to improving code quality. It is not essential that all of the code is covered by unit tests (although that is obviously very desirable!). Unit tests are a key part of Test Driven Development, however they are useful for all software development processes. In Qt code unit tests are almost always used to test a single C++ class (although testing a macro or C function is also possible). It provides an introduction to the ideas behind unit testing, tutorial material on the QtTestLib framework, and suggestions for getting the most value for your effort.Ī unit test is a test that checks the functionality, behaviour and correctness of a single software component. It uses the QtTestLib framework provided starting with Qt 4.1. This article provides guidance on writing unittests for software based on QtĪnd KDE frameworks. 13.3 GUI application testing - Squish and KDExecutor.8 Tutorial 5: Testing Qt slots and signals.7.3 Checking debug messages and warnings.7 Tutorial 4 - Testing for failure and avoiding tests. 6 Tutorial 3: Testing Graphical User Interfaces.5.2 Running selected tests with selected data.5 Tutorial 2: Data driven testing of a date class.4 Tutorial 1: A simple test of a date class.
0 Comments
Leave a Reply. |