Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jan 28, 2013, 03:36 PM   #1
nashyo
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
Test driven development

What is everyone's opinion on test driven development in an iOS environment?

I have looked over unit testing and done some simple implementation using a test target.

I don't fully understand it.

One example of unit testing I read about involved examining the structure of incoming data, to ensure it was structured as expected. But why? Why not put in-place a conditional statement that captures a null object if one is returned? It seems like a lot of excess code.
__________________
Xcode 4.5.2, SDK 6.0, OSX Mountain Lion, ARC
nashyo is offline   0 Reply With Quote
Old Jan 29, 2013, 02:47 PM   #2
szymczyk
macrumors regular
 
Join Date: Mar 2006
The word test in test driven development can cause confusion. It implies that test driven development is a replacement for running your app and checking that everything works. Test driven development isn't a replacement for testing that your app works.

You can think of test driven development as a way of designing your app's code. Instead of writing code for your app first, you start by writing a test that describes how your app code should behave. Then you write the code to make the test pass. A single test should test one thing, which means an app that uses test driven development ends up with a lot of tests.

As you noted, test driven development requires additional code to write the tests. But test driven development provides the following benefits:
  • It forces you to think about what your code is supposed to accomplish before you write the code.
  • It provides a strong incentive to write code that is simple to test, which usually results in simpler code.
  • It makes your code easier to modify. If you change your code, you have a suite of tests to make sure your changes don't break anything.
  • You spend less time debugging. Instead of stepping through your code in the debugger to find the problem in your code, the unit tests tell you there's a problem when they fail.
Regarding your question about the unit testing example, there can be problems with the incoming data besides a null object. The data could be in the wrong order. Some of the data could be invalid. Just checking for a null object isn't enough to ensure the code works correctly. Unit testing can help you find those problems, but you can write code to find those problems without using test-driven development.
__________________
Mark Szymczyk
Author, Xcode Tools Sensei
http://www.meandmark.com
szymczyk is offline   1 Reply With Quote
Old Jan 29, 2013, 05:29 PM   #3
PhoneyDeveloper
macrumors 68030
 
PhoneyDeveloper's Avatar
 
Join Date: Sep 2008
Unit tests and Unit test frameworks and TDD are all related but different.

I think that unit testing isn't widely used among iOS developers. It's rarely brought up in any of the iOS developer forums that I frequent. There's no good code coverage tool for iOS.

I believe it is widely used among Java developers.

Unit tests and TDD aren't that useful in UI code. I've found unit tests or unit test frameworks to be more useful in model layer code.

Unit testing and other kinds of automated testing are part of modern disciplined development methodologies. If you want to get a job doing iOS development it will come up.
PhoneyDeveloper is offline   0 Reply With Quote
Old Jan 30, 2013, 12:42 PM   #4
nashyo
Thread Starter
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
I like the idea of test driven development. It makes a lot of sense. If it might appear in upcoming jobs then it might be worth learning.

I'm surprised there are not that many responses to this. I thought it would be a popular thread. I guess there aren't that many people doing this.
__________________
Xcode 4.5.2, SDK 6.0, OSX Mountain Lion, ARC
nashyo is offline   0 Reply With Quote
Old Feb 2, 2013, 12:49 PM   #5
amorya
macrumors regular
 
Join Date: Jun 2007
Test driven development is where you write a test before you write each bit of code, in order to make concrete your assumptions. The test tells you when you've correctly written the code. And since all the previous tests you've written are still there, you know at once if you violate any of those assumptions.

I find if your app has huge view controller subclasses where all the logic happens, TDD is hard and not that helpful. But the more you split up your code to work in smaller reusable units, the more useful it is.

For example, I have a library for rendering table views based on keypaths on a data model. Recently, I wanted to make it able to render multiple rows per object in the model, where previously it had only rendered one. That required quite a lot of complicated arithmetic inside the library, to get the index paths right. I wrote some tests first of all, giving each one a hard-coded model and testing that my library was outputting the correct number of table rows and getting them in the right place. Of course, at that point it wasn't, since I hadn't written the feature. Then I wrote it, and as soon as the tests showed up green I knew it worked… even though I hadn't used it in an app yet!
amorya is offline   0 Reply With Quote
Old Feb 8, 2013, 01:56 AM   #6
nashyo
Thread Starter
macrumors 6502
 
Join Date: Oct 2010
Location: Bristol
So Amorya, you had some code that confirmed how many objects were in several arrays you had for several sections of your table view?

You compared these values against how many objects you wanted the arrays to have in the first place?

Did you provide a hard coded value for that check? Such as, "7" objects should be in array 'X' ?

Forgive me for asking you to spell this out. I'm asking because I'm still having a hard time seeing the point.
nashyo is offline   0 Reply With Quote
Old Feb 20, 2013, 05:23 AM   #7
ArtOfWarfare
macrumors 603
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
I just wanted to point out that there are frameworks available for performing unit testing on your UI:

http://developer.apple.com/library/ios/documentation/DeveloperTools/Conceptual/InstrumentsUserGuide/UsingtheAutomationInstrument/UsingtheAutomationInstrument.html#//apple_ref/doc/uid/TP40004652-CH20

I've been reading up on test driven development a lot recently and it all seems like a great idea... But I'm not sure if these UIA tests can be set to run automatically at each build... My understanding is you can do that when using model / controller test frameworks.
ArtOfWarfare is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > iPhone/iPad Programming

Tags
test, unit tests

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Most underrated car you've ever driven? G51989 Community Discussion 12 Mar 21, 2014 09:18 PM
new laptop needed for web development/game design/app development olup Web Design and Development 6 Oct 2, 2013 03:09 PM
Resolved: Need Help Figuring out Test Driven Development on a NSMutableString Category... ArtOfWarfare Mac Programming 0 Apr 7, 2013 11:15 AM
Ok I'm being driven crazy... Georgio iPad Tips, Help and Troubleshooting 6 Dec 2, 2012 02:26 PM
Anybody own or has test-driven a fiat 500? Italianblend Community Discussion 72 Nov 25, 2012 07:09 PM

Forum Jump

All times are GMT -5. The time now is 02:15 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC