title: QTest - writing tests for Qt-based library and exposing classes
body: <p>I have a library (let's call it <code>MyLib</code>) which has the following structure:</p>
  
  <ul>
  <li><p><strong>mylib_global.h</strong> - determines whether a <code>Q_DECL_IMPORT</code> or <code>Q_DECL_EXPORT</code> is behind the <code>MYLIBSHARED_EXPORT</code> prefix which is used to expose specific symbols of the library when writing an application that uses the library:</p>
  
  <pre><code>#ifndef MYLIB_GLOBAL_H
  #define MYLIB_GLOBAL_H
  
  #include &lt;QtCore/qglobal.h&gt;
  
  #if defined(MYLIB_LIBRARY)       // Set using DEFINES inside MyLib.pro
  #  define MYLIBSHARED_EXPORT Q_DECL_EXPORT
  #else
  #  define MYLIBSHARED_EXPORT Q_DECL_IMPORT
  #endif
  
  #endif // MYLIB_GLOBAL_H
  </code></pre></li>
  <li><p><strong>MyLib.h</strong> - contains the main class which also includes various other classes:</p>
  
  <pre><code>#include "mylib_global.h"
  #include "MyLibOtherClass1.h"
  #include "MyLibOtherClass2.h"
  
  // Symbol will be exposed to the application that links against MyLib
  // and can be directly interacted with from the application's code
  class MYLIBSHARED_EXPORT MyLibMainClass {
      public:
          ...
      private:
          MyLibOtherClassA ocA;
          MyLibOtherClass1 oc1;
          MyLibOtherClass2 oc2;
          ...
  }
  
  // Symbol will not be exposed to the application that links against MyLib
  // however it is indirectly used through the `MyLibMainClass`
  class MyLibOtherClassA {
      ...
  }
  </code></pre></li>
  <li><p><strong>Source files and other classes (header+source)</strong> - contain the declarations and definitions for the underlying logic of <code>MyLib</code>.</p></li>
  </ul>
  
  <p>As you can see I have a mixture of classes - some are defined in the same header file as the library's main class (it controls all other class instances), others are in different header files. There are just very few classes with the <code>MYLIBSHARED_EXPORT</code> which I need to expose in order to be able to access the library's features when linking against it.</p>
  
  <p>This is perfectly fine when writing a normal application. However I've decided to write some tests (using the Qt test framework) since I alter the code quite often (for now :)), there are many complex dependencies in there and I want to make sure that after each change all tests I have written are covered and the functionality remains the same as intended.</p>
  
  <p>When writing tests I need to access ALL classes to ensure that every cog is turning in the right direction (even though it's not exposed to the outside). How do I deal with this? I can add <code>MYLIBSHARED_EXPORT</code> in front of all classes but this will also allow normal users to see stuff, that is only intended to be used indirectly. This can be regulated more precisely using another <code>#define</code> which can be set through the project's settings and only when defined all class that don't have the prefix will get one, otherwise they will be not exposed. While this solution is very likely to do the trick it will create a code mess, which I'd like to avoid if possible.</p>