getting started with windows phone 7 and unit test
DESCRIPTION
An introduction to unit testing in Windows Phone 7TRANSCRIPT
![Page 1: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/1.jpg)
Getting started with Tests on your WP7 App
Friday, June 29, 2012
![Page 2: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/2.jpg)
Who am I?
• Developer freelance:–C# Asp.net Mvc, Wpf, Wp7–Python Django–Blog: orangecode.it/blog
Friday, June 29, 2012
![Page 3: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/3.jpg)
What we’re going to see
Friday, June 29, 2012
![Page 4: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/4.jpg)
What we’re going to see
•What is a unit test
Friday, June 29, 2012
![Page 5: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/5.jpg)
What we’re going to see
•What is a unit test• How to write it
Friday, June 29, 2012
![Page 6: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/6.jpg)
What we’re going to see
•What is a unit test• How to write it• Integration Test vs Unit Test
Friday, June 29, 2012
![Page 7: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/7.jpg)
What we’re going to see
•What is a unit test• How to write it• Integration Test vs Unit Test• Testing tools for WP7
Friday, June 29, 2012
![Page 8: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/8.jpg)
What we’re going to see
•What is a unit test• How to write it• Integration Test vs Unit Test• Testing tools for WP7• The first test
Friday, June 29, 2012
![Page 9: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/9.jpg)
What we’re going to see
•What is a unit test• How to write it• Integration Test vs Unit Test• Testing tools for WP7• The first test• Maintaining test suite
Friday, June 29, 2012
![Page 10: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/10.jpg)
Let’s start
Friday, June 29, 2012
![Page 11: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/11.jpg)
From Wikipedia
• A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness of some assumptions afterward. • If the assumptions turn out to be wrong, the unit test has failed. • A “unit” is a method or function.
Friday, June 29, 2012
![Page 12: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/12.jpg)
Good test properties• It should be automated and repeatable. • It should be easy to implement. • Once it’s written, it should remain for future use. • Anyone should be able to run it. • It should run at the push of a button. • It should run quickly.
Roy Osherove
Friday, June 29, 2012
![Page 13: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/13.jpg)
Good test properties
• If your test doesn’t follow at least one of the previous rules, there’s a high probability that what you’re implementing isn’t a unit test.
• You’ve done integration testing.
Friday, June 29, 2012
![Page 14: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/14.jpg)
Integration Test
• Integration testing means testing two or more dependent software modules as a group.
Friday, June 29, 2012
![Page 15: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/15.jpg)
First Test
9
Friday, June 29, 2012
![Page 16: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/16.jpg)
Unit vs Integration Test
• integration test: test many units of code that work together to evaluate one or more expected results from the software.
• unit test: test only a single unit in isolation.
10
Friday, June 29, 2012
![Page 17: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/17.jpg)
Classic wp7 approach
• Code coupled with UI– Xaml + Code Behind -> one class
public partial class MainView : PhoneApplicationPage { public MainView() { InitializeComponent(); } }
11
Friday, June 29, 2012
![Page 18: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/18.jpg)
Classic wp7 approach
Your code is NOT TESTABLE!
12
Friday, June 29, 2012
![Page 19: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/19.jpg)
•Architectural Pattern•Derived from Presentation Model pattern (Fowler)
•Clear separation between UI and Logic
MVVM Approach
13
UI
Collections, DelegateCommand, Properties
ViewModel
Friday, June 29, 2012
![Page 20: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/20.jpg)
MVVM Approach• Code structure:
–ViewModel (c#): Logic–View (Xaml): Presentation–No more code behind
• Now the ViewModel (as a plain c# class) IS TESTABLE
14
Friday, June 29, 2012
![Page 21: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/21.jpg)
Unit Test framework
• Nunit for Windows Phone 7
• No official mocking framework for Windows Phone 7, but I found out that Moq 3.1 for silverlight works!
15
Friday, June 29, 2012
![Page 22: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/22.jpg)
Test Runner
• NUnit
• Resharper
• Test Driven.net
16
Friday, June 29, 2012
![Page 23: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/23.jpg)
Context
• Application that resize images• Two boxes for the new size• Ok button
• Error if the image is not in 4:3
17
Friday, June 29, 2012
![Page 24: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/24.jpg)
Solution structure
18
WP7 App
ViewModel
WP7 Class Library
NUnit Library (via NuGet)
TestSuite
View
Friday, June 29, 2012
![Page 25: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/25.jpg)
MainViewModel
19
namespace OrangeCode.Resizer.ViewModel{ public class MainViewModel { public int Height { get; set; }
public int Width { get; set; }
public bool RatioCheck { get { return Width*3 == Height*4; } } }}
Friday, June 29, 2012
![Page 26: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/26.jpg)
First test
20
namespace OrangeCode.Resizer.Fixture{ public class MainViewModelFixture { [Test] public void RatioCheck_InvalidSize_ReturnFalse() {
var viewModel= new MainViewModel();
viewModel.Height = 100; viewModel.Width = 80;
bool result= viewModel.RatioCheck;
Assert.IsFalse(result); } }}
Friday, June 29, 2012
![Page 27: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/27.jpg)
First test
21
[Test]public void RatioCheck_InvalidSize_ReturnFalse(){
var viewModel= new MainViewModel();
viewModel.Height = 100;viewModel.Width = 80;
bool result = viewModel.RatioCheck;
Assert.IsFalse(result);}
[MethodName]_[StateUnderTest]_[ExpectedBehavior]
Arrange objects.
Act on an objects.
Assert something expected.
Friday, June 29, 2012
![Page 28: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/28.jpg)
Run test
22
Friday, June 29, 2012
![Page 29: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/29.jpg)
Refactor our test
23
[TestFixture] public class MainViewModelFixture { private MainViewModel viewModel;
[SetUp] public void SetUp() { viewModel = new MainViewModel(); }
[Test] public void RatioCheck_InvalidSize_ReturnFalse() { viewModel.Height = 100; viewModel.Width = 80;
Assert.IsFalse(viewModel.RatioCheck); } }
executed before every test
Friday, June 29, 2012
![Page 30: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/30.jpg)
Refactor our test
24
[TestFixture] public class MainViewModelFixture { [Test] public void RatioCheck_InvalidSize_ReturnFalse() { viewModel.Height = 100; viewModel.Width = 80;
Assert.IsFalse(viewModel.RatioCheck); }
[TearDown]public void TearDown(){
viewModel=null;}
}
executed after every test
Friday, June 29, 2012
![Page 31: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/31.jpg)
Run test again
25
Friday, June 29, 2012
![Page 32: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/32.jpg)
Some more cases
26
[TestCase(10,10)] [TestCase(12, 12)] [TestCase(0, 0)] public void RatioCheck_InvalidSize_ReturnFalse(int height,int width) { viewModel.Height = height; viewModel.Width = width;
Assert.IsFalse(viewModel.RatioCheck); }
Friday, June 29, 2012
![Page 33: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/33.jpg)
Some more cases
27
Friday, June 29, 2012
![Page 34: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/34.jpg)
From red to green
28
namespace OrangeCode.Resizer.ViewModel{ public class MainViewModel { public int Height { get; set; }
public int Width { get; set; }
public bool RatioCheck { get {
if(Width>0 && Height>0)return Width*3 == Height*4;
return false; }
} }}
Friday, June 29, 2012
![Page 35: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/35.jpg)
Run tests
29
Friday, June 29, 2012
![Page 36: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/36.jpg)
Test Driven Development
• We just made it!
• Life Cycle:–Write test (red) –Write logic to pass the test (green)–Refactor code (refactor)–Again..
30
Friday, June 29, 2012
![Page 37: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/37.jpg)
Back to the first test
31
[TestCase(10,10)] [TestCase(12, 12)] [TestCase(0, 0)] public void RatioCheck_InvalidSize_ReturnFalse(int height,int width) { viewModel.Height = height; viewModel.Width = width;
Assert.IsFalse(viewModel.RatioCheck); }
Friday, June 29, 2012
![Page 38: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/38.jpg)
Good test properties• It should be automated and repeatable. • It should be easy to implement. • Once it’s written, it should remain for future
use. • Anyone should be able to run it. • It should run at the push of a button. • It should run quickly.
32
Friday, June 29, 2012
![Page 39: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/39.jpg)
What to test in WP7
• Properties with logic (not only get/set)• Command• Navigation between pages• Interaction with storage• Converter• Event
33
Friday, June 29, 2012
![Page 40: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/40.jpg)
Final thoughts
• Use small consistent test –One test can test only one feature
• If work with legacy code–create an integration test for every feature–split a integration test in few unit test
34
Friday, June 29, 2012
![Page 41: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/41.jpg)
Recap
•What is a unit test• How to write it• Integration Test vs Unit Test • Testing tools• The first test• Maintaining test suite
35
Friday, June 29, 2012
![Page 42: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/42.jpg)
Be in contactMail: [email protected]: @piccoloaiutanteWeb: www.orangecode.itBlog: www.orangecode.it/blogGitHub: https://github.com/piccoloaiutante
Community: WEBdeBS
36
Friday, June 29, 2012
![Page 43: Getting started with Windows Phone 7 and unit test](https://reader033.vdocuments.us/reader033/viewer/2022060111/5565ac72d8b42a4c6f8b49e2/html5/thumbnails/43.jpg)
Grazie aDotNET Lombardia!
37
Friday, June 29, 2012