Sophie

Sophie

distrib > Mageia > 6 > armv5tl > media > core-updates > by-pkgid > 768f7d9f703884aa2562bf0a651086df > files > 2394

qtbase5-doc-5.9.4-1.1.mga6.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qttestlib-manual.qdoc -->
  <title>Chapter 1: Writing a Unit Test | Qt Test 5.9</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.9</td><td ><a href="qttest-index.html">Qt Test</a></td><td >Chapter 1: Writing a Unit Test</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
  <link rel="next" href="qttestlib-tutorial2-example.html" />
<p class="naviNextPrevious headerNavi">
<a class="nextPage" href="qttestlib-tutorial2-example.html">Chapter 2</a>
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#writing-a-test">Writing a Test</a></li>
<li class="level1"><a href="#executing-a-test">Executing a Test</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Chapter 1: Writing a Unit Test</h1>
<span class="subtitle"></span>
<!-- $$$tutorial1-description -->
<div class="descr"> <a name="details"></a>
<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 id="writing-a-test">Writing a Test</h2>
<p>Let's assume you want to test the behavior of our <a href="../qtcore/qstring.html">QString</a> class. First, you need a class that contains your test functions. This class has to inherit from <a href="../qtcore/qobject.html">QObject</a>:</p>
<pre class="cpp">

  <span class="preprocessor">#include &lt;QtTest/QtTest&gt;</span>

  <span class="keyword">class</span> TestQString: <span class="keyword">public</span> <span class="type"><a href="../qtcore/qobject.html">QObject</a></span>
  {
      Q_OBJECT
  <span class="keyword">private</span> <span class="keyword">slots</span>:
      <span class="type">void</span> toUpper();
  };

</pre>
<p><b>Note: </b>You need to include the <a href="qtest.html">QTest</a> header and declare the test functions 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 class="cpp">

  <span class="type">void</span> TestQString<span class="operator">::</span>toUpper()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> str <span class="operator">=</span> <span class="string">&quot;Hello&quot;</span>;
      QVERIFY(str<span class="operator">.</span>toUpper() <span class="operator">=</span><span class="operator">=</span> <span class="string">&quot;HELLO&quot;</span>);
  }

</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 class="cpp">

  <span class="type">void</span> TestQString<span class="operator">::</span>toUpper()
  {
      <span class="type"><a href="../qtcore/qstring.html">QString</a></span> str <span class="operator">=</span> <span class="string">&quot;Hello&quot;</span>;
      QCOMPARE(str<span class="operator">.</span>toUpper()<span class="operator">,</span> <span class="type"><a href="../qtcore/qstring.html">QString</a></span>(<span class="string">&quot;HELLO&quot;</span>));
  }

</pre>
<p>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.</p>
<p>Finally, to make our test case a stand-alone executable, the following two lines are needed:</p>
<pre class="cpp">

  QTEST_MAIN(TestQString)
  <span class="preprocessor">#include &quot;testqstring.moc&quot;</span>

</pre>
<p>The <a href="qtest.html#QTEST_MAIN">QTEST_MAIN</a>() macro expands to a simple <code>main()</code> method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a <code>.cpp</code> file, we also need to include the generated moc file to make Qt's introspection work.</p>
<a name="executing-a-test"></a>
<h2 id="executing-a-test">Executing a Test</h2>
<p>Now that we finished writing our test, we want to execute it. Assuming that our test was saved as <code>testqstring.cpp</code> in an empty directory, we build the test using qmake to create a project and generate a makefile.</p>
<pre class="cpp">

  <span class="operator">/</span>myTestDirectory$ qmake <span class="operator">-</span>project <span class="string">&quot;QT += testlib&quot;</span>
  <span class="operator">/</span>myTestDirectory$ qmake
  <span class="operator">/</span>myTestDirectory$ make

</pre>
<p><b>Note: </b>If you're using windows, replace <code>make</code> with <code>nmake</code> or whatever build tool you use.</p><p>Running the resulting executable should give you the following output:</p>
<pre class="cpp">

  <span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span> Start testing of TestQString <span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span>
  Config: Using <span class="type"><a href="qttest-module.html">QtTest</a></span> library <span class="operator">%</span>VERSION<span class="operator">%</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qt.html">Qt</a></span> <span class="operator">%</span>VERSION<span class="operator">%</span>
  PASS   : TestQString<span class="operator">::</span>initTestCase()
  PASS   : TestQString<span class="operator">::</span>toUpper()
  PASS   : TestQString<span class="operator">::</span>cleanupTestCase()
  Totals: <span class="number">3</span> passed<span class="operator">,</span> <span class="number">0</span> failed<span class="operator">,</span> <span class="number">0</span> skipped
  <span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span> Finished testing of TestQString <span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span><span class="operator">*</span>

</pre>
<p>Congratulations! You just wrote and executed your first unit test using the Qt Test framework.</p>
<p>Files:</p>
<ul>
<li><a href="qttestlib-tutorial1-testqstring-cpp.html">tutorial1/testqstring.cpp</a></li>
<li><a href="qttestlib-tutorial1-tutorial1-pro.html">tutorial1/tutorial1.pro</a></li>
</ul>
</div>
<!-- @@@tutorial1 -->
<p class="naviNextPrevious footerNavi">
<a class="nextPage" href="qttestlib-tutorial2-example.html">Chapter 2</a>
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br>    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. </p>
</div>
</body>
</html>