Automate acceptance/unit testing C++ in Xcode
In my quest to automate all repetitive tasks, setting up a Xcode project for unit testing (using Boost.Test) is in my crosshairs. I’m in the process of adopting BDD, so you’ll see that terminology mixed with that of unit testing.
Plan
- List out all the manual steps. I started with the process described by Richard Dingwall in a very nice tutorial here.
- Implement task in Applescript
Manual process
Ok, so here’s what I do when I start a new C++ project…
- File->New Project…
- Add Group “Features” @ [project-directory]/Features
- Add Group “Specifications” @ [project-directory]/Specifications
- Add Group “Boost Test Common” @ [project-directory]/Boost Test Common
- Add XcodeLogFormatter.h (allows Boost.Test reports to be properly displayed in Xcode build results; see below) and run_tests.cpp (Boost.Test’s main function) to Boost Test Common
- Add Features Target
- Add->”New Target”
- BSD->Shell Tool
- Name: Features
- Add run_tests.cpp to “Compile Sources”
- Add “/usr/local/include/boost-1_38/” to “Header Search Paths” of Features Target
- Add->New Build Phase->New Run Script Build Phase
- Script: “${TARGET_BUILD_DIR}/${EXECUTABLE_NAME}” (this causes the Features to be run every time the target is built)
- Add Specification (i.e. unit tests) Target: repeat step 6 above for “Specifications”
- Add->Existing Files->[boost unit test library]; check Targets: “Features” and “Specifications”
Applescript
Finally done! Here’s the script and Xcode library script.
Not Doing Now
As I work, I make a list of things that would be nice to have, but are not important enough to think about right now and would take me away from my task:
- Put Boost Test Common files in a commonly accessible include folder
Source Code
XcodeLogFormatter.h (from http://richarddingwall.name/category/xcode/):
#include <boost/test/included/unit_test.hpp>
struct xcode_log_formatter :
public boost::unit_test::output::compiler_log_formatter
{
// Produces an Xcode-friendly message prefix.
void print_prefix(std::ostream& output,
boost::unit_test::const_string file_name, std::size_t line
{
output << file_name << ‘:’ << line << “: “;
}
};
run_tests.cpp (from http://richarddingwall.name/category/xcode/):
#define BOOST_AUTO_TEST_MAIN
#include <boost/test/unit_test.hpp>
#include “XcodeLogFormatter.h”// Set up the unit test framework to use an xcode-friendly log formatter.
struct xcode_config {
xcode_config() {
boost::unit_test::unit_test_log.set_formatter( new xcode_log_formatter );
}
~xcode_config() {}
};// Call our fixture.
BOOST_GLOBAL_FIXTURE(xcode_config);
(updated) Check out the finished AppleScripts:
1. main script @ http://seandenigris.com/blog/files/Make%20new%20C++%20project.scpt
2. Xcode handler library script @ http://seandenigris.com/blog/files/Xcode.scpt
Recent Comments