Since you are trying to "check some class code", I would really recommend learning to use QTestLib . It provides the QTest namespace and the QtTest module , which contains a number of useful functions and objects, including QSignalSpy , which you can use to verify that certain signals are emitted.
Since you will eventually integrate with the full GUI, using QTestLib and testing without sleep or waiting will give you a more accurate test - one that better reflects true usage patterns. But, if you decide not to follow this route, you can use QTestLib :: qSleep to accomplish what you requested.
Since you just need to pause between starting the pump and turning it off, you can easily use the one-shot timer:
class PumpTest: public QObject { Q_OBJECT Pump &pump; public: PumpTest(Pump &pump):pump(pump) {}; public slots: void start() { pump.startpump(); } void stop() { pump.stoppump(); } void stopAndShutdown() { stop(); QCoreApplication::exit(0); } void test() { start(); QTimer::singleShot(1000, this, SLOT(stopAndShutdown)); } }; int main(int argc, char* argv[]) { QCoreApplication app(argc, argv); Pump p; PumpTest t(p); t.test(); return app.exec(); }
But qSleep() will definitely be easier if all you are interested in is checking a couple of things on the command line.
EDIT . Based on comments, usage patterns are required here.
First you need to edit the .pro file to enable qtestlib:
CONFIG += qtestlib
Secondly, you need to include the necessary files:
- For the QTest namespace (which includes
qSleep ): #include <QTest> - For all elements in the
QtTest module: #include <QtTest> . This is functionally equivalent to adding an include for each element that exists in the namespace.
Kaleb Pederson Sep 20 '10 at 15:58 2010-09-20 15:58
source share