{"id":32,"date":"2005-07-16T14:47:00","date_gmt":"2005-07-16T22:47:00","guid":{"rendered":"http:\/\/eschatologist.net\/blog\/?p=32"},"modified":"2009-01-25T19:56:54","modified_gmt":"2009-01-26T03:56:54","slug":"xcode-unit-testing","status":"publish","type":"post","link":"https:\/\/eschatologist.net\/blog\/?p=32","title":{"rendered":"Xcode: Unit Testing"},"content":{"rendered":"<p>Xcode 2.1 introduced <em>integrated unit testing<\/em> to the Xcode IDE.  Xcode includes two unit testing frameworks, target templates for setting up test bundle targets, and infrastructure for running unit tests every time you build your project and reporting their results in the Build Results window just like compilers and linkers do.<\/p>\n<p>With Xcode unit testing, you group your test cases into <em>test bundles<\/em> which are built by separate targets.  This means that you don&#8217;t need to build two versions of your software \u00e2\u20ac\u201d one with tests and one without tests \u00e2\u20ac\u201d and can run your tests against the actual software you want to deliver.<\/p>\n<p>Xcode 2.1 and later include <a href=\"http:\/\/www.sente.ch\/software\/ocunit\/\">OCUnit<\/a> for unit testing of Objective-C Cocoa software, and it includes a new framework called CPlusTest for unit testing of C++ software.  Using either you should be able to test C code with relative ease.  Corresponding target templates are included for creating unit test bundles appropriate for Cocoa and Carbon applications and frameworks, and file templates are included for creating OCUnit and CPlusTest test case classes.<\/p>\n<p>The test bundle target templates have a shell script build phase at the very end that invokes <code>\/Developer\/Tools\/RunUnitTests<\/code>.  <code>RunUnitTests<\/code> looks at the build settings it&#8217;s passed via its environment and determines from that information how to run the tests in your test bundle.<\/p>\n<p>If you&#8217;re testing a framework, <code>RunUnitTests<\/code> will run the appropriate test rig and tell it to load and run the tests in your bundle.  Since your test bundle should link against your framework, your framework will be loaded when the test rig loads your bundle.<\/p>\n<p>If you&#8217;re testing an application, you need to specify the application as the <em>Test Host<\/em> and <em>Bundle Loader<\/em> for your test bundle in its configuration&#8217;s build settings.  The <em>Bundle Loader<\/em> setting tells the linker to link your bundle against the application that&#8217;s loading it as if the application were a framework, allowing you to refer to classes and other symbols within the application from your bundle without actually including them in the bundle.  The <em>Test Host<\/em> setting tells <code>RunUnitTests<\/code> to launch the specified application and <em>inject<\/em> your test bundle into it in order to run its tests.<\/p>\n<p>There&#8217;s even support in <code>RunUnitTests<\/code> for invoking a test rig of your own, rather than the test rig for one of the supplied frameworks.  You just need to specify the <em>Test Rig<\/em> build setting for your test bundle; this should be the path to a tool to run.  It will be passed the path to your test bundle as its first argument, and if it needs to generate failure information it can just generate it in a <code>gcc<\/code>\/compiler-like format on <code>stderr<\/code>:  <\/p>\n<blockquote><p><code>FailingTest.c: 10: error: (1 == 0) failed<\/code><\/p><\/blockquote>\n<p>  This will cause it to show up in the Build Results window as an error, just like a compiler or linker error.  You can see the <code>RunUnitTests<\/code> manpage for more information on the environment in which your test rig will be run.<\/p>\n<p>There&#8217;s a great <a href=\"http:\/\/developer.apple.com\/documentation\/DeveloperTools\/Conceptual\/UnitTesting\/index.html\">Unit Testing Guide<\/a> on the <a href=\"http:\/\/developer.apple.com\/\">Apple Developer Connection web site<\/a> that has lots of information on getting started with Xcode unit testing.  Check it out!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Xcode 2.1 introduced integrated unit testing to the Xcode IDE. Xcode includes two unit testing frameworks, target templates for setting up test bundle targets, and infrastructure for running unit tests every time you build your project and reporting their results in the Build Results window just like compilers and linkers do. With Xcode unit testing,&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[23,21,9,18,22,7,17],"class_list":["post-32","post","type-post","status-publish","format-standard","hentry","category-technology","tag-c","tag-carbon","tag-cocoa","tag-objective-c","tag-programming","tag-unit-testing","tag-xcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p74loH-w","_links":{"self":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/32","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=32"}],"version-history":[{"count":1,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/32\/revisions"}],"predecessor-version":[{"id":33,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/32\/revisions\/33"}],"wp:attachment":[{"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=32"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=32"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eschatologist.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=32"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}