Getting started            with Tests on           your WP7 AppFriday, June 29, 2012
Who am I?         • Developer freelance:               – C# Asp.net Mvc, Wpf, Wp7               – Python Django           ...
What we’re going to seeFriday, June 29, 2012
What we’re going to see         • What is a unit testFriday, June 29, 2012
What we’re going to see         • What is a unit test         • How to write itFriday, June 29, 2012
What we’re going to see         • What is a unit test         • How to write it         • Integration Test vs Unit TestFri...
What we’re going to see         • What is a unit test         • How to write it         • Integration Test vs Unit Test   ...
What we’re going to see         • What is a unit test         • How to write it         • Integration Test vs Unit Test   ...
What we’re going to see         • What is a unit test         • How to write it         • Integration Test vs Unit Test   ...
Let’s startFriday, June 29, 2012
From Wikipedia         • A unit test is a piece of a code (usually           a method) that invokes another piece         ...
Good test properties        • It should be automated and repeatable.        • It should be easy to implement.        • Onc...
Good test properties         • If your test doesn’t follow at least one           of the previous rules, there’s a high   ...
Integration Test         • Integration testing means testing two           or more dependent software modules           as...
First Test                                     9Friday, June 29, 2012
Unit vs Integration Test         • integration test: test many units of           code that work together to evaluate     ...
Classic wp7 approach        • Code coupled with UI               – Xaml + Code Behind -> one class                public p...
Classic wp7 approach              Your code is             NOT TESTABLE!                                           12Frida...
MVVM Approach         •Architectural Pattern         •Derived from Presentation Model          pattern (Fowler)         •C...
MVVM Approach         • Code structure:               – ViewModel (c#): Logic               – View (Xaml): Presentation   ...
Unit Test framework         • Nunit for Windows Phone 7         • No official mocking framework for           Windows Phon...
Test Runner         • NUnit         • Resharper         • Test Driven.net                                      16Friday, J...
Context         • Application that resize images         • Two boxes for the new size         • Ok button         • Error ...
Solution structure              WP7 App           View           ViewModel         WP7 Class Library             NUnit Lib...
MainViewModel         namespace OrangeCode.Resizer.ViewModel         {           public class MainViewModel           {   ...
First test         namespace OrangeCode.Resizer.Fixture         {           public class MainViewModelFixture           { ...
First test                                        [MethodName]_[StateUnderTest]_[ExpectedBehavior]         [Test]         ...
Run test                                   22Friday, June 29, 2012
Refactor our test             [TestFixture]             public class MainViewModelFixture             {                pri...
Refactor our test             [TestFixture]             public class MainViewModelFixture             {                  [...
Run test again                                         25Friday, June 29, 2012
Some more cases                  [TestCase(10,10)]                  [TestCase(12, 12)]                  [TestCase(0, 0)]  ...
Some more cases                                          27Friday, June 29, 2012
From red to green         namespace OrangeCode.Resizer.ViewModel         {           public class MainViewModel           ...
Run tests                                    29Friday, June 29, 2012
Test Driven Development         • We just made it!         • Life Cycle:            – Write test (red)            – Write ...
Back to the first test                  [TestCase(10,10)]                  [TestCase(12, 12)]                  [TestCase(0...
Good test properties         • It should be automated and repeatable.         • It should be easy to implement.         • ...
What to test in WP7         • Properties with logic (not only get/set)         • Command         • Navigation between page...
Final thoughts         • Use small consistent test               – One test can test only one feature         • If work wi...
Recap         • What is a unit test         • How to write it         • Integration Test vs Unit Test         • Testing to...
Be in contact             Mail: michele@orangecode.it             Twitter: @piccoloaiutante             Web: www.orangecod...
Grazie a                        DotNET Lombardia!                                            37Friday, June 29, 2012
Upcoming SlideShare
Loading in...5
×

Getting started with Windows Phone 7 and unit test

1,326

Published on

An introduction to unit testing in Windows Phone 7

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,326
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Getting started with Windows Phone 7 and unit test

  1. 1. Getting started with Tests on your WP7 AppFriday, June 29, 2012
  2. 2. Who am I? • Developer freelance: – C# Asp.net Mvc, Wpf, Wp7 – Python Django – Blog: orangecode.it/blogFriday, June 29, 2012
  3. 3. What we’re going to seeFriday, June 29, 2012
  4. 4. What we’re going to see • What is a unit testFriday, June 29, 2012
  5. 5. What we’re going to see • What is a unit test • How to write itFriday, June 29, 2012
  6. 6. What we’re going to see • What is a unit test • How to write it • Integration Test vs Unit TestFriday, June 29, 2012
  7. 7. What we’re going to see • What is a unit test • How to write it • Integration Test vs Unit Test • Testing tools for WP7Friday, June 29, 2012
  8. 8. 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 testFriday, June 29, 2012
  9. 9. 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 suiteFriday, June 29, 2012
  10. 10. Let’s startFriday, June 29, 2012
  11. 11. 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
  12. 12. 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 OsheroveFriday, June 29, 2012
  13. 13. 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
  14. 14. Integration Test • Integration testing means testing two or more dependent software modules as a group.Friday, June 29, 2012
  15. 15. First Test 9Friday, June 29, 2012
  16. 16. 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. 10Friday, June 29, 2012
  17. 17. Classic wp7 approach • Code coupled with UI – Xaml + Code Behind -> one class public partial class MainView : PhoneApplicationPage { public MainView() { InitializeComponent(); } } 11Friday, June 29, 2012
  18. 18. Classic wp7 approach Your code is NOT TESTABLE! 12Friday, June 29, 2012
  19. 19. MVVM Approach •Architectural Pattern •Derived from Presentation Model pattern (Fowler) •Clear separation between UI and Logic UI ViewModel Collections, DelegateCommand, Properties 13Friday, June 29, 2012
  20. 20. MVVM Approach • Code structure: – ViewModel (c#): Logic – View (Xaml): Presentation – No more code behind • Now the ViewModel (as a plain c# class) IS TESTABLE 14Friday, June 29, 2012
  21. 21. 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! 15Friday, June 29, 2012
  22. 22. Test Runner • NUnit • Resharper • Test Driven.net 16Friday, June 29, 2012
  23. 23. Context • Application that resize images • Two boxes for the new size • Ok button • Error if the image is not in 4:3 17Friday, June 29, 2012
  24. 24. Solution structure WP7 App View ViewModel WP7 Class Library NUnit Library (via NuGet) TestSuite 18Friday, June 29, 2012
  25. 25. MainViewModel 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; } } } } 19Friday, June 29, 2012
  26. 26. First test 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); } } } 20Friday, June 29, 2012
  27. 27. First test [MethodName]_[StateUnderTest]_[ExpectedBehavior] [Test] public void RatioCheck_InvalidSize_ReturnFalse() { var viewModel= new MainViewModel(); viewModel.Height = 100; Arrange objects. viewModel.Width = 80; Act on an objects. bool result = viewModel.RatioCheck; Assert something expected. Assert.IsFalse(result); } 21Friday, June 29, 2012
  28. 28. Run test 22Friday, June 29, 2012
  29. 29. Refactor our test [TestFixture] public class MainViewModelFixture { private MainViewModel viewModel; [SetUp] executed before every test public void SetUp() { viewModel = new MainViewModel(); } [Test] public void RatioCheck_InvalidSize_ReturnFalse() { viewModel.Height = 100; viewModel.Width = 80; Assert.IsFalse(viewModel.RatioCheck); } } 23Friday, June 29, 2012
  30. 30. Refactor our test [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 } } 24Friday, June 29, 2012
  31. 31. Run test again 25Friday, June 29, 2012
  32. 32. Some more cases [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); } 26Friday, June 29, 2012
  33. 33. Some more cases 27Friday, June 29, 2012
  34. 34. From red to green 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; } } } } 28Friday, June 29, 2012
  35. 35. Run tests 29Friday, June 29, 2012
  36. 36. Test Driven Development • We just made it! • Life Cycle: – Write test (red) – Write logic to pass the test (green) – Refactor code (refactor) – Again.. 30Friday, June 29, 2012
  37. 37. Back to the first test [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); } 31Friday, June 29, 2012
  38. 38. 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. 32Friday, June 29, 2012
  39. 39. What to test in WP7 • Properties with logic (not only get/set) • Command • Navigation between pages • Interaction with storage • Converter • Event 33Friday, June 29, 2012
  40. 40. 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 34Friday, June 29, 2012
  41. 41. Recap • What is a unit test • How to write it • Integration Test vs Unit Test • Testing tools • The first test • Maintaining test suite 35Friday, June 29, 2012
  42. 42. Be in contact Mail: michele@orangecode.it Twitter: @piccoloaiutante Web: www.orangecode.it Blog: www.orangecode.it/blog GitHub: https://github.com/piccoloaiutante Community: WEBdeBS 36Friday, June 29, 2012
  43. 43. Grazie a DotNET Lombardia! 37Friday, June 29, 2012
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×