Test drive your windows phone application

Preview:

DESCRIPTION

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!

Citation preview

Dror HelperSenior consultant@dhelper | http://blog.drorhelper.com | drorh@oz-code.com

Test drive your windows phone application

2

• Developing software (professionally) since 2002

• TDD/Unit testing evangelist

• Not a windows phone 8 expert

• Blogger: http://blog.drorhelper.com

About.ME

3

Unit testing mobile applications is:

Impossible

or Too damn hard

orNot cost effective

4

The truth is that unit testing mobile applications is easy and fully supported* by your IDE

5

Test individual unit of work

For developers by developers

A unit test is…

6

1. Tests specific functionality

2. Clear pass/fail criteria

3. Good unit test runs in isolation

Pillars of good unit test

7

[Test]public void CheckPassword_ValidUser_ReturnTrue()

{bool result = CheckPassword(“Skroob”,

“12345”);

Assert.That(result, Is.True);}

This is a unit test

• Unit tests should be:– Small– Atomic– Test a single functional unit– Isolated!

• Integration tests - test several units together• Functional/System tests

Test classification

9

Saves time & moneyDon’t need to run full flow to test codeReduce manual testing effort

Catch regression issuesDocumentation

And especially

Why write unit tests

In the distant past (2008)

The ultimate tool was created

10

Before that - a History lesson

Notifier

11

The new Notifier

12

The flow to implement

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

13

Writing the “client”

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

14

Get (user) data from StackOveflow

StackExchange API: Http-Get JSON

Our client

15

Write and build managed assemblies targeted to one or more .NET framework platform

Portable library

PCL

.NET Framework

Silverlight

Windows PhoneWinRT

Xbox 360

Xamarin

16

Test as plain old .NET code

unit testing framework of choice

The new (and shiny) Test Explorer

Testing portable library

17

The problem with external services

18

a

[Test] public void AddTest() {

var cut = new Calculator(); var result = cut.Add(2, 3);

Assert.AreEqual(5, result); }

This is unit testnot a

real

19

Testing in Isolation

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

20

Testing in UserRepository in Isolation

Test Code UserRepository Fake HttpClient

GetUserHttpGetAsync

JSONUser

21

• Create Fake objects

• Set behavior on fake objects

• Verify method was called

• And more...

What Mocking framework can do for you?

22

Testing our application

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

23

MVVM to the rescue

ViewModel ViewModelCommands

Binding

Easy to create Easy to test

24

MSTest

Using Emulator

Async Test methods supported

Test logic (Model/ViewModels)

The “real” WP8 unit testing

25

Async test won’t run return Task from test method

UnauthorizedAccessAxception use [UITestMethod]

Pitfalls and gotchas

26

• Unit testing == good

• Unit testing portable code as simple as “regular” code

• Unit testing WP8 logic is easy(ish)

Summary

27

Dror HelperC: 972.50.7668543e: drorh@oz-code.comB: blog.drorhelper.comw: www.ozcode.net

Recommended