ADDING UNIT TESTS

When you're writing code, you obviously want to know whether it works as you intend. Typically, this involves running the code (with or without a debugger attached) either on the emulator or on a real device. If you want to test an aspect of the user interface, such as whether tapping a button really takes you to a particular new page, this is easy — you just run the app, tap the button, and watch what happens.

Things get harder when you want to test whether a small unit of code, such as a calculation or piece of business logic, does what you expect — especially if that unit of code is buried deep within your application. For example, say that you want to check that a customer is at least 18 years old. Assuming that your application validates birthdays only after users have done their shopping and created an account, this test might be quite convoluted. In order to test your code, you'd need to tap your way through many screens before you can get to it. And if you know you need to check a number of different cases, you'll have to create a new account for each set of test inputs. After all this effort, you might find a bug in your code, and once you've fixed that bug, you'll need to re-run all your different test cases, by hand, again.

This can get tedious. However, there's an easy way to simplify this procedure. All you actually want to do is to call your code multiple times with multiple inputs and check that your expected result is returned each time. You don't ...

Get Beginning Windows® Phone 7 Application Development: Building Windows® Phone Applications Using Silverlight® and XNA® now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.