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

Frans Klaver fransklaver at gmail.com
Tue Dec 13 15:32:18 CET 2011


On Tue, 13 Dec 2011 15:08:24 +0100, David Heremans  
<david.heremans at intersoft-electronics.com> 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.
>
> 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.

So you already ruled this one out. Good.

>     b) Create several *.pro files in you project directory tree, one for
> the project itself and one for each unit test?

This would be a decent approach to begin with. To reduce build times you  
might want to consider putting your implementation into a library and link  
your test programs and your main program against it. If you plan on  
unittesting libraries, you can either use the Qt approach (create a  
separate build, exporting objects you wouldn't have in your released  
interface), or the approach we use at the company I work at -- create a  
static library that contains the implementation of your library or  
application. You can then also link your test application(s) against that  
static library.


>     c) Is it possible to have multiple targets in one pro file ? (And if
> so is this a Good Idea(TM)?)

Can't remember, and usually not.

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

There's an awful lot of literature about unit testing. A nice article to  
start with is http://geosoft.no/development/unittesting.html. The most  
important thing though is to remember that tests aren't the goal, they're  
the tool. If all your unittests pass, you might still have bugs and your  
program may not even run. It's just a good starting point in ensuring your  
code does what you intended it to do.

Good luck,
Frans



More information about the Interest mailing list