Quantcast
Channel: User Chris W. Rea - Stack Overflow
Viewing all articles
Browse latest Browse all 42

Answer by Chris W. Rea for How to set up working logic unit tests target in Xcode 4.5 "Command Line Tool" project?

$
0
0

I've determined a workaround I find suitable and which doesn't appear to have significant drawbacks, other than an added target.

In a nutshell: the solution involves adding a static library target to take advantage of Xcode's ability to create & run unit test code around such a target. The command line tool target then delegates to the static library, where an alternate main()-like function is defined and called by the real main() entry point. The command line tool is kept free of non-trivial code, so the unit test target can access everything worth testing.

Here are the steps:

  • From an empty Xcode, from the menu choose File...New Project.

  • In the resulting dialog, chose OS X...Application...Command Line Tool. For the purpose of this example, I'll assume it is named SampleCmd.

After the basic command line tool project has been created:

  • From the menu choose File...New...Target.

  • In the resulting dialog, choose OS X...Framework & Library...Cocoa Library. For the purpose of this example, I'll assume it is named SampleCmdLogic.

  • Choose type Static, so the command line tool will remain a stand-alone executable.

  • Make sure the Include Unit Tests box is checked.

After the static library project has been created:

  • Copy the main() function from main.m to SampleCmdLogic.m, replacing the @implementation block. (This file will hold the main entry point only. Other files can be added for Objective-C classes, etc.)  Rename the function to libMain().

  • In SampleCmdLogic.h, add a declaration for the new libMain(), replacing the @interface block:
    int libMain(int argc, const char * argv[]);

  • In the command line tool's main.m, add an #import "SampleCmdLogic.h" at top.

  • In the command line tool's main.m, change the entire contents of the real main() function to:
    return libMain(argc, argv);

The code is now ready, but there are required linking steps:

  • In the project settings for SampleCmd, under Build Phases, expand Target Dependencies and add (+) SampleCmdLogic as a dependency.

  • In the project settings for SampleCmd, under Build Phases, expand Link Binary With Libraries and add (+) libSampleCmdLogic.a

Everything is now ready. When you change to the SampleCmd target and choose Product..Run from the menu, the build should succeed and output generated as expected. When you change to the SampleCmdLogic target and choose Product...Test from the menu, the build should succeed and the unit tests will run. The single issue reported will be the initial default failing unit test assertion inserted by Xcode, in SampleCmdLogicTests.m. This is expected.

From this point on, proceed to add all logic and corresponding tests to the SampleCmdLogic target. The SampleCmd target is to remain trivial and providing the command line tool entry point only.


Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>