[Interest] Unit testing: any best-practices tips ?

jason.mcdonald at nokia.com jason.mcdonald at nokia.com
Wed Dec 14 02:28:36 CET 2011


David Heremans wrote:
> So far I have used Qt in its most simple form. => one *.pro file to generate one executable.
> I've been reading about unit testing with qtestlib, but apparently for each unit test one should build its own executable etc etc.

Hi David,

One unit test per executable is indeed the preferred way to go (where a "unit test" is a set of tests for one class).  This ensures that every unit test starts out with a clean state that hasn't been tainted by any previous failures of other tests and also ensures that one crashing test can't prevent all subsequent tests from running.  Separating the tests for each class also makes it easier to mock/stub classes on which the tested class normally depends.

>But how is this best implement in practice ?
>    a) copy around all the code you want to test in different
>directories and create a separate project for each test? This seems like
>a bad idea, I guess those files will be out of sync in a the shortest
>possible amount of time.
>    b) Create several *.pro files in you project directory tree, one for
>the project itself and one for each unit test?
>    c) Is it possible to have multiple targets in one pro file ? (And if
>so is this a Good Idea(TM)?)

The unit tests for qt5's qtbase module (those in the tests/auto directory tree) now provide a reasonably good example of how to structure a large set of unit tests.

The qtbase unit tests are arranged as a hierarchy that fairly closely matches the structure of the source tree that is being tested.  Each sub-directory in the tree has its own .pro file and any subtree of tests/auto can be executed by running 'make check", so you can choose whether to run all tests or just those for one parts of qtbase.

I would recommend taking a similar approach to structuring tests for application code (and also for structuring the source tree itself) -- use a hierarchy that breaks down the complete application into subsystems, then groups of related classes, then individual tests/classes.

> Can anybody give me some good advice on how to continue? Are there any
> good "DO's and DON'TS"  articles out there?

For writing individual unit tests with qtestlib, I have collected some "Best Practice" information at 

https://wiki.qt-project.org/Writing_Unit_Tests

--
Jason McDonald
QTestLib Maintainer
Nokia Brisbane


More information about the Interest mailing list