Sophie

Sophie

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

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>Qt Test Overview | 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 >Qt Test Overview</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">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#creating-a-test">Creating a Test</a></li>
<li class="level1"><a href="#building-a-test">Building a Test</a></li>
<li class="level1"><a href="#qt-test-command-line-arguments">Qt Test Command Line Arguments</a></li>
<li class="level2"><a href="#syntax">Syntax</a></li>
<li class="level2"><a href="#options">Options</a></li>
<li class="level1"><a href="#creating-a-benchmark">Creating a Benchmark</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Qt Test Overview</h1>
<span class="subtitle"></span>
<!-- $$$qtest-overview.html-description -->
<div class="descr"> <a name="details"></a>
<p>Qt Test is a framework for unit testing Qt based applications and libraries. Qt Test provides all the functionality commonly found in unit testing frameworks as well as extensions for testing graphical user interfaces.</p>
<p>Qt Test is designed to ease the writing of unit tests for Qt based applications and libraries:</p>
<div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Feature</th><th >Details</th></tr></thead>
<tr valign="top" class="odd"><td ><b>Lightweight</b></td><td >Qt Test consists of about 6000 lines of code and 60 exported symbols.</td></tr>
<tr valign="top" class="even"><td ><b>Self-contained</b></td><td >Qt Test requires only a few symbols from the Qt Core module for non-gui testing.</td></tr>
<tr valign="top" class="odd"><td ><b>Rapid testing</b></td><td >Qt Test needs no special test-runners; no special registration for tests.</td></tr>
<tr valign="top" class="even"><td ><b>Data-driven testing</b></td><td >A test can be executed multiple times with different test data.</td></tr>
<tr valign="top" class="odd"><td ><b>Basic GUI testing</b></td><td >Qt Test offers functionality for mouse and keyboard simulation.</td></tr>
<tr valign="top" class="even"><td ><b>Benchmarking</b></td><td >Qt Test supports benchmarking and provides several measurement back-ends.</td></tr>
<tr valign="top" class="odd"><td ><b>IDE friendly</b></td><td >Qt Test outputs messages that can be interpreted by Visual Studio and KDevelop.</td></tr>
<tr valign="top" class="even"><td ><b>Thread-safety</b></td><td >The error reporting is thread safe and atomic.</td></tr>
<tr valign="top" class="odd"><td ><b>Type-safety</b></td><td >Extensive use of templates prevent errors introduced by implicit type casting.</td></tr>
<tr valign="top" class="even"><td ><b>Easily extendable</b></td><td >Custom types can easily be added to the test data and test output.</td></tr>
</table></div>
<a name="creating-a-test"></a>
<h2 id="creating-a-test">Creating a Test</h2>
<p>To create a test, subclass <a href="../qtcore/qobject.html">QObject</a> and add one or more private slots to it. Each private slot is a test function in your test. <a href="qtest.html#qExec">QTest::qExec</a>() can be used to execute all test functions in the test object.</p>
<p>In addition, there are four private slots that are <i>not</i> treated as test functions. They will be executed by the testing framework and can be used to initialize and clean up either the entire test or the current test function.</p>
<ul>
<li><code>initTestCase()</code> will be called before the first test function is executed.</li>
<li><code>cleanupTestCase()</code> will be called after the last test function was executed.</li>
<li><code>init()</code> will be called before each test function is executed.</li>
<li><code>cleanup()</code> will be called after every test function.</li>
</ul>
<p>If <code>initTestCase()</code> fails, no test function will be executed. If <code>init()</code> fails, the following test function will not be executed, the test will proceed to the next test function.</p>
<p>Example:</p>
<pre class="cpp">

  <span class="keyword">class</span> MyFirstTest: <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> initTestCase()
      { <a href="../qtcore/qtglobal.html#qDebug">qDebug</a>(<span class="string">&quot;called before everything else&quot;</span>); }
      <span class="type">void</span> myFirstTest()
      { QVERIFY(<span class="number">1</span> <span class="operator">=</span><span class="operator">=</span> <span class="number">1</span>); }
      <span class="type">void</span> mySecondTest()
      { QVERIFY(<span class="number">1</span> <span class="operator">!</span><span class="operator">=</span> <span class="number">2</span>); }
      <span class="type">void</span> cleanupTestCase()
      { <a href="../qtcore/qtglobal.html#qDebug">qDebug</a>(<span class="string">&quot;called after myFirstTest and mySecondTest&quot;</span>); }
  };

</pre>
<p>For more examples, refer to the <a href="qtest-tutorial.html">Qt Test Tutorial</a>.</p>
<a name="building-a-test"></a>
<h2 id="building-a-test">Building a Test</h2>
<p>If you are using <code>qmake</code> as your build tool, just add the following to your project file:</p>
<pre class="cpp">

  QT += testlib

</pre>
<p>If you would like to run the test via <code>make check</code>, add the additional line:</p>
<pre class="cpp">

  CONFIG += testcase

</pre>
<p>See the <a href="../qmake/qmake-common-projects.html#building-a-testcase">qmake manual</a> for more information about <code>make check</code>.</p>
<p>If you are using other build tools, make sure that you add the location of the Qt Test header files to your include path (usually <code>include/QtTest</code> under your Qt installation directory). If you are using a release build of Qt, link your test to the <code>QtTest</code> library. For debug builds, use <code>QtTest_debug</code>.</p>
<p>See <a href="qttestlib-tutorial1-example.html">Writing a Unit Test</a> for a step by step explanation.</p>
<a name="qt-test-command-line-arguments"></a>
<h2 id="qt-test-command-line-arguments">Qt Test Command Line Arguments</h2>
<a name="syntax"></a>
<h3 >Syntax</h3>
<p>The syntax to execute an autotest takes the following simple form:</p>
<pre class="cpp">

  testname <span class="operator">[</span>options<span class="operator">]</span> <span class="operator">[</span>testfunctions<span class="operator">[</span>:testdata<span class="operator">]</span><span class="operator">]</span><span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>

</pre>
<p>Substitute <code>testname</code> with the name of your executable. <code>testfunctions</code> can contain names of test functions to be executed. If no <code>testfunctions</code> are passed, all tests are run. If you append the name of an entry in <code>testdata</code>, the test function will be run only with that test data.</p>
<p>For example:</p>
<pre class="cpp">

  <span class="operator">/</span>myTestDirectory$ testQString toUpper

</pre>
<p>Runs the test function called <code>toUpper</code> with all available test data.</p>
<pre class="cpp">

  <span class="operator">/</span>myTestDirectory$ testQString toUpper toInt:zero

</pre>
<p>Runs the <code>toUpper</code> test function with all available test data, and the <code>toInt</code> test function with the test data called <code>zero</code> (if the specified test data doesn't exist, the associated test will fail).</p>
<pre class="cpp">

  <span class="operator">/</span>myTestDirectory$ testMyWidget <span class="operator">-</span>vs <span class="operator">-</span>eventdelay <span class="number">500</span>

</pre>
<p>Runs the <code>testMyWidget</code> function test, outputs every signal emission and waits 500 milliseconds after each simulated mouse/keyboard event.</p>
<a name="options"></a>
<h3 >Options</h3>
<a name="logging-options"></a>
<h4 >Logging Options</h4>
<p>The following command line options determine how test results are reported:</p>
<ul>
<li><code>-o</code> <i>filename,format</i> <br />
 Writes output to the specified file, in the specified format (one of <code>txt</code>, <code>xml</code>, <code>lightxml</code> or <code>xunitxml</code>). The special filename <code>-</code> may be used to log to standard output.</li>
<li><code>-o</code> <i>filename</i> <br />
 Writes output to the specified file.</li>
<li><code>-txt</code> <br />
 Outputs results in plain text.</li>
<li><code>-xml</code> <br />
 Outputs results as an XML document.</li>
<li><code>-lightxml</code> <br />
 Outputs results as a stream of XML tags.</li>
<li><code>-xunitxml</code> <br />
 Outputs results as an Xunit XML document.</li>
<li><code>-csv</code> <br />
 Outputs results as comma-separated values (CSV). This mode is only suitable for benchmarks, since it suppresses normal pass/fail messages.</li>
<li><code>-teamcity</code> <br />
 Outputs results in TeamCity format.</li>
</ul>
<p>The first version of the <code>-o</code> option may be repeated in order to log test results in multiple formats, but no more than one instance of this option can log test results to standard output.</p>
<p>If the first version of the <code>-o</code> option is used, neither the second version of the <code>-o</code> option nor the <code>-txt</code>, <code>-xml</code>, <code>-lightxml</code>, <code>-teamcity</code> or <code>-xunitxml</code> options should be used.</p>
<p>If neither version of the <code>-o</code> option is used, test results will be logged to standard output. If no format option is used, test results will be logged in plain text.</p>
<a name="test-log-detail-options"></a>
<h4 >Test Log Detail Options</h4>
<p>The following command line options control how much detail is reported in test logs:</p>
<ul>
<li><code>-silent</code> <br />
 Silent output; only shows fatal errors, test failures and minimal status messages.</li>
<li><code>-v1</code> <br />
 Verbose output; shows when each test function is entered. (This option only affects plain text output.)</li>
<li><code>-v2</code> <br />
 Extended verbose output; shows each <a href="qtest.html#QCOMPARE">QCOMPARE</a>() and <a href="qtest.html#QVERIFY">QVERIFY</a>(). (This option affects all output formats and implies <code>-v1</code> for plain text output.)</li>
<li><code>-vs</code> <br />
 Shows all signals that get emitted and the slot invocations resulting from those signals. (This option affects all output formats.)</li>
</ul>
<a name="testing-options"></a>
<h4 >Testing Options</h4>
<p>The following command-line options influence how tests are run:</p>
<ul>
<li><code>-functions</code> <br />
 Outputs all test functions available in the test, then quits.</li>
<li><code>-datatags</code> <br />
 Outputs all data tags available in the test. A global data tag is preceded by ' __global__ '.</li>
<li><code>-eventdelay</code> <i>ms</i> <br />
 If no delay is specified for keyboard or mouse simulation (<a href="qtest.html#keyClick">QTest::keyClick</a>(), <a href="qtest.html#mouseClick">QTest::mouseClick</a>() etc.), the value from this parameter (in milliseconds) is substituted.</li>
<li><code>-keydelay</code> <i>ms</i> <br />
 Like -eventdelay, but only influences keyboard simulation and not mouse simulation.</li>
<li><code>-mousedelay</code> <i>ms</i> <br />
 Like -eventdelay, but only influences mouse simulation and not keyboard simulation.</li>
<li><code>-maxwarnings</code> <i>number</i> <br />
 Sets the maximum number of warnings to output. 0 for unlimited, defaults to 2000.</li>
<li><code>-nocrashhandler</code> <br />
 Disables the crash handler on Unix platforms. On Windows, it re-enables the Windows Error Reporting dialog, which is turned off by default. This is useful for debugging crashes.</li>
<li><code>-platform</code> <i>name</i> <br />
 This command line argument applies to all Qt applications, but might be especially useful in the context of auto-testing. By using the &quot;offscreen&quot; platform plugin (-platform offscreen) it's possible to have tests that use <a href="../qtwidgets/qwidget.html">QWidget</a> or <a href="../qtgui/qwindow.html">QWindow</a> run without showing anything on the screen. Currently the offscreen platform plugin is only fully supported on X11.</li>
</ul>
<a name="benchmarking-options"></a>
<h4 >Benchmarking Options</h4>
<p>The following command line options control benchmark testing:</p>
<ul>
<li><code>-callgrind</code> <br />
 Uses Callgrind to time benchmarks (Linux only).</li>
<li><code>-tickcounter</code> <br />
 Uses CPU tick counters to time benchmarks.</li>
<li><code>-eventcounter</code> <br />
 Counts events received during benchmarks.</li>
<li><code>-minimumvalue</code> <i>n</i> <br />
 Sets the minimum acceptable measurement value.</li>
<li><code>-minimumtotal</code> <i>n</i> <br />
 Sets the minimum acceptable total for repeated executions of a test function.</li>
<li><code>-iterations</code> <i>n</i> <br />
 Sets the number of accumulation iterations.</li>
<li><code>-median</code> <i>n</i> <br />
 Sets the number of median iterations.</li>
<li><code>-vb</code> <br />
 Outputs verbose benchmarking information.</li>
</ul>
<a name="miscellaneous-options"></a>
<h4 >Miscellaneous Options</h4>
<ul>
<li><code>-help</code> <br />
 Outputs the possible command line arguments and gives some useful help.</li>
</ul>
<a name="creating-a-benchmark"></a>
<h2 id="creating-a-benchmark">Creating a Benchmark</h2>
<p>To create a benchmark, follow the instructions for creating a test and then add a QBENCHMARK macro to the test function that you want to benchmark.</p>
<pre class="cpp">

  <span class="keyword">class</span> MyFirstBenchmark: <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> myFirstBenchmark()
      {
          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> string1;
          <span class="type"><a href="../qtcore/qstring.html">QString</a></span> string2;
          QBENCHMARK {
              string1<span class="operator">.</span>localeAwareCompare(string2);
          }
      }
  };

</pre>
<p>The code inside the QBENCHMARK macro will be measured, and possibly also repeated several times in order to get an accurate measurement. This depends on the selected measurement back-end. Several back-ends are available. They can be selected on the command line:</p>
<a name="testlib-benchmarking-measurement"></a><div class="table"><table class="generic">
 <thead><tr class="qt-style"><th >Name</th><th >Command-line Argument</th><th >Availability</th></tr></thead>
<tr valign="top" class="odd"><td >Walltime</td><td >(default)</td><td >All platforms</td></tr>
<tr valign="top" class="even"><td >CPU tick counter</td><td >-tickcounter</td><td >Windows, macOS, Linux, many UNIX-like systems.</td></tr>
<tr valign="top" class="odd"><td >Event Counter</td><td >-eventcounter</td><td >All platforms</td></tr>
<tr valign="top" class="even"><td >Valgrind Callgrind</td><td >-callgrind</td><td >Linux (if installed)</td></tr>
<tr valign="top" class="odd"><td >Linux Perf</td><td >-perf</td><td >Linux</td></tr>
</table></div>
<p>In short, walltime is always available but requires many repetitions to get a useful result. Tick counters are usually available and can provide results with fewer repetitions, but can be susceptible to CPU frequency scaling issues. Valgrind provides exact results, but does not take I/O waits into account, and is only available on a limited number of platforms. Event counting is available on all platforms and it provides the number of events that were received by the event loop before they are sent to their corresponding targets (this might include non-Qt events).</p>
<p>The Linux Performance Monitoring solution is available only on Linux and provides many different counters, which can be selected by passing an additional option <code>-perfcounter countername</code>, such as <code>-perfcounter cache-misses</code>, <code>-perfcounter branch-misses</code>, or <code>-perfcounter l1d-load-misses</code>. The default counter is <code>cpu-cycles</code>. The full list of counters can be obtained by running any benchmark executable with the option <code>-perfcounterlist</code>.</p>
<ul>
<li><b>Notes</b>:<ul>
<li>Using the performance counter may require enabling access to non-privileged applications.</li>
<li>Devices that do not support high-resolution timers default to one-millisecond granularity.</li>
</ul>
</li>
</ul>
<p>See <a href="qttestlib-tutorial5-example.html">Writing a Benchmark</a> in the Qt Test Tutorial for more benchmarking examples.</p>
</div>
<!-- @@@qtest-overview.html -->
        </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>