c++unit-testingcxxtest

Is there a #define _KEYWORD_ for CXXTEST compiler to exclude blocks of code?


I need to temporarily put a main() function in my object to test alternate functionality (beyond a simple unit test), but I keep having to comment out my main() function to run my unit tests.

Is there a #define _KEYWORD_ for CxxTest to allow this to happen automatically?


Solution

  • You simply need to encapsulate the offending code with a compiler directive based on the definition of the CXXTEST_RUNNING keyword.

    For example:

    class myClass {
      public:
        myClass () {}
    };
    
    #ifndef CXXTEST_RUNNING
    int main (int argc, char *argv[]) {
      // Temporary runner. Typically deleted upon completion
      // of alternate functionality added later in the project.
    }
    #endif
    

    In this case, when using the CxxTest framework the main() function will be ignored in favor of the main() created by the testing framework. Otherwise, the main() function you have provided will be used.