{"id":172,"date":"2010-12-07T22:43:01","date_gmt":"2010-12-07T20:43:01","guid":{"rendered":"http:\/\/www.smartersoftware.de\/wordpress\/?p=172"},"modified":"2010-12-07T22:54:19","modified_gmt":"2010-12-07T20:54:19","slug":"unit-testing-an-iphone-app-not-important-to-apple","status":"publish","type":"post","link":"http:\/\/www.smartersoftware.de\/wordpress\/2010\/12\/unit-testing-an-iphone-app-not-important-to-apple\/","title":{"rendered":"Unit Testing an iPhone App \u2013 Not important to Apple?"},"content":{"rendered":"<p>After the base functionality of my iPhone app was in place, I wanted to have a closer look into unit testing. I try to use unit testing where it makes sense when developing for my customers, so I wanted to do that for my own app as well. I was encouraged that there was some documentation in the Apple developer\u2019s guide, and I was really looking forward to putting the framework to use.<\/p>\n<h2>Restructuring my App for testability<\/h2>\n<p>Before being able to properly test the tax calculation functionality, I had to a bit of restructuring to do in order to properly isolate the functionality I wanted to test and to separate it from the GUI. I pulled the tax calculation into its own class and made the GUI use this TaxCalculator. This separation of concerns is one of the benefits of using a Test Driven Approach \u2013 it forces you to think about testability and separation of concerns if you want to have an easily testable class. The changed were totally unnoticeable to the user, but added some value because the code is now much easier to understand and to maintain. It took me less than an hour of development&#160; time, and that was time well spent.<\/p>\n<h2>Initial Problems with SDK 4.1<\/h2>\n<p>The next step was reading up on Unit Testing with the iPhone SDK. There is a chapter in <a href=\"http:\/\/developer.apple.com\/library\/ios\/documentation\/Xcode\/Conceptual\/iphone_development\/\">the iOS Development Guide<\/a> that describes how to add tests to your project. From the start, I was getting strange errors even though the tests seemed to pass. As I was developing these tests while riding the train, I played a bit around in order to avoid the problems, but didn\u2019t have any success. I added a few tests for the TaxCalculator, but ran into some more strange behaviour.<\/p>\n<h2>Some Progress with SDK 4.2<\/h2>\n<p>When I was back home, I googled for my problems and quickly found that it was a problem with SDK 4.1 (thanks to <a href=\"http:\/\/stackoverflow.com\/questions\/3696092\/unit-testing-broken-in-ios-4-1-sdk\">this Stackoverflow question<\/a>). There seemed to be a workaround in 4.1, but as the problem was fixed in SDK 4.2, I just decided to upgrade my SDK which I wanted to do anyways.<\/p>\n<p>After that, I had the next problem \u2013 XCode was telling me that my \u201cbase SDK was missing\u201d. Googling for that problem I quickly found an easy fix for this issue as well (the <a href=\"http:\/\/www.johnalexanderrowley.com\/base-sdk-missing-fix-for-latest-xcode\/\">one I used<\/a> was on John Alexander Rowley\u2019s blog, but there is a number of helpful hints across then net. After that, my tests were finally running without any errors and I was finally able to focus on writing test cases.<\/p>\n<p>However, I soon ran into a lot of problems in properly checking what the tests were doing and if my app was working fine. I\u2019m usually trying to reserve judgment, but in this case I can\u2019t put it any other way:<\/p>\n<h2>What was Apple thinking?<\/h2>\n<p>Using the Apple supplied Unit Test library, the tests are only run during the build phase. Running tests during the build phase is not a bad idea, it can prevent building an app if some tests are failing. But running it only during the build phase has some serious drawbacks: Messages sent to the log do not show up in the debugger console (at least they show up in the system console), but more important: You can\u2019t debug the tests!! (I should note that there are <a href=\"http:\/\/www.grokkingcocoa.com\/how_to_debug_iphone_unit_te.html\">some descriptions<\/a> on how to set up a separate target that would allow you to debug the tests, but the procedure is very complicated \u2013 like setting the environment variable <font face=\"Courier New\">CFFIXED_USER_HOME<\/font> to <font face=\"Courier New\">&quot;${HOME}\/Library\/Application Support\/iPhone Simulator\/User&quot;<\/font>. I gave up on it after unsuccessfully trying for an hour or so.)<\/p>\n<p>I have no idea how anyone can think that what is currently delivered is good enough to use. No one who has never used Unit Tests and pretty much wants (or has) to use it will endure the procedure to make it work. It just seems to be some item on a list that Apple wanted to tick (\u201cUnit Testing? We have that.\u201d) but wasn\u2019t really interested in. Very \u201cun-Apple\u201d \u2026 <\/p>\n<h2>Switching to GHUnit<\/h2>\n<p>Luckily, there seems to be a good open source alternative to the Apple Unit Testing framework, name GHUnit (named after it\u2019s developer, Gabriel Handford) which can be found at <a title=\"http:\/\/gabriel.github.com\/gh-unit\/index.html\" href=\"http:\/\/gabriel.github.com\/gh-unit\/index.html\">http:\/\/gabriel.github.com\/gh-unit\/index.html<\/a>. It was very simple to download and add to my project. Within 15 minutes I had my project use GHUnit and I was able to run the previously developed tests within the simulator:<\/p>\n<p align=\"center\"><a href=\"http:\/\/www.smartersoftware.de\/wordpress\/wp-content\/pics\/iPhoneUnitTesting_12C28\/image.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.smartersoftware.de\/wordpress\/wp-content\/pics\/iPhoneUnitTesting_12C28\/image_thumb.png\" width=\"206\" height=\"400\" \/><\/a><\/p>\n<p>It also enabled me to log to the console and debug my test cases in the XCode debugger just like any normal app \u2013 without having to jump through any hoops. You may argue for a nicer interface (color anyone?), but that is certainly a minor issue. After the disappointing Apple framework, GHUnit delivered on what I was looking for. I\u2019m hoping that the framework will hold up under all the testing I want to throw at it in the next weeks! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>After the base functionality of my iPhone app was in place, I wanted to have a closer look into unit testing. I try to use unit testing where it makes sense when developing for my customers, so I wanted to do that for my own app as well. I was encouraged that there was some [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,32],"tags":[33],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-english","category-ios","tag-unittesting"],"_links":{"self":[{"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/comments?post=172"}],"version-history":[{"count":2,"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/posts\/172\/revisions"}],"predecessor-version":[{"id":176,"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/posts\/172\/revisions\/176"}],"wp:attachment":[{"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/media?parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/categories?post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.smartersoftware.de\/wordpress\/wp-json\/wp\/v2\/tags?post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}