Unit testing with AppCode
Today we’ll talk about unit testing support in AppCode. We’ll assume that you already know how it works in Xcode, and that you’ve read our previous post about run configurations. Let’s focus more on the details of how it’s done in AppCode.
First, if you already have an Xcode project with configured unit tests in it, and you open it in AppCode, you need to create test run configurations: go to Run | Edit Configurations and add a OCUnit configuration.
Here you need to specify a class and methods to be run – note, that you can use code completion in these fields. If you leave the Class field blank, all tests in the target will be run. The rest of the options are similar to regular run configurations.
Now, when you want to run your tests, you can do it the same way you run your app – choose a configuration in the right upper corner of the IDE and click the green arrow button, or select it from the Run popup (Ctrl+Alt+R) invoked right from the editor.
When you run your tests, a dedicated tool window will appear at the bottom of the IDE showing you the progress and the results when ready.
Let’s take a moment to inspect the options that this tool window provides.
On the left vertical panel there are the two important options: rerun current configuration and stop.
You can find a number of options above the tests tree that can help you investigate the problems faster: hide successful tests, sort them alphabetically, navigate to previous/next failed test (which can also be done with shortcuts Alt+Cmd+Up/Alt+Cmd+Down) and export test results.
To find a test in the tree by name you can use speed search like in many other views – just start typing its name.
Under a gear-like icon you’ll find a number of settings allowing you to control the presentation of the tests in the view and enabling additional features e.g. statistics.
From this tool window you can easily navigate to the failing test code – simply click on the test in the tree. When you have fixed it, you don’t need to re-run the whole bunch of tests, you can re-run only this test in question: select the test and hit Ctrl+Shift+R (or choose it from the context menu).
You can also run a single test from the editor. Position the caret anywhere inside a test method and press Ctrl+Shift+R (Run [test name] from the context menu)to run this particular test. AppCode will create a temporary test run configuration which you can save and reuse later. You can also position the caret within a test class (but outside of the methods) and press Ctrl+Shift+R to run all tests in the class.
Now, in addition to all above, here’s a short FAQ.
If my project doesn’t have unit tests yet, how can I add them?
Currently you need Xcode to do that – can add Cocoa Unit Testing Bundle or Cocoa Touch Unit Testing Bundle target in Xcode. In AppCode you can add individual tests by generating them from Xcode template: in Project view hit Cmd+N and choose File from Xcode template.
Can I run tests on device?
Yes, you can – change the destination for test run configuration the same way as you do with normal run configurations.
Can I debug tests?
Yes, you can. Use Ctrl+Alt+D shortcut to debug instead of Ctrl+Alt+R.
How to run Kiwi tests?
The same way as OCUnit tests – configure Kiwi tests in Xcode, and create a test run configuration. You only need to remember to open *.xcworkspace project file not *.xcodeproj if you have added Kiwi tests via CocoaPods.
That’s about it!