Test drive your windows phone application
Dror Helper
Senior consultant
@dhelper | http://blog.drorhelper.com | drorh@oz-c...
About.ME
• Developing software (professionally) since
2002
• TDD/Unit testing evangelist
• Not a windows phone 8 expert
• ...
Unit testing mobile applications is:
Impossible

or
Too damn hard

or
Not cost effective
The truth is that unit
testing mobile applications
is easy and fully
supported* by your IDE
A unit test is…
Test individual unit of work
For developers by developers
Pillars of good unit test
1. Tests specific functionality
2. Clear pass/fail criteria
3. Good unit test runs in isolation
This is a unit test
[Test]
public void CheckPassword_ValidUser_ReturnTrue()
{
bool result = CheckPassword(“Skroob”, “12345...
Test classification
• Unit tests should be:
– Small
– Atomic
– Test a single functional unit
– Isolated!

• Integration te...
Why write unit tests
Saves time & money
Don’t need to run full flow to test code
Reduce manual testing effort
Catch regres...
Before that - a History lesson
In the distant past (2008)
The ultimate tool was created
The new Notifier
The flow to implement
UserRepository

WP8 App

HttpClient

StackExchange

GetUser
HttpGetAsync
HTTP GET

User

JSON

JSON ...
Writing the “client”
UserRepository

WP8 App

HttpClient

StackExchange

GetUser
HttpGetAsync
HTTP GET

User

JSON

JSON (...
Our client

Get (user) data from StackOveflow
StackExchange API: Http-Get  JSON
Portable library
Write and build managed assemblies
targeted to one or more .NET framework platform
.NET
Framework

Xamari...
Testing portable library

Test as plain old .NET code
unit testing framework of choice

The new (and shiny) Test Explorer
17
This is a unit test
[Test]
public void AddTest()
{
var cut = new Calculator();
var result = cut.Add(2, 3);
Assert.AreEqual...
Testing in Isolation
UserRepository

WP8 App

HttpClient

StackExchange

GetUser
HttpGetAsync
HTTP GET

User

JSON

JSON (...
Testing in UserRepository in Isolation
UserRepository

Test Code

Fake HttpClient

GetUser
HttpGetAsync

User

JSON
What Mocking framework can do for you?
• Create Fake objects
• Set behavior on fake objects
• Verify method was called
• A...
Testing our application
UserRepository

WP8 App

HttpClient

StackExchange

GetUser
HttpGetAsync
HTTP GET

User

JSON

JSO...
MVVM to the rescue

Easy to create
Model

Easy to test
ViewModel

Binding
Commands

View
The “real” WP8 unit testing
MSTest
Using Emulator
Async Test methods supported
Test logic (Model/ViewModels)
Pitfalls and gotchas
Async test won’t run  return Task from test method

UnauthorizedAccessAxception  use [UITestMethod]
Summary
• Unit testing == good
• Unit testing portable code
as simple as “regular” code

• Unit testing WP8 logic is easy(...
Dror Helper
C: 972.50.7668543
e: drorh@oz-code.com
B: blog.drorhelper.com
w: www.ozcode.net
Upcoming SlideShare
Loading in …5
×

Test drive your windows phone application

3,514 views

Published on

Unit testing is a modern engineering practice that speed up development and give developers and testers a quick way to look for logic errors in the methods of apps they’ve written.
There is no excuse not to write unit tests for windows phone – since both the architecture and tools makes it feasible and simple. In this talk I’ll show examples and strategies of unit testing windows phone applications and how you can take control of your code today!

Published in: Technology, Education
  • Be the first to comment

Test drive your windows phone application

  1. 1. Test drive your windows phone application Dror Helper Senior consultant @dhelper | http://blog.drorhelper.com | drorh@oz-code.com
  2. 2. About.ME • Developing software (professionally) since 2002 • TDD/Unit testing evangelist • Not a windows phone 8 expert • Blogger: http://blog.drorhelper.com
  3. 3. Unit testing mobile applications is: Impossible or Too damn hard or Not cost effective
  4. 4. The truth is that unit testing mobile applications is easy and fully supported* by your IDE
  5. 5. A unit test is… Test individual unit of work For developers by developers
  6. 6. Pillars of good unit test 1. Tests specific functionality 2. Clear pass/fail criteria 3. Good unit test runs in isolation
  7. 7. This is a unit test [Test] public void CheckPassword_ValidUser_ReturnTrue() { bool result = CheckPassword(“Skroob”, “12345”); Assert.That(result, Is.True); }
  8. 8. Test classification • Unit tests should be: – Small – Atomic – Test a single functional unit – Isolated! • Integration tests - test several units together • Functional/System tests
  9. 9. Why write unit tests Saves time & money Don’t need to run full flow to test code Reduce manual testing effort Catch regression issues Documentation And especially
  10. 10. Before that - a History lesson In the distant past (2008) The ultimate tool was created
  11. 11. The new Notifier
  12. 12. The flow to implement UserRepository WP8 App HttpClient StackExchange GetUser HttpGetAsync HTTP GET User JSON JSON (Compressed)
  13. 13. Writing the “client” UserRepository WP8 App HttpClient StackExchange GetUser HttpGetAsync HTTP GET User JSON JSON (Compressed)
  14. 14. Our client Get (user) data from StackOveflow StackExchange API: Http-Get  JSON
  15. 15. Portable library Write and build managed assemblies targeted to one or more .NET framework platform .NET Framework Xamarin Silverlight PCL Windows Phone Xbox 360 WinRT
  16. 16. Testing portable library Test as plain old .NET code unit testing framework of choice The new (and shiny) Test Explorer
  17. 17. 17
  18. 18. This is a unit test [Test] public void AddTest() { var cut = new Calculator(); var result = cut.Add(2, 3); Assert.AreEqual(5, result); }
  19. 19. Testing in Isolation UserRepository WP8 App HttpClient StackExchange GetUser HttpGetAsync HTTP GET User JSON JSON (Compressed)
  20. 20. Testing in UserRepository in Isolation UserRepository Test Code Fake HttpClient GetUser HttpGetAsync User JSON
  21. 21. What Mocking framework can do for you? • Create Fake objects • Set behavior on fake objects • Verify method was called • And more...
  22. 22. Testing our application UserRepository WP8 App HttpClient StackExchange GetUser HttpGetAsync HTTP GET User JSON JSON (Compressed)
  23. 23. MVVM to the rescue Easy to create Model Easy to test ViewModel Binding Commands View
  24. 24. The “real” WP8 unit testing MSTest Using Emulator Async Test methods supported Test logic (Model/ViewModels)
  25. 25. Pitfalls and gotchas Async test won’t run  return Task from test method UnauthorizedAccessAxception  use [UITestMethod]
  26. 26. Summary • Unit testing == good • Unit testing portable code as simple as “regular” code • Unit testing WP8 logic is easy(ish)
  27. 27. Dror Helper C: 972.50.7668543 e: drorh@oz-code.com B: blog.drorhelper.com w: www.ozcode.net

×