Aggiunta di test
Esistono due modi principali per testare il codice del plugin per l'Qt Creator basato su C++:
Entrambi presentano casi d’uso e configurazioni specifici, descritti nelle sezioni seguenti.
Configurazione di CMake
Prima di aggiungere i test, preparate i vostri file di compilazione. Questi devono riconoscere la dipendenza da QtTest e includere un'opzione CMake per la compilazione del plugin con i test:
# Add a CMake option that enables building your plugin with tests.
# You don't want your released plugin binaries to contain tests,
# so make that default to 'NO'.
# Enable tests by passing -DWITH_TESTS=ON to CMake.
option(WITH_TESTS "Builds with tests" NO)
if(WITH_TESTS)
# Look for QtTest
find_package(${QtX} REQUIRED COMPONENTS Test)
# Tell CMake functions like add_qtc_plugin about the QtTest component.
set(IMPLICIT_DEPENDS Qt::Test)
# Enable ctest for auto tests.
enable_testing()
endif()Test dei plugin
I test del plugin sono profondamente integrati nel plugin stesso e nella sua interazione con Qt Creator. Per aggiungere un test per qualcosa che richiede la configurazione dell’infrastruttura di Qt Creator o del proprio plugin, scrivere un test del plugin.
I test del plugin vengono eseguiti avviando Qt Creator con l’argomento della riga di comando -test <pluginname>. Qt Creator carica quindi completamente il plugin e tutti i plugin da cui dipende, seguendo il normale ciclo di vita del plugin. Dopo che il plugin e tutte le dipendenze sono stati completamente inizializzati, vengono eseguiti i test. Successivamente, Qt Creator si chiude automaticamente. Pertanto, i test del plugin hanno accesso a tutte le funzionalità esportate di tutti i plugin Qt Creator da cui il plugin dipende, come Core::ICore. Utilizzare le normali macro di test di QtTest, come QVERIFY o QCOMPARE per segnalare il successo o il fallimento del test.
Per aggiungere test al plugin, crea una classe basata su QObject con slot privati per i tuoi test e registrala con ExtensionSystem::IPlugin::addTest() nel metodo ExtensionSystem::IPlugin::initialize() del tuo plugin. Proteggi tutto il codice relativo ai test con un controllo per WITH_TESTS, per evitare di distribuire una versione binaria del tuo plugin contenente funzioni di test.
Includi QtTest:
#ifdef WITH_TESTS #include <QtTest> #endif
Quindi implementa le funzioni di test:
#ifdef WITH_TESTS class MyPluginTests : public QObject { Q_OBJECT private slots: void testMyTest() { // a failing test QVERIFY(false); } }; #endif
Registra il tuo test in ` ExtensionSystem::IPlugin::initialize()`:
#ifdef WITH_TESTS addTest<MyPluginTests>(); #endif
Se hai dichiarato l'oggetto di test nel file sorgente, come in questo esempio, includi anche il file .moc necessario per il Meta-Object Compiler di Qt:
#ifdef WITH_TESTS #include "example.moc" #endif
Test automatici
Per aggiungere un test che non dipenda da un'infrastruttura Qt Creator in esecuzione, utilizzare un test automatico che sia indipendente dall'interfaccia del plugin. I parser ne sono un esempio comune, ma è possibile testare molte altre cose in questo modo se sono state scritte in modo modulare.
Anche se il test non risiede nell’interfaccia del plugin, come nel caso dei test dei plugin, è comunque possibile collegarlo alle librerie e alla libreria del plugin stesso, per evitare la duplicazione del codice o la compilazione duplicata.
In linea di principio è possibile utilizzare qualsiasi framework di test automatici, ma QtTest è un framework semplice che si integra bene con Qt ed è utilizzato anche per i test dei plugin.
Per aggiungere il proprio test, aggiungere il file C++ del test e utilizzare add_qtc_test nel proprio file CMake per aggiungere il target di test. Se il test utilizza la libreria del plugin, aggiungerla come dipendenza con DEPENDS.
Nell’esempio seguente, il plugin esporta una funzione addOne:
#pragma once #include "example_global.h" namespace Example { int EXAMPLE_EXPORT addOne(int i); } // namespace Example
E la implementa in un file sorgente:
int addOne(int i) { return i; // that is wrong! }
Il test viene collegato al target della libreria del plugin con ` DEPENDS`:
# conditionally add auto tests
if(WITH_TESTS)
add_qtc_test(tst_mytest
SOURCES tst_mytest.cpp
DEPENDS Example
)
endif()Il test basato su ` QtTest ` include quindi l’intestazione del plugin e verifica la funzione:
#include "examplefunctions.h" #include <QtTest> class tst_MyTest : public QObject { Q_OBJECT private slots: void mytest(); }; void tst_MyTest::mytest() { // a failing test QCOMPARE(Example::addOne(1), 2); } QTEST_GUILESS_MAIN(tst_MyTest) #include "tst_mytest.moc"
Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. 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.