jump to navigation

Every Test is Valuable 17 July 2012

Posted by Oliver Mason in Uncategorized.
trackback

After reading Graham Lee’s Test-Driven iOS Development; (disclaimer: affiliate link) I have (again) adopted a test-driven approach to software developing. Whenever I create a new class I write a bunch of tests exercising the class’ properties. One might question the value of this, because there is not really any reason why those should not work. However, having such a test in place just uncovered an as-yet unnoticed bug I introduced in a project.

Originally the class property in question was going to be set in the `init` method, so I tested for the presence of the property after creating an instance of the relevant class. Easy pass. Weeks later I did something (and forgot to run the test suite). Today I did something else, and this time I did run them. Hey presto, failed test. And completely unexpected, because it was the property exercising one. How on Earth did that happen?

Upon closer inspection I tracked it down to a refactoring, where I extracted some code from `init` as there were now multiple ways an object could be initialised. The failing code was part of that, and I realised that it was called from the new `initFromFile:` method, but *no longer from the default initialiser*. Easy mistake to make. And had I run my test-suite more consistently, my application would not have been with a potential bug in the mean-time.

What did I take home from this?

  • Even Mickey-Mouse-tests are valuable. No test is too small to be useful (provided it’s actually testing something valid).
  • The test-suite should really be run after every change. I’ll have to check if I can get Xcode to run them automatically after each compilation…
Advertisements

Comments»

1. Javier Soto (@JaviSoto) - 18 July 2012

Go to your target settings and search for the option “test after build” :)

Good post!


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: