K
K
Kalombyr2018-02-26 13:37:55
Qt
Kalombyr, 2018-02-26 13:37:55

Unittests in Qt, how to properly organize project structure?

Good day!
There is a main project ProjectName, it uses subprojects.
I decided to do testing through qtest, respectively, I create a new Tests subproject, in the wizard I select the Unit test Qt template, the test skeleton appears.
And this is where the problems begin.
1. I press the launch - only the test is launched and that's it ... Um, I would like to launch the application, and launch the tests by command from the "Tools" menu ... The main application starts only when it is launched from the submenu in the project tree
2. I want to do testing the next class - I create a new class in Tests, I call it tst_myclass.cpp by analogy, I prescribe the rest by analogy,
when starting, the error is "multiple definition of `main'", I google how the tests work, I find out that the QTEST_MAIN macro creates the main () function, I remove this macro in the second class, I run it, only one, the first class is tested.
I look at the implementation of the QTEST_MAIN macro, and I see that only the test specified in the macro parameters is launched. How can I make it test other classes without manually specifying classes in QTest::qExec()?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
res2001, 2018-02-26
@res2001

In general, unit tests (unit tests) are not intended to be embedded in an application. A test is a separate application designed to test components (functions, classes, etc.) of the main application. Usually, tests are not included in the release of the application (I'm not talking now about applications distributed in source, in which case the tests are often included).
To do what you want, just write the test as part of the application, without using the libraries for unit tests. It will be faster.
In principle, some of them can be used, given that most of them output information to the console, create their own main function, ... I.e. in order to use them for other purposes, you need to know the internal organization well and understand how it works.

A
Ariox41, 2018-02-26
@Ariox41

Each test suite in QTest is usually created as a separate subproject (application). Accordingly, the tests are launched as a separate application. The choice of the current active project to run in QtCreator is done through the "projects" panel on the left, or by "ctrl + T", this also applies to tests. In principle, tests can be concentrated in one project, but this is extremely inconvenient just because of the problem you are facing. For this configuration, Catch is better suited . QtCreator seems to be able to run all tests at the same time, but I did not use it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question