Unit Testing With OpenFrameworks in XCode

I had to struggle unnecessarily to set up unit testing with an OpenFrameworks project in Xcode. If you are impatient like me and don’t mind a bit of a hacky solution here are the steps I used.

  1. Add a test target named “tests” to your project.
  2. Add CxxTest-4.3 to your source root folder and add its location to the header search paths.
  3. Create your unit tests in the “tests” target.
  4. Create the following shell script in your source root folder and name it “cxxtest.sh”.
    1
    2
    3
    4
    5
    6
    7
    
    #!/bin/bash
      # cxxtest script
      ./cxxtest-4.3/bin/cxxtestgen --error-printer -o ./cxxtest-4.3/bin/runner.cpp ./tests/*
      perl -pi -w -e 's/<cxxtest\//</g;' ./cxxtest-4.3/bin/runner.cpp
      rm ./cxxtest-4.3/bin/runner
      g++ -std=c++11 -stdlib=libc++ -o ./cxxtest-4.3/bin/runner  -I ./cxxtest-4.3/cxxtest/ -I ./src/ ./cxxtest-4.3/bin/runner.cpp
      ./cxxtest-4.3/bin/runner

If everything is set up right you should see the following result when you run “cxxtest.sh” in the terminal!

1
Running cxxtest tests (1 test).OK!

The script creates “runner.cpp” from your tests, edits the include statements to match your project structure, and then compiles and runs the test.

Hope this helps!

Comments