Tutorial for unit testing OF projects in Xcode

I just wrote a short guide on how to set up Xcode to test OF projects using Catch. Although I wrote it as a reminder to myself, I thought it might be useful to others. Suggestions are welcome.

1 Like

A few of us (e.g. @theDANtheMAN, etc) have been talking about unit testing for some time. I landed on google test for my last project – but the simplicity of Catch is attractive. How did you decide on Catch and how do you think it compares to all of the other unit testing options …

Basically, I’m trying to figure out if this would be a good package to use for unitesting for all of OF :smile:

To be honest, because it’s the only c++ unit testing framework I’ve managed to run (and undesrtand) :smile:

I’ve been looking for unit testing frameworks for a while. I’m not a professional coder (just an artist coder) and most of them seemed quite obscure to me. I tried using cppUnit, google test, MiniCppUnit, … to no avail. Catch helped me understand how unit testing works with C++ and now I may be able to use the others. But I like Catch’s simplicity, and the tutorials and documentation are quite clear to me. So I think I’ll stick with it for a while. I don’t think I’ll need anything fancier anytime soon.

Basically, I’m trying to figure out if this would be a good package to use for unitesting for all of OF

I’m sorry I cannot provide you with technical specificities and/or advantages of this framework over the others. But I’ll be happy to know if there’s a better one or not!

Thanks for the guide on OF and Catch. I was able to set it up and have it run on stand alone classes that I added on to OF projects (like the SampleClass.h/.cpp in your example) but haven’t been able to successfully test any classes/methods with OF specific features. i.e. “Undefined symbols for architecture i386: “ofLog::~ofLog()”, referenced from: …error” I am assuming this is an out-of-scope problem / test app doesn’t see the OF structure. I have tried adding #include “ofMain.h”, and/or #include “ofApp.h” and/or #include “main.cpp” to no avail. How did you set up the test target / file structure so you can effectively test the all of your OF app?

Sorry for the delay on my answer.

It’s been a while since I last used this testing framework and I don’t remember the details, but I don’t think I ever tested OF methods with it. Actually, wouldn’t that be testing OF itself rather than your app? There shouldn’t be any problems testing methods that return OF objects (or typedefs), but I don’t see why you should test OF methods themselves.

I’m sorry I can’t be of any help on this matter. But, please, post your findings. I’d like to know how you solved it.

Thanks for the response.

I’m not trying to test oF methods directly, just trying to test methods that include oF methods. For instance using your SampleClass if you add a simple cout and ofLogVerbose output notice like …

  std::string SampleClass::bigStr()
{
  std::stringstream ss;
  ss << _str << " : " << _str.length();
    
    // added code
        // works/compiles/tests
        std::cout  << "Returning-> "  << ss.str();
        // oFSampleProject complies/runs, ofTestSampleProject compile linker error
        ofLogVerbose()  << "Returning-> "  << ss.str();
    
  return ss.str();

It compiles and runs the oFSampleProject but has a linker error when try to compile the ofTestSampleProject with the same Catch test in your example (which doesn’t “test” ofLogVerbose ). I assume the ofTestSampleProject is not linked to OpenFrameworks code for compiling but not sure where / how to to add that. I am sure there is some simple “include”/make file addition/build path addition that is needed but it is escaping me. Otherwise the ability to test is limited to only methods/classes that don’t include any oF methods that make coding with oF so much easier …

Oh, I see. I missunderstood.

You’re absolutely right it doesn’t make any sense to test only methods that don’t include OF methods. I just realized I set up the project and never got to use it! It doesn’t work for me either…