Menu

How to access CPPUNIT in MSVC++ 6.0 project?

Help
2002-12-06
2002-12-25
  • Stephen Peterson

    I have some knowledge  of java and junit, but I am pretty new to MSVC and C++.  Example projects just don't put the peices together for me.  They work within themselves,but it's not clear how to combine them with another already-written project into which I want to incorporate test-first-design in an incremental fashion.

    This message is sort of a takeoff on the earlier thread with the same subject.  I have tried a dozen different ways to get CppUnit working with an MFC document-view project that a friend has written, and keep hitting various brick walls.

    In this particular case, I am trying to follow the hints in this earlier post by Tom Plunket:

    "Include path in project settings (C/C++ tab) would be blah/cppunit/include/, then "#include <cppunit/something.h>" would find something.h in cppunit/include/cppunit/, which is how the distribution that I grabbed is set up.

    If you insert the CppUnit project into your workspace as a dependency of each project that needs it, it appears that the output (cppunit.lib or cppunitd.lib) gets linked in automatically. Otherwise, just set the "library include path" in project settings (Link tab) to the directory where CppUnit's libs are getting built to; I don't know what it is off hand but probably something like blah/cppunit/lib/."

    This is my interpretation, which must be in error because it did not work -

    1) Setup my MFC workspace in a folder beside cppunit-1.8.0 in the directory hierarchy.  To insert the cppunit project, I searched for cppunit.dsp under cppunit-1.8.0 and found one in src/cppunit.   I did the "insert cppunit project", then I set my original MFC project to be dependant on the cppunit project.  I then looked at the project settings, and there does not seem to be any lib settings that are changed as a result.  That is my first question.  What went wrong here?

    2) I went to the project settings, link tab, but did not see any "library include path".  Closest match seems to be "Additional library path" which appears after setting category to "Input".
    I set that to "..\cppunit-1.8.0\lib", or is it "../cppunit-1.8.0/lib", or does it matter?

    I then took the cppunit cookbook as a guide to create a new test class.  I selected my MFC project in the classview window, then right-clicked "new class...", set class type to "Generic Class", and Name to  "AnalysisTest", base class derived from "CppUnit::TestCase" as public.

    After hitting ok I get the message "The New Class Wizard cannot confirm that
       'CppUnit::TestCase" is a valid base class.  Derive from "CppUnit::TestCase" anyway?'
       I click OK again
       Then I get the message saying to the effect:
       The New Class Wizard can't find the appropriate header files to include for the
       base class CppUnit::TestCase.  If you choose to derive from the classes anyway,
       you need to add the appropriate header files to my Analysis.h.  It seems like I am doing something wrong still.

    3) It seems like I need to specify the include location in cppunit as well.
       So I cancel out of the class wizard,and look for a project setting to fix this.
       The closest thing I can find is in project settings, C/C++ tab, category Preprocessor,
       "Additional include directories" ..\cppunit-1.8.0\include
      
       I try the New Class Wizard again.  I still get the same error.  I try again changing
       to ../cppunit-1.8.0/include, same results.

    4) I try to build my phonology project after adding this new AnalysisTest class
       I get these two errors:
       error C2653: 'CppUnit' : is not a class or namespace name
        error C2504: 'TestCase' : base class undefined.

    So you can see, I am really lost.  Could someone kindly throw a few suggestions my way?  I will be deeply grateful to you.

    thanks!

     
    • Tom Plunket

      Tom Plunket - 2002-12-06

      hey that was me who said that!  :)

      I'm guessing you're using .NET, and a lot has changed in the UI.  Can't really help too much there, but it sounds like you did "the right thing."  I don't recall getting any feedback that the cppunit.lib gets linked in automatically, but when one project is dependent upon another, and when they have the same project name (e.g. Win32 Debug or something), then it "just works."

      So now we have to get to the errors.  Oh- I prefer '/' as a directory separator but VC6 will take either one.

      Don't use the classwizard.  :)  type in the class declaration:

      class MyTest : public CppUnit::TestCase { };

      You'll need to put the right include file in here.
      #include <cppunit/extensions/HelperMacros.h>

      that's the only one that's necessary for defining tests.  my test.cpp (it's in a DLL) looks like this:

      #pragma warning(disable:4786)

      #include <cppunit/TextTestRunner.h>
      #include <cppunit/extensions/TestFactoryRegistry.h>
      #include <cppunit/extensions/HelperMacros.h>

      #define DLLEXPORT  extern "C" __declspec(dllexport)

      //-------------------------------------
      // Test
      DLLEXPORT bool Test()
      {
          CppUnit::TextTestRunner runner;

          runner.addTest( CppUnit::TestFactoryRegistry::getRegistry().makeTest() );
          runner.run();
          return true;
      }

      I only do console output with CppUnit, with MFC ymmv.

      Looks like your setup is correct with the add'l include directories, maybe you've just forgotten the #include ?

      g'luck;

       
    • Stephen Peterson

      Hi Tom,
      It is Visual C++ version 6, not .net.  Thanks for the info, I'll take another crack at it.

       
    • Stephen Peterson

      Hi Tom,
      Your post raised a couple of questions for me:

      1) you said 'I don't recall getting any feedback that the cppunit.lib gets linked in automatically, but when one project is dependent upon another, and when they have the same project name (e.g. Win32 Debug or something), then it "just works." '

      How can 2 projects in the same workspace have the same project name?  Do you mean the same name on the active configuration selected for each project?  I have selected Win32 Debug on both projects and this doesn't seem to matter any.  If someone could precisely describe the steps on this for VC6 I think it would be really helpful.

      2) you said 'Don't use the classwizard. :) type in the class declaration:  (followed by text for test.cpp)'

      I put this into the directory with my phonology project, then did "project/add to project/files..."  This puts a 'MyTest' class into the classview pane for me.  When I do rebuild, I get

      'fatal error C1010: unexpected end of file while looking for precompiled header directive
      Error executing cl.exe.'

      Am I doing something dumb?

      I've gotten this error before when trying to do this sort of thing, trying to get CppUnit to work with my project.  If anyone can explain what the problem is when C1010 occurs, or maybe refer me to where this sort of thing is properly explained, it would be really helpful.

      3) you mentioned 'I only do console output with CppUnit, with MFC ymmv.'  So I take it you do not have any MFC-built classes in your application - Is trying to build a set of cppunit tests around classes built with MFC going to be a big problem?  That is what I have to work with at this point.

      thanks!

       
      • Tom Plunket

        Tom Plunket - 2002-12-06

        hey there-

        you're right, different project name, shared configuration names.  I'm still not totally in-the-loop as to how this works or if it's editable (e.g. make my Release-Test build link automatically on a Release CppUnit build), but shared config names are what seem to be checked when "hooking it all up."

        Also don't know anything about precompiled headers, that's something I've not used much either.  I'm not sure what the MSDN docs are even telling me, but if you type C1010 into the Index you'll get it.  Maybe just disable PCH in your project.  :)

        I think the example code has an MFC test runner, I've just never used it.  Should be easy enough to get working.  My application area is games, so tools run in a console window anyway and my DirectX app opens a console window and sends its output there.  Didn't want to link in MFC just to get pretty pictures with my tests.  :)

        I may try working up some better steps for "your first steps with CppUnit," but at this time I'm working under a deadline that does not permit me to screw around with that sort of configuration stuff.  Sorry-

        Good luck,

         
    • Stephen Peterson

      I think I managed to eliminate the C1010 problem by turning off the precompiled headers per your suggestion - project -> headings ->  C/C++ tab -> category=Precompiled Headers -> click 'Not using precompiled headers' radio button.   This gives me two new error msgs instead:

      error C2653: 'CppUnit' : is not a class or namespace name
      error C2504: 'TestCase' : base class undefined

      I've seen these error msgs before too - it would be great to know how to solve these ...

      thanks for your suggestions - I think some more detailed configuration instructions would be really helpful - if I ever get this working I certainly will make available the steps - and some specific ways to avoid each error situation I encounter -  maybe a new VC6 section at http://c2.com/cgi/wiki?CppUnit perhaps?

       
    • Wolfgang Stoecher

      > error C2653: 'CppUnit' : is not a class or namespace name
      > error C2504: 'TestCase' : base class undefined

      definitely signs of missing includes! See the examples for what to include (TestCase.h, but better derive from TestFixture; HelperMacros.h, ...)

       
    • Stephen Peterson

      Yes, I believe I did solve this by adding some missing includes.

      Also I wanted to mention another 'discovery' I made, after reading up on the C++ language in Bruce Eckel's 'thinking in C++' - What I really wanted to do was put all my cppunit stuff in a seperate project in the same workspace as my mvc project, but could never get the cppunit project to be able to find my mvc project classes in order to instantiate and exercise them in testcases.  Well, I was finally able to use the HostApp example as a seperate cppunit project that could access my original MVC project classes - here is how:  create yet a third project in the workspace per the vc6 help article 'Create a Static Library (To create a non-MFC static library)'!  This project will then have all the C++ header and source files included that are in the original MVC project.  When built, it will create a lib file of object files for the linker to use when building the cppunit/HostApp project!  Put in a dependancy of HostApp on the static library project.  Include the created lib files in the link settings in the hostapp project.  It seems to work!

      Now if I can just follow the examples of testcases that are written, especially understand how to do assertEquals of two strings - I think examples are all there, but learning it is not easy.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.