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.

Conclusion

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.


Sources


[1] "XUnit." Wikipedia. January 12, 2014. Accessed December 08, 2014. http://en.wikipedia.org/wiki/XUnit.

[2] "JUnit." Wikipedia. January 12, 2014. Accessed December 08, 2014. http://en.wikipedia.org/wiki/JUnit.

[3] "IPhone Unit Testing | Sen:te." Sente RSS. Accessed December 08, 2014. http://www.sente.ch/?p=535&lang=en.

[4] "Test Method." At XUnitPatterns.com. Accessed December 08, 2014. http://xunitpatterns.com/Test%20Method.html.

[5] "Testcase Class." At XUnitPatterns.com. Accessed December 08, 2014. http://xunitpatterns.com/Testcase%20Class.html.

[6] "Four-Phase Test." Four Phase Test at XUnitPatterns.com. Accessed December 08, 2014. http://xunitpatterns.com/Four%20Phase%20Test.html.

[7] "Ten Years Of Test Driven Development." Ten Years Of Test Driven Development. Accessed December 08, 2014. http://c2.com/cgi/wiki?TenYearsOfTestDrivenDevelopment.

20 comments :

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

Ancy merina said...

Your blog keeps getting better and better! Your older articles are not as good as newer ones you have a lot more creativity and originality now keep it up!
Web Development Services in Bangalore
BEST web design services in bangalore
ECommerce Website developers in bangalore
Outsource magento ecommerce services india

Moving India said...

Thank you for sharing the wounderful Article!
Major Cities
Packers and Movers in Pune
Packers and Movers in Bangalore
Packers and Movers in Chennai
Packers and Movers in Delhi
Packers and Movers in Mumbai
Packers and Movers in Gurgaon
Packers and Movers in Hyderabad

steve said...


Thanks for a great topic see also :
افضل شركة نقل عفش بالمدينة المنورة


نقل اثاث

افضل شركة نقل اثاث بالمدينة المنورة

ارخص شركة نقل الاثاث بالمدينة المنورة

aarij ali said...

I got what you mean , a debt of gratitude is in order for posting .Woh I am cheerful to discover this site through google. Tax Enquiry Accounts

Robin Jack said...

I as of late discovered numerous helpful data in your site particularly this web journal page. Among the bunches of remarks on your articles. A debt of gratitude is in order for sharing. HMRC Tax Investigation Accountants

Anas Qureshi said...

Really great post. I just unearthed your online journal and needed to say that I have truly delighted in perusing your blog entries. Any way I'll be subscribing to your food and I trust you post again soon. Huge much obliged for the valuable data. HMRC Tax Enquiry Advice

Anas Qureshi said...

Much obliged to you for such an elegantly composed article. It's brimming with astute data and diverting depictions. Your perspective is the best among numerous. Let Campaign Tax Enquiry

List Forever said...

Fastest Cars In The World We can measure automotive excellence in several ways, but top speed which a automobile can achieve is the one everyone secretly cares about the most

Photo Retouch Services said...

Best Photo Retouch Services information is here for you go our site to read the ultimate guide to photo retouching service and image retouch

Photo Background Editor said...

"This photo is perfect except for background which is...". Well, let's say "bad". Have you ever thought like that? Probably, you even made an attempt or two to edit the picture with the ruined Photo Background Editor, to no avail. Surely, not all of us are designers or at least IT savvy. So, here is a way for the rest of humanity to change the back scene of any photograph – the Photo Scissors online tool.

Nearbest10 said...

Nearbest10 is a participant in the Amazon Services LLC Associates Program which is an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com. Amazon, the Amazon logo, AmazonSupply, and the AmazonSupply logo are trademarks of Amazon.com, Inc. or its affiliate.

Best Foot Massager said...

Which Best Foot Massager To Buy – Which Foot Massager Is Right For You? If you ask which Foot Massager model is the finest this year, certainly I would recommend going through this guide to know which Best Foot Massager is actually market leaders.

Packers Movers Chennai said...

Packers and Movers Chennai Give Safe and Reliable ***Household Shifting Services in Chennai with Reasonable ###Packers and Movers Price Quotation. We Provide Household Shifting, Office Relocation, ✔✔✔Local and Domestic Transportation Services, Affordable and Reliable Shifting Service Charges @
Packers And Movers Chennai

Packers Movers Delhi said...

Packers And Movers Delhi to Bangalore

Packers And Movers Delhi to Ahmedabad

Packers And Movers Delhi to Jaipur

Packers And Movers Delhi to Gurgaon

Packers And Movers Delhi to Kolkata

Packers And Movers Delhi to Chennai

Packers And Movers Delhi to Pune

Packers And Movers Delhi to Bhubaneswar

Packers And Movers Delhi to Bhopal

Packers And Movers Delhi to Lucknow

Packers And Movers Bangalore said...

Packers and Movers Bangalore as a Services providing company can make all the difference to your ###Home Relocation experience. Bangalore based Company which offers versatile solutions, Right team that easily reduce the stress associated with a ✔✔✔Household Shifting, ***Vehicle Transportation. we help things run smoothly and reduce breakages and offer you seamless, Affordable, Reliable Shifting Services, Compare Shifting Charges @
Packers And Movers Bangalore