[Qt-creator] Unit testing private parts of shared libraries

Elvis Stansvik elvstone at gmail.com
Tue Oct 17 09:51:59 CEST 2017


2017-10-17 9:45 GMT+02:00 Christian Gagneraud <chgans at gmail.com>:
> On 17/10/2017 8:42 pm, "Christian Gagneraud" <chgans at gmail.com> wrote:
>
> On 17/10/2017 8:35 pm, "Elvis Stansvik" <elvstone at gmail.com> wrote:
>
> 2017-10-17 9:29 GMT+02:00 Christian Kandeler <christian.kandeler at qt.io>:
>> On Mon, 16 Oct 2017 18:57:19 +0200
>> Elvis Stansvik <elvstone at gmail.com> wrote:
>>
>>> I'm working on some tests for my own application, and started thinking
>>> about unit testing of private parts of shared libraries (think the
>>> _p.h/_p.cpp parts). Such parts are normally not exported AFAIK (in the
>>> visibility attribute sense). This becomes a problem if you link the
>>> unit tests dynamically against the code under test, as the tests won't
>>> be able to see those symbols.
>>>
>>> So it seems to me there is only a few options:
>>>
>>>  1. Exporting the private parts after all,
>>>  2. Adding the sources of the code under test to the unit tests,
>>>  3. ?
>>
>> You might want to take a look at the Q_AUTOTEST_EXPORT macro in Qt and how
>> it is used.
>
> Ah, didn't know about that one, thanks. That's certainly possible in
> the case of Qt, where there's a "developer build" which can make this
> macro effective. It does mean that you're not testing the library
> as-shipped though. But I guess short of exporting everything outright,
> there's no other way.
>
>
> There is another way, and it's used in qbs I believe, that is option 2:
>
>
> Sorry: s/Qbs/QtC/
>
>
>
>
>
> #include "Foo.h"
> #include "Foo.cpp"
>
> class FooTest: QObject
> {
> ...
> }
>
> It requires very low coupling tho. Which is a good practice, but not always
> easy to achieve, unless you strongly stick to the cpp unit testing idiom.

Alright, yes I didn't look through all tests in QtC, but suspected
this approach was used as well.

The downside I guess that again, you're not testing the code exactly
as shipped and that compilation times go up a bit.

I think I might have asked for some miracle here, when I already knew
the options. Guess I just have to decide on what I want to do. I'll
probably end up exporting the private parts after all, but namespace
them with "Internal" or similar, like is done in QtC.

Thanks for the input!

Elvis

>
> My 2 cents,
> Chris
>
>
>
>
> Elvis
>
>>
>>
>> Christian
>> _______________________________________________
>> Qt-creator mailing list
>> Qt-creator at qt-project.org
>> http://lists.qt-project.org/mailman/listinfo/qt-creator
> _______________________________________________
> Qt-creator mailing list
> Qt-creator at qt-project.org
> http://lists.qt-project.org/mailman/listinfo/qt-creator
>
>
>



More information about the Qt-creator mailing list