<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- qtestlib.qdoc --> <head> <title>Qt 4.6: Chapter 1: Writing a Unit Test</title> <link rel="contents" href="qtestlib-tutorial.html" /> <link rel="next" href="qtestlib-tutorial2.html" /> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td> <td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><p> [<a href="qtestlib-tutorial.html">Contents</a>] [Next: <a href="qtestlib-tutorial2.html">Chapter 2</a>] </p> <h1 class="title">Chapter 1: Writing a Unit Test<br /><span class="subtitle"></span> </h1> <p>Files:</p> <ul> <li><a href="qtestlib-tutorial1-testqstring-cpp.html">qtestlib/tutorial1/testqstring.cpp</a></li> <li><a href="qtestlib-tutorial1-tutorial1-pro.html">qtestlib/tutorial1/tutorial1.pro</a></li> </ul> <p>In this first chapter we will see how to write a simple unit test for a class, and how to execute it.</p> <a name="writing-a-test"></a> <h2>Writing a Test</h2> <p>Let's assume you want to test the behavior of our <a href="qstring.html">QString</a> class. First, you need a class that contains your test functions. This class has to inherit from <a href="qobject.html">QObject</a>:</p> <pre> #include <QtTest/QtTest> class TestQString: public QObject { Q_OBJECT private slots: void toUpper(); };</pre> <p>Note that you need to include the <a href="qtest.html">QTest</a> header, and that the test functions have to be declared as private slots so the test framework finds and executes it.</p> <p>Then you need to implement the test function itself. The implementation could look like this:</p> <pre> void TestQString::toUpper() { QString str = "Hello"; QVERIFY(str.toUpper() == "HELLO"); }</pre> <p>The <a href="qtest.html#QVERIFY">QVERIFY</a>() 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.</p> <p>But if you want a more verbose output to the test log, you should use the <a href="qtest.html#QCOMPARE">QCOMPARE</a>() macro instead:</p> <pre> void TestQString::toUpper() { QString str = "Hello"; QCOMPARE(str.toUpper(), QString("HELLO")); }</pre> <p>If the strings are not equal, the contents of both strings is appended to the test log, making it immediately visible why the comparison failed.</p> <p>Finally, to make our test case a stand-alone executable, the following two lines are needed:</p> <pre> QTEST_MAIN(TestQString) #include "testqstring.moc"</pre> <p>The <a href="qtest.html#QTEST_MAIN">QTEST_MAIN</a>() macro expands to a simple <tt>main()</tt> method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a <tt>.cpp</tt> file, we also need to include the generated moc file to make Qt's introspection work.</p> <a name="executing-a-test"></a> <h2>Executing a Test</h2> <p>Now that we finished writing our test, we want to execute it. Assuming that our test was saved as <tt>testqstring.cpp</tt> in an empty directory: we build the test using qmake to create a project and generate a makefile.</p> <pre> /myTestDirectory$ qmake -project "CONFIG += qtestlib" /myTestDirectory$ qmake /myTestDirectory$ make</pre> <p><b>Note:</b>If you're using windows, replace <tt>make</tt> with <tt>nmake</tt> or whatever build tool you use.</p> <p>Running the resulting executable should give you the following output:</p> <pre> ********* Start testing of TestQString ********* Config: Using QTest library 4.6.3, Qt 4.6.3 PASS : TestQString::initTestCase() PASS : TestQString::toUpper() PASS : TestQString::cleanupTestCase() Totals: 3 passed, 0 failed, 0 skipped ********* Finished testing of TestQString *********</pre> <p>Congratulations! You just wrote and executed your first unit test using the <a href="qtestlib-manual.html#qtestlib">QTestLib</a> framework.</p> <p> [<a href="qtestlib-tutorial.html">Contents</a>] [Next: <a href="qtestlib-tutorial2.html">Chapter 2</a>] </p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="40%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> <td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="40%" align="right"><div align="right">Qt 4.6.3</div></td> </tr></table></div></address></body> </html>