0

Is it possible to use cxxunit or any other unit testing framework (excluding QtTestLib) to test qt widgets?

If yes, then there are two more questions :

  1. How?
  2. Since I am running unit tests using valgrind, can this report some errors?
BЈовић
  • 62,405
  • 41
  • 173
  • 273

2 Answers2

2

Yes, it should be possible. I'm not sure about cxxunit specifically, but it is theoretically possible.

  1. To properly test Qt objects, you will probably need to create/destroy a QApplication object in your global setup and teardown functions. Unless you are specifically testing QApplication functionality, you should only create one for the entire run of the test application. This will allow you to test portions of the widget's logic, but not easily the appearance or UI interactivity of the widget. Also, testing certain items may rely on having the application's event loop running, which would be more difficult.
  2. Valgrind may report some errors. It may also report errors with Qt's code, in particular static allocations that are left to application teardown to reclaim.
Caleb Huitt - cjhuitt
  • 14,785
  • 3
  • 42
  • 49
  • 1) But how to emulate (for example) mouse clicks? 2) It is not very clear (at least to me) what are real memory problems when using qt. I know that the qt manages the destruction of widgets which have parent. – BЈовић Dec 04 '10 at 21:04
  • @VJo: Regarind valgrind, run it on your application or unit test executable. The destruction of widgets that have parents should be visible to valgrind, and therefore not show up as memory leaks. However, the last time I ran valgrind on a Qt application, there were some static allocations that Qt made which were not cleaned up. – Caleb Huitt - cjhuitt Dec 06 '10 at 20:40
  • Ok, the destructors are visible to valgrind, but can you provide some more details. What should be a parent of widgets in unit test? Is it the QApplication object? Which leaks in QApplication are false? – BЈовић Dec 07 '10 at 11:25
1

If you want to test your UI, I suggest to use a UI testing tool like Squish. Unit tests I find more suited to test the logic behind the widgets, not the widgets itself. If you really want to unit-test your Qt widgets, I don't think there is a better solution than QtTestLib.

Valgrind: There is a valgrind plugin for Squish. I haven't used that one myself though. Other unit tests can of course easily be run in valgrind, although I don't know of any solution that fully automates this. One would have to make sure to really suppress all warnings from outside one owns code so that some error in e.g. x11 libs doesn't trigger the unit test to fail.

Frank Osterfeld
  • 24,815
  • 5
  • 58
  • 70
  • 1
    We are already using squish, but squish is not for unit testing. I have seen widgets with really heavy logic within them, which should be unit tested. – BЈовић Dec 04 '10 at 21:06