Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test Driven Development (TDD): Writing unit tests in Xcode 4

I am keen to write a library project with tests, even if I then include the code in other projects (as opposed to linking to the lib.) for inclusion on ios projects.

It seems checking the 'Include unit tests' isn't enough for xcode 4 to properly initialise a project with tests, and after looking through the Apple documentation I can't find anything relevant.

Does anybody know of any resources or tutorials for getting this working?

Even with the default Cocoa Touch Static Library project, upon choosing "Test" from the "Product" menu, it fails (when linking) with this error (the project is called test1):

library not found for -ltest1
Command /Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2 failed with exit code 1

Any help would be much appreciated.

like image 851
Mat Ryer Avatar asked Feb 20 '11 15:02

Mat Ryer


People also ask

How do I write unit test cases in Xcode?

Enable Unit Tests in Xcode ProjectWhile creating a new Project, click the checkbox “Include Unit Tests”, “Include UI Tests”. Once created, you can able to see a folder in the project called “ProjectNameTests” and the XCode already creates a default test case class with a template generated to start working with.

What is TDD unit testing?

Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases.

What are the 3 steps of test-driven development?

Red, Green and Refactor is the three phase of Test Driven Development and this the sequence that get followed while writing code. When followed, this order of steps helps ensure that you have tests for the code you are writing and you are writing only the code that you have to test for.


1 Answers

OK I finally got this working.

The trick was to create the library without checking the 'Include unit tests' option in XCode 4. Then I created a new target myself.

(in xcode 4)

  1. Create a new Cocoa Touch Static Library project and DON'T check "Include Unit Tests"
  2. Select the project in the navigator
  3. Go into the "Build Phases" tab
  4. Click 'Add Target' and choose 'Cocoa Touch Unit Testing Bundle'
  5. Go through the wizard until the target is created
  6. Then go in the "Product" menu and choose "Edit Scheme..."
  7. Make sure your project (not the test target) is selected and, select "Test"
  8. Click + to add a new target
  9. Choose your test project and click Add
  10. Now press Cmd+U (this is the shortcut for "Product > Test" menu item) and you'll notice the default test fails.

I hope this helps.

like image 58
Mat Ryer Avatar answered Nov 15 '22 16:11

Mat Ryer