Archives For Unit Testing

Skill in Unit Testing is very important for TDD. This category contains every article I’ve written on iOS Unit Testing, for both Objective-C and Swift.

Enumerations with associated values are my favorite feature of Swift. But how can we write unit tests against them? “Make them Equatable and use XCTAssertEqual” is common advice.

I’m here to argue otherwise. In fact, let’s use this as a jumping-off point to discuss Swift Equatables in unit tests.

Apples and oranges: Sometimes you shouldn't care about Swift equatable testing Continue Reading…

I’ve shown you how to replace real objects with fakes in Swift, so that you can begin creating useful mock objects. We did this by moving away from a concrete type to a protocol. Anything that implements the protocol will work, so fakes are no problem.

But what do we do when we can’t change the signature? How do we substitute fake arguments, or a fake return value?

It’s time to talk about partial mocks in Swift.

Continue Reading…

Last time, we looked at How Does Swift Support Stubbing and Mocking? Swift’s extension declaration opens the way, letting us attach new protocols to existing classes. That much is well-known in the Swift community.

But then what? How do we create useful mock objects? I’ve seen many examples that are okay… to start with. But in the long run, I’m afraid they’ll fall short for ongoing maintenance.

I wrote OCMockito because I wanted Objective-C mock objects that struck a better balance between “precise” and “forgiving”. I also wanted test code that was easier to read and write. What can we do with Swift mock objects? Continue Reading…

You’re writing a unit test in Swift. You feel the need to avoid interactions with a real object. You want to provide a fake object, or a “test double” (like a stunt double). But Swift is strict about types! What do we do?

Continue Reading…

I try to avoid asynchronous tests because they’re slow. But sometimes they’re worth having. When you do have them, it’s worth spending extra effort to make them as fast as possible.

My mistake was that I only focused on the speed of the happy path. Today, I learned a way to improve the speed of failure scenarios, at least around testing network requests.

Continue Reading…

Since 2001, I’ve relied on an understanding of test execution flow in xUnit frameworks. But somewhere along the way, my understanding of the XCTestCase life cycle got messed up. I picked up an assumption that’s just wrong.

At best, it’s an assumption that can bloat our test runs. At worst, it can wreak havoc.

When is an XCTestCase deallocated?

This is the big question. I had it right for a long time, but then I lost it. Continue Reading…

I’ve been unit testing view controllers for as long as I’ve worked in iOS. My screencast How to Do UIViewController TDD is for folks who want to do TDD, but couldn’t figure out how in a programming model centered around view controllers.

But for every person who wants to know how, there are others who question the whole idea. They wonder if unit testing view controllers is worth it at all. So this time let’s skip the “how” and focus on the “why”.

One reader asked me how to win over a team lead who is test-reluctant. The team lead wanted the reader to stop spending time writing unit tests for view controllers. “He questioned why I unit test UI when it seems to take a long time and does not seem necessary.” Continue Reading…

When a test fails, we want to know the location of the failure. Getting this information in Objective-C required us to dance with the preprocessor. But with Swift, it’s much more straightforward.

Continue Reading…

Refactor tests, I say. Extract those helper methods. …But sometimes it’s not that simple! You have be careful when extracting an assertion test helper. Especially when it becomes long or complex.

YO DAWG, I HEARD YOU LIKE UNIT TESTS, SO I PUT UNIT TESTS IN YOUR UNIT TESTS SO YOU CAN TEST TESTS WHILE YOU TEST
Continue Reading…

Tests are code, too.” So you may ask: who tests the tests? Who watches the watchmen? Is TDD flawed, because it’s turtles all the way down?

This is about speed. The more I trust the tests over a section of code, the more fearless I can be with bold refactoring. Fearless refactoring means I don’t spend time checking correctness. That’s what the tests are for! Instead, I can focus completely on good design.

So how can you increase confidence in your test code? Continue Reading…