In this series of posts, we’re going to cover the basics of using Xcode's Instruments tool to test and debug code.
First of all, let’s explain what Xcode is:
Xcode Instruments is a performance, analysis, and testing tool for dynamically tracing and profiling OS X and iOS code. It is a flexible and powerful tool that lets you track one or more processes and examine the collected data. Instruments helps you understand the behaviour of both user apps and the operating system. It comes bundled with Xcode when you download it.
If you have ever experienced a bug in your code and have taken many steps to replicate it, then this first instalment is for you. Testing and debugging code can be a very laborious process, requiring huge amounts of time, patience and dedication - especially in the realm of the iPhone.
For example, say you have an application that drills down 4 levels deep in the view hierarchy; now let’s say you have a bug in that fourth level. Usually, you would perform the following steps to replicate that bug:
As you can see this is not an ideal situation and can be very wasteful of a developer’s time. This is where Xcode's Instruments tool comes in; one of the most useful functions it provides is the ability to automate your testing:
- Open up the applications project you wish to test/debug.
- Launch the application in the simulator.
- Open Instruments – This is located in /Developer/Applications/Instruments.
- Select UIRecorder and click Choose.
- Now attach this tool to the iPhone Simulator process. Click the drop-down above Default Target -> Attach to Process -> iPhone Simulator.
- Now click Drive & Record and do all of the steps in the simulator to test your application. At this point, the UI Recorder is recording your every move. When you have finished press the Stop button. Note: After you have recorded your actions, don’t move the simulator as it will mess up the entire process.
- Make any changes to your code.
- Press the Drive & Record. The test is automatically done for you. You should see your mouse move over to the simulator and the system mimicking every action that you did before.
There we are – that’s how you automate testing using Instruments, it’s really as simple as that.
Even though this isn't a very long tutorial, it is incredibly useful and can save hundreds of man hours in development, as well as a lot of headaches!