[Interest] How to use unit testing with Qt framework properly

Samuel Gaist samuel.gaist at edeltech.ch
Tue Apr 10 10:43:06 CEST 2012


On 10 avr. 2012, at 10:29, Gilles Habran wrote:

> Hi guys,
> 
> I have a personal project and I would like to try to use Test Driven Development. For the moment, my only knowledge in Unit Tests come from several books, I never really use that concept in a project.
> 
> Here is the arch of my project folder :
> 
> xxx/xxx.pro
> xxx/src/*.cpp
> xxx/include/*.h
> xxx/obj/*.o
> xxx/bin/
> 
> xxx/tests/yyy.pro
> xxx/tests/*.cpp & *.h
> 
> So I have two projects, one for the main project and one for the tests, because I read that it's better that the tests do not mess with the project and I agree with that.
> 
> For my test project, I plan to have a main.cpp that controls all the tests and my tests classes like this : testAAA.cpp+testAAA.h
> 
> My problem is that I don't know how to "link" my project with unit tests to my main project. 
> I read that I should either link my main project object files (.o) with my test project or have some .so to link to (my main project is not a library, it will have a GUI, DB access, XML, ... so how should I get that .so ?) or finally, add all the files mainProject.cpp and mainProject.h to the unit tests project to have access to them while compiling the project with unit tests.
> 
> Can someone help me with documentation or good practice with unit testing with Qt (blog post, ...) ? 
> 
> I already have read the documentation for Qt Test Lib and I tested it (I included the .cpp in my test project files but it seemed really ugly) and it worked fine so I just need advises how to do it the proper way.
> 
> Thanks a lot.
> 
> Kind regards,
> 
> GH
> _______________________________________________
> Interest mailing list
> Interest at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/interest

Hello,

What I personally do for TDD with Qt is to develop all basic elements of my application in one or more libraries and then the application uses these elements so the hierarchy pretty much looks like this:

MyProject/apps/xxx
MyProject/libs/xxx

In the library I have:
xxx/xxx.pro
xxx/src/*.cpp/*.h
xxx/test/test.pro
xxx/test/test.pri
xxx/test/tst_class1
xxx/test/tst_class2


tst_class1/tst_class1.pro
tst_class1/tst_class1.cpp

So each class has its own unit test.

test.pri contains all the stuff that is needed to link against libxxx.so (dylib, dll etc...) and is included by all tst_xxx.pro

Hope this helps
Samuel


More information about the Interest mailing list