test drive your windows phone application
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!TRANSCRIPT
![Page 1: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/1.jpg)
Dror HelperSenior consultant@dhelper | http://blog.drorhelper.com | [email protected]
Test drive your windows phone application
![Page 2: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/2.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/3.jpg)
3
Unit testing mobile applications is:
Impossible
or Too damn hard
orNot cost effective
![Page 4: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/4.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/5.jpg)
5
Test individual unit of work
For developers by developers
A unit test is…
![Page 6: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/6.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/7.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/8.jpg)
• 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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/10.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/11.jpg)
11
The new Notifier
![Page 12: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/12.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/13.jpg)
13
Writing the “client”
WP8 App UserRepository HttpClient StackExchange
GetUserHttpGetAsync
HTTP GET
JSON (Compressed)JSON
User
![Page 14: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/14.jpg)
14
Get (user) data from StackOveflow
StackExchange API: Http-Get JSON
Our client
![Page 15: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/15.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/16.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/17.jpg)
17
The problem with external services
![Page 18: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/19.jpg)
19
Testing in Isolation
WP8 App UserRepository HttpClient StackExchange
GetUserHttpGetAsync
HTTP GET
JSON (Compressed)JSON
User
![Page 20: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/20.jpg)
20
Testing in UserRepository in Isolation
Test Code UserRepository Fake HttpClient
GetUserHttpGetAsync
JSONUser
![Page 21: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/21.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/22.jpg)
22
Testing our application
WP8 App UserRepository HttpClient StackExchange
GetUserHttpGetAsync
HTTP GET
JSON (Compressed)JSON
User
![Page 23: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/23.jpg)
23
MVVM to the rescue
ViewModel ViewModelCommands
Binding
Easy to create Easy to test
![Page 24: Test drive your windows phone application](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/24.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/25.jpg)
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](https://reader033.vdocuments.net/reader033/viewer/2022052822/554f3ca2b4c905471e8b4ac5/html5/thumbnails/26.jpg)
26
• Unit testing == good
• Unit testing portable code as simple as “regular” code
• Unit testing WP8 logic is easy(ish)
Summary