I am using Qt Creator with qmake to create my project and my test project (QTest-tests). I have the test project running on its own now, but I have a hard time to understand how to test classes from my normal project. I can include the .h files from my project in the test cases, but that's of course not enough.
It throws my linker errors since it does have not the code or a library to look up the code for the class. Is there a simple way to tell qmake to look up all source files in a specific folder to load them? Or what is the correct way to set up such a test project without having to add each source file also to the test project or compiling your application into an library to include?
Writing a Test#include <QTest> class TestQString: public QObject { Q_OBJECT private slots: void toUpper(); }; Note: You need to include the QTest header and declare the test functions as private slots so the test framework finds and executes it. The QVERIFY() macro evaluates the expression passed as its argument.
In the Test framework field, select Qt Test or Qt Quick Test. For a Qt test, select the GUI Application check box to create a Qt application. In the Test case name field, enter a name for the test case. For a Qt test, select the Requires QApplication check box to add the include statement for QApplication to the main.
To get started, select a method, a type, or a namespace in the code editor in the project you want to test, right-click, and then choose Create Unit Tests. The Create Unit Tests dialog opens where you can configure how you want the tests to be created.
Is there a simple way to tell QMake to look up all source files in a specific folder to load them?
qmake supports wildcards so you can have something like
SOURCES += *.cpp
HEADERS += *.h
in your project file.
Also make sure to adjust MOC_DIR
and RCC_DIR
to point somewhere else than the source folder. You don't want to include generated source files in your SOURCES
.
Or what is the correct way to set up such an test project without having to add each source file also to the test project or compiling your app into an lib to include
I prefer to separate my automated tests into two classes:
Unit tests testing single components (usually living in a source file). I mock away dependencies to other parts of the system. Each unit test is a Qt project of its own and only includes the source files being tested + test code + mock code.
Integration tests testing components as whole without mock code. Usually it's the application engine part built as a library and then linked to the test project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With