I want to run tests in Xcode 4 using OCUnit without launching the simulator. Please, don't try and convince me I am doing unit testing wrong or anything like that. I like to do TDD the traditional way: write the API for the class in the tests, then make the class pass the tests. I will write separate tests that are end-to-end that run in the simulator.
If there's no way to do this, then please can someone tell me how to have the test harness not instantiate the whole app? My app is event driven, and it sends a bunch of events through when it starts up that mess with my tests.
In Xcode 5 you can run a single test case by clicking the little play button that you can find next to the test in the test navigator or next to the test method in the editor. In both places it shows up when you hover over it.
Run the test - ⌘ U "Command-U" is the shortcut to run all the tests in the current scheme. If you need help memorizing it imagine a teenager sending a text message to Xcode saying "I command u 2 run the unit tests".
To add a unit test target to an existing Xcode project, choose File > New > Target. Select your app platform (iOS, macOS, watchOS, tvOS) from the top of the New Target Assistant. Select the Unit Testing Bundle target from the list of targets.
In Xcode, choose New File, then select the Unit Test Case Class template to automatically create an appropriate class. The test method should contain three steps, in the order described below: Arrange. Create any objects or data structures that the code path you're exercising uses.
Please can someone tell me how to have the test harness not instantiate the whole app? My app is event driven, and it sends a bunch of events through when it starts up that mess with my tests.
I use Xcode 4's built-in testing. App instantiation may seem like a pain, but as I write on Xcode Unit Testing: The Good, the Bad, the Ugly, it makes it possible to write tests without distinguishing between logic tests and application tests. Specifically, it lets me write unit tests for view controllers.
Here's what I do to avoid my full startup sequence:
Edit the scheme
runningTests
to YES
Edit your app delegate
Add the following to -application:didFinishLaunchingWithOptions:
as soon as it makes sense to:
#if DEBUG if (getenv("runningTests")) return YES; #endif
Do the same for -applicationDidBecomeActive:
but simply return
.
Update: I have changed my approach. See How to Easily Switch Your App Delegate for Testing.
In the last xcode version (5.0.2) you can do this in very easy way. Choose your Test target, "General" tab. Set "None" in field "Target". Then tap on "Build phases" tab and remove your Main target from "Target dependencies".
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