Search code examples
iosxcodeunit-testingxcode4ocunit

Organising iOS OCUnit test files in XCode


I am adding unit tests to an existing iOS project using OCUnit with Xcode 4.2. I have successfully added a new build target (following instructions here: http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/) and have set up a few trivial example tests just to check that I can run the tests and that they work as expected.

My question is about adding more test files, and how to organise them. The existing code base is extensive, and contains hundreds of classes that we will need to add tests for sooner or later. Clearly, to put all the tests in the auto-generated 'UnitTests.m' file would be madness. I see that I can add more files using the 'Objective-C Test Case Class' template, but it's not clear to me how that relates to the overarching 'UnitTest.m' that XCode generated when I added the test target.

Does XCode simply look for all test files for the test target and run all the tests? If so, it would make sense to generate a new test file for each class that I'm testing, and not have anything in the main 'UnitTests.m' file. If that is the case, can I safely delete that file, as it won't contain any tests? Or does it need to remain in order to run other tests in other files?


Solution

  • Xcode is initially set to run all the unit tests for the files that are members of the unit test target. In your situation you should create a new test file for each class you are testing. When creating the new file, make sure you add the file to the unit test target, not the application target. Xcode initially adds the file to the application target.

    You can delete the UnitTest.m file. Each unit test file you create contains enough information to run the tests. You can also use the scheme editor to control how Xcode runs your tests.

    Xcode 5 Update

    Xcode 5 adds a test navigator that lets you run a single test, run all tests for a test class, or run all tests. The test navigator reduces the need to use the scheme editor to control how Xcode runs your tests.