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 Trolltech, 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.

Table of contents:

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.
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 Qt testing products provided by Trolltech 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:

 CONFIG += qtestlib

If you are using other buildtools, 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 testfunctions to be executed. If no testfunctions are passed, all tests are run. If the name of an entry in the test function's test data is appended to the test function's name, the test function will be run only with that testdata.

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

The following command line arguments are understood:

  • -help
    outputs the possible command line arguments and give some useful help.
  • -functions
    outputs all test functions available in the test.
  • -o filename
    write output to the specified file, rather than to standard output
  • -silent
    silent output, only shows warnings, failures and minimal status messages
  • -v1
    verbose output; outputs information on entering and exiting test functions.
  • -v2
    extended verbose output; also outputs each QCOMPARE() and QVERIFY()
  • -vs
    outputs every signal that gets emitted
  • -xml
    outputs XML formatted results instead of plain text
  • -lightxml
    outputs results as a stream of XML tags
  • -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.
  • -keyevent-verbose
    output more verbose output for keyboard simulation
  • -maxwarnings numberBR sets the maximum number of warnings to output. 0 for unlimited, defaults to 2000.

Publicité

Best Of

Actualités les plus lues

Semaine
Mois
Année
  1. « Quelque chose ne va vraiment pas avec les développeurs "modernes" », un développeur à "l'ancienne" critique la multiplication des bibliothèques 64
  2. Apercevoir la troisième dimension ou l'utilisation multithreadée d'OpenGL dans Qt, un article des Qt Quarterly traduit par Guillaume Belz 0
  3. Les développeurs ignorent-ils trop les failles découvertes dans leur code ? Prenez-vous en compte les remarques des autres ? 17
  4. BlackBerry 10 : premières images du prochain OS de RIM qui devrait intégrer des widgets et des tuiles inspirées de Windows Phone 0
  5. Quelles nouveautés de C++11 Visual C++ doit-il rapidement intégrer ? Donnez-nous votre avis 10
  6. Adieu qmake, bienvenue qbs : Qt Building Suite, un outil déclaratif et extensible pour la compilation de projets Qt 17
  7. La rubrique Qt a besoin de vous ! 1
Page suivante

Le Qt Developer Network au hasard

Logo

Comment fermer une application

Le Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.

Communauté

Ressources

Liens utiles

Contact

  • Vous souhaitez rejoindre la rédaction ou proposer un tutoriel, une traduction, une question... ? Postez dans le forum Contribuez ou contactez-nous par MP ou par email (voir en bas de page).

Qt dans le magazine

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 4.2
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