Archives For Unit Testing

XcodeCoverage - code coverage for Xcode

The Xcode 5.1 update proved to be more exciting than I anticipated. And I mean “exciting” in the sense of “breaking the tools I use.” xctool stopped working, and so did my code coverage scripts.

(Lesson learned: Always download the developer preview and give it a try, before running Software Update…)

Facebook updated xctool. And I’m happy to say that XcodeCoverage now works with Xcode 5.1! Thanks to my coworker Mike Maietta for figuring out how to create a wrapper around the new gcov so that it continues to work with lcov.

Your coverage numbers may change slightly as you use Xcode 5.1 — but not much. And we don’t need the __gcov_flush() workaround anymore. Hurrah! Continue Reading…

Code coverage ERROR: no .gcda files found

But code coverage worked on iOS 6…

Update: This workaround is no longer needed for Xcode 5.1. But if you use my XcodeCoverage scripts, see Code Coverage Fixed for Xcode 5.1

Code coverage… oy vey! Back in the days of running on iOS 6 using Xcode 4, measuring code coverage for unit tests was fairly straightforward. A set of coverage scripts I published made it easier still.

Then along came iOS 7 and Xcode 5. Coverage still worked if you continued to run on iOS 6. But on iOS 7, you’d get “ERROR: no .gcda files found” indicating that coverage data wasn’t being captured. “Well, maybe I need to switch from SenTestingKit to Apple’s new XCTest framework.” Nope, that didn’t help. Continue Reading…

NSBrief Podcast

June 10, 2013 — 2 Comments

NSBrief

NSBrief is a fun and informative podcast about Cocoa “developer-y topics.” I’m honored that I had my moment of glory, Episode #97: Jon Reid! Host extraordinaire Saul Mora and I had an hour-and-a-half conversation about iOS unit testing, test driven development, and such.

There are a quite a few other episodes, but two in particular I’d like to recommend:

Check them out. Subscribe to NSBrief! Then in the banner at the top of the NSBrief site, click on “Glassboard” to join the discussions taking place on a private social network.

UIActionSheet example

People assume you can’t write unit tests for user interface code. That just ain’t so. I’ve already shown you how to do UIViewController TDD. Can we do the same for UIAlertView and UIActionSheet? Sure!

This time instead of a screencast, I’ve put my code on GitHub, because you’ll want to incorporate some classes into your tests. Go to my iOSAlertViewActionSheetUnitTesting repository. Continue Reading…

Lock: Sometimes it's OK to unlock for greater testability

Sometimes it’s OK to unlock things for greater testability

In my UIViewController TDD screencast, I put IBOutlets and IBActions in the header file. This made them accessible to unit tests, but I knew it would raise questions:

It’s a fair question. There’s a tension between information hiding (don’t reveal things in the interface) and testability (certain things need to be exposed). Exploring that tension leads me to apply the Extract Class refactoring in places I hadn’t considered before. Continue Reading…

Code coverage hole

Measure your code coverage to find unit testing holes

Update: For Xcode 5.1, see Code Coverage Fixed for Xcode 5.1

One of the first things I do when working on any Xcode project is set up code coverage. If the coverage shows a hole, I know that area is lacking unit tests.

(Be careful, the opposite isn’t true: Just because some code has been touched by unit test execution doesn’t mean it’s actually covered. If altering the behavior of the code causes a test to fail, then you know it’s covered.)

Many people use CoverStory, a a code coverage browser app written by my friend Dave MacLachlan. Others use gcovr to integrate code coverage into their Jenkins continuous integration. Me, I use lcov because it lets me exclude third-party libraries from the measurements before generating an HTML report. Continue Reading…

Apple's unit test template, and my replacement

Xcode 4 has a File Template Library for creating new files. It includes a unit test template called “Objective-C test case class.”

Don’t use it.

Wouldn’t you like your unit test development to go faster? Let me give you a better file template.

Apple’s template puts cruft in your project. Cruft slows you down.

What’s wrong with Apple’s unit test template? Depending on which version of Xcode 4 you’re using, the template can be unnecessarily complicated, due to a distinction between logic tests and application tests that no longer exists.

But the template’s description reveals an ongoing problem: “An Objective-C class containing an OCUnit test case with a header.” A header? What for? Continue Reading…

Xcode unit testing is now fully integrated into the UI!

Unit testing is now fully integrated into the UI!

Xcode unit testing has come a long way for iOS development. Before Xcode 4, I recommended adding third-party unit testing frameworks such as Google Toolbox for Mac (GTM) and GHUnit. But with Xcode 4, the out-of-the-box tools are mostly sufficient. I say “mostly,” because it’s still a mix of the good, the bad, and the ugly. But mostly good, read on for a rundown of the pros and cons… Continue Reading…