Certain semantic conventions used in Qt-based applications dictate specific ways in which unit tests should and should not be constructed. In particular:

  1. Many Qt classes take the Parent pointer as a constructor parameter. Not all subclasses of the base type specified for that parameter are semantically valid for any given Qt class.
  2. Qt Parent objects take ownership of their children, including memory management. This implies that Parents delete children when they are deleted themselves.

Implications for automatically-generated test cases are as follows:

We recommend the following procedure for unit testing Qt-based classes:

  1. Automatically generate unit tests; this will give you a template.
  2. Keep one of the generated tests as a template, preferably one that passes (does not throw an exception) when run.
  3. Using the one test as a template, replace test object allocations by declaration with those on the heap (using new).
  4. At the end of the test, if a parent object is used, delete only that—whether it is the primary test object or a secondary test object.
  5. Validate the one test by using a debugger to observe the final test object states and inserting appropriate assertion macros.
  6. Add more test cases (using the GUI dialog option, and using the first validated test case as the template).