27
Dror Helper Senior consultant @dhelper | http://blog.drorhelper.com | [email protected] Test drive your windows phone application

Test drive your windows phone application

Embed Size (px)

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

Page 1: Test drive your windows phone application

Dror HelperSenior consultant@dhelper | http://blog.drorhelper.com | [email protected]

Test drive your windows phone application

Page 2: 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

Page 3: Test drive your windows phone application

3

Unit testing mobile applications is:

Impossible

or Too damn hard

orNot cost effective

Page 4: Test drive your windows phone application

4

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

Page 5: Test drive your windows phone application

5

Test individual unit of work

For developers by developers

A unit test is…

Page 6: Test drive your windows phone application

6

1. Tests specific functionality

2. Clear pass/fail criteria

3. Good unit test runs in isolation

Pillars of good unit test

Page 7: Test drive your windows phone application

7

[Test]public void CheckPassword_ValidUser_ReturnTrue()

{bool result = CheckPassword(“Skroob”,

“12345”);

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

This is a unit test

Page 8: Test drive your windows phone application

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

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

Test classification

Page 9: Test drive your windows phone application

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

Page 10: Test drive your windows phone application

In the distant past (2008)

The ultimate tool was created

10

Before that - a History lesson

Notifier

Page 11: Test drive your windows phone application

11

The new Notifier

Page 12: Test drive your windows phone application

12

The flow to implement

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

Page 13: Test drive your windows phone application

13

Writing the “client”

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

Page 14: Test drive your windows phone application

14

Get (user) data from StackOveflow

StackExchange API: Http-Get JSON

Our client

Page 15: Test drive your windows phone application

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

Page 16: Test drive your windows phone application

16

Test as plain old .NET code

unit testing framework of choice

The new (and shiny) Test Explorer

Testing portable library

Page 17: Test drive your windows phone application

17

The problem with external services

Page 18: Test drive your windows phone application

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

Page 19: Test drive your windows phone application

19

Testing in Isolation

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

Page 20: Test drive your windows phone application

20

Testing in UserRepository in Isolation

Test Code UserRepository Fake HttpClient

GetUserHttpGetAsync

JSONUser

Page 21: Test drive your windows phone application

21

• Create Fake objects

• Set behavior on fake objects

• Verify method was called

• And more...

What Mocking framework can do for you?

Page 22: Test drive your windows phone application

22

Testing our application

WP8 App UserRepository HttpClient StackExchange

GetUserHttpGetAsync

HTTP GET

JSON (Compressed)JSON

User

Page 23: Test drive your windows phone application

23

MVVM to the rescue

ViewModel ViewModelCommands

Binding

Easy to create Easy to test

Page 24: Test drive your windows phone application

24

MSTest

Using Emulator

Async Test methods supported

Test logic (Model/ViewModels)

The “real” WP8 unit testing

Page 25: Test drive your windows phone application

25

Async test won’t run return Task from test method

UnauthorizedAccessAxception use [UITestMethod]

Pitfalls and gotchas

Page 26: Test drive your windows phone application

26

• Unit testing == good

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

• Unit testing WP8 logic is easy(ish)

Summary

Page 27: Test drive your windows phone application

27

Dror HelperC: 972.50.7668543e: [email protected]: blog.drorhelper.comw: www.ozcode.net