Does anyone know how to get started with unit testing in Xcode 4?
How do I create a test and how do I run it?
EDIT: All the Apple documentation is for Xcode 3
Create a unit test target by selecting the Include Tests checkbox when creating your Xcode project. Add unit test classes to the test target by choosing File > New > File. Import the XCTest framework in your unit test classes. Use the @testable import statement to give your unit test classes access to your app's code.
Writing Unit Test Cases in Swift With an ExampleUnit test cases run with the testing target. Always check if you have a unit test target or not, if not, then add it. Then under this target, you need to create a new test case file. You will use this new file for writing test cases.
Running Tests and Coverage Locally in Xcode To enable code coverage, click the scheme editor in the toolbar. Select the CodecovDemo scheme and choose Edit Scheme. Select the Test action on the left. Check the Code Coverage box to gather coverage data.
See this question: Unit testing in XCode 4
Basically, it says to use GHUnit and ignore SenTestingKit for now (bceause it's broken). GHUnit is pretty nifty.
Although most of the documentation is in fact for XCode 3, the XCode4 User Guide: Building and Running Your Code gives clear step-by-step instructions. Essentially, you create a Cocoa [Touch] Unit Testing Bundle target and add all your test code to that target. Then add the target to your build scheme. Pretty straightforward (not something I'd say for XCode4 in general...)
I just switched from using GTM (which is what GHUnit uses) and it works fine. The main thing you get from GHUnit is the nifty UI which shows you all the tests and whether they succeeded. The other problems mentioned with OCUnit (such as no debugging) are no longer relevant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With