I have a Qt Unit test (sub)project, which generates me one class (with the main generated by QTEST_APPLESS_MAIN
).I can start this from within Qt Creator as console app.
Q: How would I add additional classes as test cases to this particular project.
- If these classes only have "test" slots (
private Q_SLOTS
), the methods are not called, but just the ones of the class withQTEST_APPLESS_MAIN
- Since there can be only one
main(..)
, I cannot useQTEST_APPLESS_MAIN
with more than one class in the project (is that correct?) - Of course, I can manually "wire" the slots in the (additional) classes with the one class containing the
main
, but this is very tedious.
So what is the best way to run unit test over several classes in a unit test project?
PS: In " Using QT Unit Tests in a project - conflicting main(...) functions " a Blog is mentioned, however, I cannot download the zip describing the solution.
As per the solution you linked to, the way to accomplish testing two (or more) classes within a single Qt unit test project is to ensure that each class to be tested has a corresponding test class, and that you've created a custom
int main
that executes each test class.For example:
Obviously, the different test classes can be spread out over multiple translation units, then simply included in the translation unit with your
int main
. Don't forget to include the appropriate.moc
files.Searching for this same answer, I found a very good solution from http://qtcreator.blogspot.de/2009/10/running-multiple-unit-tests.html. He creates a namespace with a container that registers all the tests created (via the DECLARE_TEST macro), and then uses it to run all the tests on the list. I rewrote it to fit my code and I post my version here (My Qt Creator version: 4.1.0):
Then, just add the ADD_TEST(class) line in your test header like this:
And and to run all the tests, just do:
Based in the accepted answer and if you are using C++11 you could be interested in a solution using lambdas. It avoids you write the same code everytime. Although you can replace the lambda with a function, I think a lambda is cleaner.
Sample test
The way I do it:
I basically made a slight variation of this post.
I'm using the following code to collect all test results: