Home > programming > Integrating Boost Test Framework with X-Code

Integrating Boost Test Framework with X-Code

Usually people are using X-Code for creating applications in Objective-C, but this IDE is also useful for C++ programming.When you are creating something, you want to test it (or you should want to). One of the unit test framework is Boost::Test. I will describe shortly how to integrate this framework with X-Code (currently 3.2.4).

 

Requirements:

  • X-Code IDE
  • Boost library installed (you can use MacPorts)

 

Creating Project

I will use simple command line tool project for this tutorial. You can choose whatever you need for your project but it should be using C++ programming language.

 

Creating Target

First you have to create new test target for the project. You can do it by right-clicking Target group in X-Code IDE and choosing Add – New Target from context menu. Choose BSD group and Shell Tool template. Input name for the target (e.g. “Unit Tests”) and continue.

Now you have to specify include and library paths for newly created target. Right-click on the new target and choose “Get Info”. Go to Build tab and search for “Header Search Path” and input path to include files from Boost library (when installing from MacPorts the path is /opt/local/include). Next search for “Always Search User Paths” and mark it. The last one is “Library Search Paths” where you have to input path to compiled Boost libraries (when MacPorts – /opt/local/lib).

 

 

Next thing to do for making Boost Test work is to input “-lboost_unit_test_framework” in “Other Linker Flags” setting from test target’s info Build tab.

The last thing to do is to add Run Script. It can be done by right-clicking test target and choosing Add – New Build Phase – New Run Script Build Phase. In Script section paste following text:

"${TARGET_BUILD_DIR}/${EXECUTABLE_NAME}"

 

First Unit Test

Now you can start creating your test suites and test cases. Create new C++ implementation file (without a header) and remember to add it to your’s unit test target.

 

 

Paste this code sample:

#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(smoke_suite)
BOOST_AUTO_TEST_CASE(smoke_test)
{
BOOST_CHECK_EQUAL(1, 2);
}
BOOST_AUTO_TEST_SUITE_END()

You can create as many such files as you wish, but they need to be tight together by the one, which content is presented below (this is also C++ implementation file and also should be added to test target):

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE my_unit_tests
#include <boost/test/unit_test.hpp>

Now you are ready to run your first test suite. Right-click on the test target and choose Build. It will compile and … you will see error. It is because our tast case should fail. You can go directly to the source of the error bu clicking on the error from the build process:


 

After fixing the test the build process should finish successfully.

 

Integrating Tests with Project

It will be better to make X-Code run tests automatically during compilation of our main project, so we will now tie our main project with the test suite. It can be done by right-clicking your main project target and choose Get Info. Being on the General tab you have to add Dependency of test target. Now whenever you will compile the project, unit test will be run.

More about Boost Test framework itself can be found here.

Advertisements
Categories: programming Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: