Monday, December 8, 2014

Setup What? Origins Of Four Step Unit Testing in iOS

When I’m at my desk during the week and have written new code, I always create a new unit test file, and it looks something like this:

I asked myself two questions:
  1. Why do I create a new testcase class for every new class?
  2. Why do all of the tests I have always contains four-steps?

History of XCTest and xUnit

XCTest is based off of OCUnit and falls under the family of xUnit testing frameworks. xUnit is the collective name for several unit testing frameworks that derive their structure and functionality from Smalltalk’s SUnit [1], notably JUnit, which was also authored by Kent Beck [2]. In March 1998, OCUnit was written by Sen:te using Kent Beck’s description of the Smalltalk testing framework [3]. Understanding the origins of XCTest and JUnit helped me discover that most of the testing habits I have today all derive from Kent Beck’s Simple Smalltalk Testing: With Patterns.

One Testcase Class

I recommend that developers write their own unit tests, one per class. The framework supports the writing of suites of tests, which can be attached to a class. I recommend that all classes respond to the message “testSuite”, returning a suite containing the unit tests.
— Kent Beck, Simple Smalltalk Testing: With Patterns

It was Kent Beck’s philosophy that led to the practice of creating one testcase class per class. Beck believed that test logic should be encoded as a single test method on some class [4], and a testcase class gives you a place to group these similarly related test methods [5]. Making test methods instance methods of a testcase class and creating a testcase object for each test, allows us to manipulate the test methods at runtime, such as running a single test case, a group of test cases, or skipping a particular test case [6].

Source: xUnitPatterns

Four-Phase Test

Source: xUnitPatterns

The idea of having a setup method, a test method, then a tearDown method come from Beck’s work, and has been formalized into what is called the Four-Phase Test. In the Four-Phase Test, every test has four distinct phases that are executed in sequence: setup, exercise SUT (system under test), result verification, and fixture teardown [6]. The purpose of creating clear distinctions in the phases was to make the system under test extremely obvious.

The inspiration for the design was based around the fact that automated tests should serve at least two purposes:

First, they should act as documentation of how the system under test (SUT) should behave; we call this Tests as Documentation. Second, they should be a self-verifying executable specification.
— Gerard Meszaros, xUnitPatterns

The Four-Phase Test can be thought of as a state machine. In the fixture setup phase, the test establishes the prior state of the world. In the second phase we interact with the SUT, which transitions us to the next state. In the third phase we analyze the post state of the world and verify that it meets our expectations. Then in the fourth phase, we reset the state of the world prior to running the test.


Unit testing may sometimes become a laborious process, so it’s refreshing to take a step back and understand why we do the things that we do. The inspiration of unit testing frameworks in iOS or OS X has been around since the first version of SUnit in 1994 [7], and given that unit testing hasn’t changed much since then, it seems that the paradigms of one testcase class per class and four-phase testing are here to stay.


[1] "XUnit." Wikipedia. January 12, 2014. Accessed December 08, 2014.

[2] "JUnit." Wikipedia. January 12, 2014. Accessed December 08, 2014.

[3] "IPhone Unit Testing | Sen:te." Sente RSS. Accessed December 08, 2014.

[4] "Test Method." At Accessed December 08, 2014.

[5] "Testcase Class." At Accessed December 08, 2014.

[6] "Four-Phase Test." Four Phase Test at Accessed December 08, 2014.

[7] "Ten Years Of Test Driven Development." Ten Years Of Test Driven Development. Accessed December 08, 2014.


Aashi siva said...

It’s really amazing that we can record what our visitors do on our site. Thanks for sharing this awesome guide. I’m happy that I came across with your site this article is on point,thanks again and have a great day. Keep update more information..

J2ee Training in Chennai Thiruvanmiyur

Sathya G said...

the blog is very useful, interesting and informative. thank you for sharing the blog with us. keep on updating.
Informatica Training in Chennai Thiruvanmiyur

Haritha said...

You did a great job.. Thanks a lot for sharing this useful informative post with us.. Keep on blogging like this informative post with us, to develop my career in the right way.
Hadoop Training in Chennai Guindy

seoindiarank chennai said...

Superb i really enjoyed very much with this article here. Really its a amazing article i had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us. expecting for your updation.
Digital Marketing Company in Chennai

Movers Packers said...

Packers and movers Pune
Packers and Movers in Bangalore
Packers and movers in Pune
Packers and Movers Bangalore