IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)

Chapter 1: Writing a Unit Test

How to write a unit test.

Article lu   fois.

L'auteur

Liens sociaux

Viadeo Twitter Facebook Share on Google+   

Chapter 1: Writing a Unit Test

This first chapter demonstrates how to write a simple unit test and how to run the test case as a stand-alone executable.

Writing a Test

Let's assume you want to test the behavior of our QString class. First, you need a class that contains your test functions. This class has to inherit from QObject:

 
Sélectionnez
#include <QTest>

class TestQString: public QObject
{
    Q_OBJECT
private slots:
    void toUpper();
};

You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it.

Then you need to implement the test function itself. The implementation could look like this:

 
Sélectionnez
void TestQString::toUpper()
{
    QString str = "Hello";
    QVERIFY(str.toUpper() == "HELLO");
}

The QVERIFY() 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.

But if you want a more verbose output to the test log, you should use the QCOMPARE() macro instead:

 
Sélectionnez
void TestQString::toUpper()
{
    QString str = "Hello";
    QCOMPARE(str.toUpper(), QString("HELLO"));
}

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.

Preparing the Stand-Alone Executable

Finally, to make our test case a stand-alone executable, the following two lines are needed:

 
Sélectionnez
QTEST_MAIN(TestQString)
#include "testqstring.moc"

The QTEST_MAIN() macro expands to a simple main() method that runs all the test functions. Note that if both the declaration and the implementation of our test class are in a .cpp file, we also need to include the generated moc file to make Qt's introspection work.

Building the Executable

You can build the test case executable using CMake or qmake.

Building with CMake

Configure your build settings in your CMakeLists.txt file:

 
Sélectionnez
cmake_minimum_required(VERSION 3.16)
project(tutorial1 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)

if(NOT DEFINED INSTALL_EXAMPLESDIR)
    set(INSTALL_EXAMPLESDIR "examples")
endif()

set(INSTALL_EXAMPLEDIR "${INSTALL_EXAMPLESDIR}/qtestlib/tutorial1")

find_package(Qt6 REQUIRED COMPONENTS Core Gui Test Widgets)

qt_add_executable(tutorial1
    testqstring.cpp
)

set_target_properties(tutorial1 PROPERTIES
    WIN32_EXECUTABLE TRUE
    MACOSX_BUNDLE TRUE
)

target_link_libraries(tutorial1 PUBLIC
    Qt::Core
    Qt::Gui
    Qt::Test
    Qt::Widgets
)

install(TARGETS tutorial1
    RUNTIME DESTINATION "${INSTALL_EXAMPLEDIR}"
    BUNDLE DESTINATION "${INSTALL_EXAMPLEDIR}"
    LIBRARY DESTINATION "${INSTALL_EXAMPLEDIR}"
)

Next, from the command line, run either cmake or use the qt-cmake convenience script located in Qt-prefix/<version>/<platform>/bin/qt-cmake:

 
Sélectionnez
&lt;Qt-prefix&gt;/&lt;version&gt;/&lt;platform&gt;/bin/qt-cmake &lt;source-dir&gt; &lt;build-dir&gt; -G Ninja

Then, run your preferred generator tool to build the executable. Here, we're using Ninja:

 
Sélectionnez
ninja

Building with qmake

Configure your build settings in your .pro file:

 
Sélectionnez
QT += widgets testlib

SOURCES = testqstring.cpp

# install
target.path = $$[QT_INSTALL_EXAMPLES]/qtestlib/tutorial1
INSTALLS += target

Next, run qmake, and, finally, run make to build your executable:

 
Sélectionnez
qmake
make

If you're using windows, replace make with nmake or whatever build tool you use.

Running the Executable

Running the resulting executable should give you the following output:

 
Sélectionnez
********* Start testing of TestQString *********
Config: Using QtTest library %VERSION%, Qt %VERSION%
PASS   : TestQString::initTestCase()
PASS   : TestQString::toUpper()
PASS   : TestQString::cleanupTestCase()
Totals: 3 passed, 0 failed, 0 skipped
********* Finished testing of TestQString *********

Congratulations! You just wrote and executed your first unit test using the Qt Test framework.

Example project

Vous avez aimé ce tutoriel ? Alors partagez-le en cliquant sur les boutons suivants : Viadeo Twitter Facebook Share on Google+