Viadeo Twitter Google Bookmarks ! Facebook Digg del.icio.us MySpace Yahoo MyWeb Blinklist Netvouz Reddit Simpy StumbleUpon Bookmarks Windows Live Favorites 
Logo Documentation Qt ·  Page d'accueil  ·  Toutes les classes  ·  Classes principales  ·  Annotées  ·  Classes groupées  ·  Modules  ·  Fonctions  · 

QTestLib Manual

The QTestLib framework, provided by Nokia, is a tool for unit testing Qt based applications and libraries. QTestLib provides all the functionality commonly found in unit testing frameworks as well as extensions for testing graphical user interfaces.

QTestLib Features

QTestLib is designed to ease the writing of unit tests for Qt based applications and libraries:

FeatureDetails
LightweightQTestLib consists of about 6000 lines of code and 60 exported symbols.
Self-containedQTestLib requires only a few symbols from the Qt Core library for non-gui testing.
Rapid testingQTestLib needs no special test-runners; no special registration for tests.
Data-driven testingA test can be executed multiple times with different test data.
Basic GUI testingQTestLib offers functionality for mouse and keyboard simulation.
BenchmarkingQTestLib supports benchmarking and provides several measurement back-ends.
IDE friendlyQTestLib outputs messages that can be interpreted by Visual Studio and KDevelop.
Thread-safetyThe error reporting is thread safe and atomic.
Type-safetyExtensive use of templates prevent errors introduced by implicit type casting.
Easily extendableCustom types can easily be added to the test data and test output.

Note: For higher-level GUI and application testing needs, please see the Partner Directory for Qt testing products provided by Nokia partners.

QTestLib API

All public methods are in the QTest namespace. In addition, the QSignalSpy class provides easy introspection for Qt's signals and slots.

Using QTestLib

Creating a Test

To create a test, subclass QObject and add one or more private slots to it. Each private slot is a testfunction in your test. QTest::qExec() can be used to execute all testfunctions in the test object.

In addition, there are four private slots that are not treated as testfunctions. 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.

  • initTestCase() will be called before the first testfunction is executed.
  • cleanupTestCase() will be called after the last testfunction was executed.
  • init() will be called before each testfunction is executed.
  • cleanup() will be called after every testfunction.

If initTestCase() fails, no testfunction will be executed. If init() fails, the following testfunction will not be executed, the test will proceed to the next testfunction.

Example:

 class MyFirstTest: public QObject
 {
     Q_OBJECT
 private slots:
     void initTestCase()
     { qDebug("called before everything else"); }
     void myFirstTest()
     { QVERIFY(1 == 1); }
     void mySecondTest()
     { QVERIFY(1 != 2); }
     void cleanupTestCase()
     { qDebug("called after myFirstTest and mySecondTest"); }
 };

For more examples, refer to the QTestLib Tutorial.

Building a Test

If you are using qmake as your build tool, just add the following to your project file:

 QT += testlib

If you would like to run the test via make check, add the additional line:

 CONFIG += testcase

See the qmake manual for more information about make check.

If you are using other build tools, make sure that you add the location of the QTestLib header files to your include path (usually include/QtTest under your Qt installation directory). If you are using a release build of Qt, link your test to the QtTest library. For debug builds, use QtTest_debug.

See Writing a Unit Test for a step by step explanation.

QTestLib Command Line Arguments

Syntax

The syntax to execute an autotest takes the following simple form:

 testname [options] [testfunctions[:testdata]]...

Substitute testname with the name of your executable. testfunctions can contain names of test functions to be executed. If no testfunctions are passed, all tests are run. If you append the name of an entry in testdata, the test function will be run only with that test data.

For example:

 /myTestDirectory$ testQString toUpper

Runs the test function called toUpper with all available test data.

 /myTestDirectory$ testQString toUpper toInt:zero

Runs the toUpper test function with all available test data, and the toInt test function with the testdata called zero (if the specified test data doesn't exist, the associated test will fail).

 /myTestDirectory$ testMyWidget -vs -eventdelay 500

Runs the testMyWidget function test, outputs every signal emission and waits 500 milliseconds after each simulated mouse/keyboard event.

Options

Logging Options

The following command line options determine how test results are reported:

  • -o filename,format
    write output to the specified file, in the specified format (one of txt, xml, lightxml or xunitxml). The special filename - may be used to log to standard output.
  • -o filename
    write output to the specified file.
  • -txt
    outputs results in plain text.
  • -xml
    outputs results as an XML document.
  • -lightxml
    outputs results as a stream of XML tags.
  • -xunitxml
    outputs results as an Xunit XML document.

The first version of the -o 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.

If the first version of the -o option is used, neither the second version of the -o option nor the -txt, -xml, -lightxml or -xunitxml options should be used.

If neither version of the -o 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.

Test Log Detail Options

The following command line options control how much detail is reported in test logs:

  • -silent
    silent output, only shows fatal errors, test failures and minimal status messages.
  • -v1
    verbose output; shows when each test function is entered. (This option only affects plain text output.)
  • -v2
    extended verbose output; shows each QCOMPARE() and QVERIFY(). (This option affects all output formats and implies -v1 for plain text output.)
  • -vs
    shows every signal that gets emitted and the slot invocations resulting from those signals. (This option affects all output formats.)
Testing Options

The following command-line options influence how tests are run:

  • -functions
    outputs all test functions available in the test, then quits.
  • -datatags
    outputs all data tags available in the test. A global data tag is preceded by ' __global__ '.
  • -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.
  • -maxwarnings numberBR sets the maximum number of warnings to output. 0 for unlimited, defaults to 2000.
  • -nocrashhandler
    disables the crash handler on Unix platforms.
Benchmarking Options

The following command line options control benchmark testing:

  • -callgrind
    use callgrind to time benchmarks (Linux only).
  • -tickcounter
    use CPU tick counters to time benchmarks.
  • -eventcounter
    counts events received during benchmarks.
  • -minimumvalue n
    sets the minimum acceptable measurement value.
  • -iterations n
    sets the number of accumulation iterations.
  • -median n
    sets the number of median iterations.
  • -vb
    output verbose benchmarking information.
Miscellaneous Options
  • -help
    outputs the possible command line arguments and give some useful help.

Creating a Benchmark

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.

 class MyFirstBenchmark: public QObject
 {
     Q_OBJECT
 private slots:
     void myFirstBenchmark()
     {
         QString string1;
         QString string2;
         QBENCHMARK {
             string1.localeAwareCompare(string2);
         }
     }
 };

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:

NameCommmand-line ArgumentAvailability
Walltime(default)All platforms
CPU tick counter-tickcounterWindows, Mac OS X, Linux, many UNIX-like systems.
Valgrind/Callgrind-callgrindLinux (if installed)
Event Counter-eventcounterAll platforms

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).

Note: Depending on the device configuration, Tick counters on the Windows CE platform may not be as fine-grained, compared to other platforms. Devices that do not support high-resolution timers default to one-millisecond granularity.

See the chapter 5 in the QTestLib Tutorial for more benchmarking examples.

Using QTestLib remotely on Windows CE

cetest is a convenience application which helps the user to launch an application remotely on a Windows CE device or emulator.

It needs to be executed after the unit test has been successfully compiled.

Prior to launching, the following files are copied to the device:

  • all Qt libraries the project links to
  • QtRemote.dll
  • the c runtime library specified during installation
  • all files specified in the .pro file following the DEPLOYMENT rules.

Using cetest

Syntax

The syntax to execute an autotest takes the following simple form:

 cetest [options] ...

Options

cetest provides the same options as those for unit-testing on non cross-compiled platforms. See Command Line Arguments for more information.

The following commands are also included:

  • -debug
    Test version compiled in debug mode.
  • -release
    Test version compiled in release mode.
  • -libpath path
    Target path to copy Qt libraries to.
  • -qt-delete
    Delete Qt libraries after execution.
  • -project-delete
    Delete project files after execution.
  • -delete
    Delete project and Qt libraries after execution.
  • -conf
    Specifies a qt.conf file to be deployed to remote directory.

Note: debug is the default build option.

QtRemote

QtRemote is a small library which is build after QTestLib. It allows the host system to create a process on a remote device and waits until its execution has been finished.

Requirements

cetest uses Microsoft ActiveSync to establish a remote connection between the host computer and the device. Thus header files and libraries are needed to compile cetest and QtRemote successfully.

Prior to installation of Qt, you need to set your INCLUDE and LIB environment variables properly.

A default installation of Windows Mobile 5 for Pocket PC can be obtained by:

 set INCLUDE=C:\Program Files\Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Activesync\Inc;%INCLUDE%
 set LIB=C:\Program Files\Windows CE Tools\wce500\Windows Mobile 5.0 Pocket PC SDK\Activesync\Lib;%LIB%

Note that Qt will remember the path, so you do not need to set it again after switching the environments for cross-compilation.

3rd Party Code

The CPU tick counters used for benchmarking is licensed under the following license: (from src/testlib/3rdparty/cycle.h)

Copyright (c) 2003, 2006 Matteo Frigo
Copyright (c) 2003, 2006 Massachusetts Institute of Technology

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. Qt 5.0-snapshot
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD.
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP !
 
 
 
 
Partenaires

Hébergement Web