0

I've seen a variety of workarounds posted in various places that suggest writing custom main functions instead of relying on the Qt QTEST_MAIN() macro when creating a single test execution that works through many different tests of different classes.

Correct me if I'm wrong, but couldn't you just have a single test class and have as many slots as you need to test as many classes as you want? Just instantiate the desired class you want to test inside the slot's implementation and run your tests in that slot. Then, a different slot might instantiate a different class and run different tests. The single QTEST_MAIN is supposed to run through all your slot tests, so everything gets tested, right?

Here are some of the alternate techniques I've read about that don't use QTEST_MAIN:

https://sites.google.com/a/embeddedlab.org/community/technical-articles/qt/qt-posts/creatingandexecutingasingletestprojectwithmultipleunittests

https://stackoverflow.com/a/12207504/768472

Community
  • 1
  • 1
johnbakers
  • 24,158
  • 24
  • 130
  • 258
  • Can you post links to the workarounds that you've found? It's hard to answer this question without knowing the context of why people were attempting to write custom `main` functions. – RA. Oct 07 '13 at 17:27
  • 1
    I think your approach is correct and will work. I did things exactly in the way you discribed - each test slot tests different classes. – vahancho Oct 07 '13 at 18:49
  • @RA. i have updated the question with a couple links. – johnbakers Oct 07 '13 at 23:53
  • @OpenLearner See Pavel's answer below. Essentially, it's undesirable to maintain a single test class that tests multiple classes as soon as you have a lot of classes to test. Consider what a test class would look like if you had to test hundreds, or even thousands of classes. – RA. Oct 08 '13 at 20:18

1 Answers1

2

Of course you can have as much slots as you want in a test class. But sooner or later you will need to separate tests and group them just because you have too many tests to place them all in one class. And you will have to create several test classes.

The original intent of QTEST_MAIN function is to run only one test. If you want to test several classes and you can do it independently of each other, you can put them in separate test classes, add a QTEST_MAIN macro for each of them and compile each class to a separate executable. The plus is that if one test case crushes, other tests continue to run properly. The minus is that you need a test runner to run all tests and check their results, and qtestlib doesn't provide a runner. You can write your own runner or use one of existing ones (example).

The options are:

  • to obey the paradigm of QTestLib. Separate tests to different executables to prevent test failing because of other tests.
  • to store all test in one class. If your app is not tiny, this will be very inconvenient.
  • to run all tests manually using custom main function. It's not very bad, but it's inconvenient too because you need to list test classes manually.
  • to use another test lib. I prefer Google Test. It's much more powerful than qtestlib, it supports death tests, it automatically registers and runs tests and counts their results. There are no such problems in google test. Note that you can use many useful qtestlib features (like QSignalSpy) along with another test framework.
Pavel Strakhov
  • 39,123
  • 5
  • 88
  • 127
  • You make good point for using Google Test instead. I had been wondering about automated test reporting and it sounds like Google Test would be better at that also. – johnbakers Oct 09 '13 at 00:30