SlideShare a Scribd company logo
1 of 43
Download to read offline
Getting started
            with Tests on
           your WP7 App




Friday, June 29, 2012
Who am I?
         • Developer freelance:
               – C# Asp.net Mvc, Wpf, Wp7
               – Python Django
               – Blog: orangecode.it/blog




Friday, June 29, 2012
What we’re going to see




Friday, June 29, 2012
What we’re going to see
         • What is a unit test




Friday, June 29, 2012
What we’re going to see
         • What is a unit test
         • How to write it




Friday, June 29, 2012
What we’re going to see
         • What is a unit test
         • How to write it
         • Integration Test vs Unit Test




Friday, June 29, 2012
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
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
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
Let’s start




Friday, June 29, 2012
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
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
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
Integration Test
         • Integration testing means testing two
           or more dependent software modules
           as a group.




Friday, June 29, 2012
First Test




                                     9

Friday, June 29, 2012
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
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
Classic wp7 approach



              Your code is
             NOT TESTABLE!




                                           12

Friday, June 29, 2012
MVVM Approach
         •Architectural Pattern
         •Derived from Presentation Model
          pattern (Fowler)
         •Clear separation between UI and Logic


                        UI                                               ViewModel

                              Collections, DelegateCommand, Properties




                                                                                     13

Friday, June 29, 2012
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
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
Test Runner
         • NUnit

         • Resharper

         • Test Driven.net




                                      16

Friday, June 29, 2012
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
Solution structure
              WP7 App

           View
           ViewModel

         WP7 Class Library

             NUnit Library (via NuGet)

             TestSuite



                                              18

Friday, June 29, 2012
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; }

                    }
              }
         }



                                                            19

Friday, June 29, 2012
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);
                  }
             }
         }




                                                                     20

Friday, June 29, 2012
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);
         }




                                                                                            21

Friday, June 29, 2012
Run test




                                   22

Friday, June 29, 2012
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);
                  }
             }



                                                                                             23

Friday, June 29, 2012
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
                  }
             }




                                                                                             24

Friday, June 29, 2012
Run test again




                                         25

Friday, June 29, 2012
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);
                  }




                                                                                         26

Friday, June 29, 2012
Some more cases




                                          27

Friday, June 29, 2012
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;
                      }
                    }
              }
         }

                                                         28

Friday, June 29, 2012
Run tests




                                    29

Friday, June 29, 2012
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
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);
                  }




                                                                                         31

Friday, June 29, 2012
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
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
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
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
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




                                                     36

Friday, June 29, 2012
Grazie a
                        DotNET Lombardia!




                                            37

Friday, June 29, 2012

More Related Content

Similar to Getting started with Windows Phone 7 and unit test

Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...TEST Huddle
 
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...TEST Huddle
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeAleksandar Bozinovski
 
Introduction to test automation in java and php
Introduction to test automation in java and phpIntroduction to test automation in java and php
Introduction to test automation in java and phpTho Q Luong Luong
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Jacinto Limjap
 
7 stages of unit testing
7 stages of unit testing7 stages of unit testing
7 stages of unit testingJorge Ortiz
 
Framework for Web Automation Testing
Framework for Web Automation TestingFramework for Web Automation Testing
Framework for Web Automation TestingTaras Lytvyn
 
Design for Testability
Design for Testability Design for Testability
Design for Testability Pawel Kalbrun
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkPeter Kofler
 
Anatomy of Test Driven Development
Anatomy of Test Driven DevelopmentAnatomy of Test Driven Development
Anatomy of Test Driven DevelopmentDhaval Shah
 
Tdd in android (mvp)
Tdd in android (mvp)Tdd in android (mvp)
Tdd in android (mvp)Prateek Jain
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsQuontra Solutions
 

Similar to Getting started with Windows Phone 7 and unit test (20)

Functional testing patterns
Functional testing patternsFunctional testing patterns
Functional testing patterns
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
Olli-Pekka Puolitaival - Model-Based Testing for Integration Testing in Real ...
 
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
Olli-Pekka Puolitaival - Model-Based Tested for Integration Tested in Real Pr...
 
Microsoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable CodeMicrosoft Fakes, Unit Testing the (almost) Untestable Code
Microsoft Fakes, Unit Testing the (almost) Untestable Code
 
Unit Tests with Microsoft Fakes
Unit Tests with Microsoft FakesUnit Tests with Microsoft Fakes
Unit Tests with Microsoft Fakes
 
Introduction to test automation in java and php
Introduction to test automation in java and phpIntroduction to test automation in java and php
Introduction to test automation in java and php
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 
7 stages of unit testing
7 stages of unit testing7 stages of unit testing
7 stages of unit testing
 
Seminario Federico Caboni, 25-10-2012
Seminario Federico Caboni, 25-10-2012Seminario Federico Caboni, 25-10-2012
Seminario Federico Caboni, 25-10-2012
 
Framework for Web Automation Testing
Framework for Web Automation TestingFramework for Web Automation Testing
Framework for Web Automation Testing
 
Unit testing in Unity
Unit testing in UnityUnit testing in Unity
Unit testing in Unity
 
Design for Testability
Design for Testability Design for Testability
Design for Testability
 
Spring MVC
Spring MVCSpring MVC
Spring MVC
 
TDD talk
TDD talkTDD talk
TDD talk
 
Munit_in_mule_naveen
Munit_in_mule_naveenMunit_in_mule_naveen
Munit_in_mule_naveen
 
Writing Tests with the Unity Test Framework
Writing Tests with the Unity Test FrameworkWriting Tests with the Unity Test Framework
Writing Tests with the Unity Test Framework
 
Anatomy of Test Driven Development
Anatomy of Test Driven DevelopmentAnatomy of Test Driven Development
Anatomy of Test Driven Development
 
Tdd in android (mvp)
Tdd in android (mvp)Tdd in android (mvp)
Tdd in android (mvp)
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
 

More from Michele Capra

Nodeschool italy at codemotion
Nodeschool italy at codemotionNodeschool italy at codemotion
Nodeschool italy at codemotionMichele Capra
 
Little bits & node.js IOT for beginner
Little bits & node.js IOT for beginnerLittle bits & node.js IOT for beginner
Little bits & node.js IOT for beginnerMichele Capra
 
Testing Windows Phone 8.1 app with unit test and Coded UI test
Testing Windows Phone 8.1 app with unit test and Coded UI testTesting Windows Phone 8.1 app with unit test and Coded UI test
Testing Windows Phone 8.1 app with unit test and Coded UI testMichele Capra
 
Porting business apps to Windows Phone
Porting business apps to Windows PhonePorting business apps to Windows Phone
Porting business apps to Windows PhoneMichele Capra
 
The magic of Dynamic in Nancy Fx
The magic of Dynamic in Nancy FxThe magic of Dynamic in Nancy Fx
The magic of Dynamic in Nancy FxMichele Capra
 
Developing application for Windows Phone 7 in TDD
Developing application for Windows Phone 7 in TDDDeveloping application for Windows Phone 7 in TDD
Developing application for Windows Phone 7 in TDDMichele Capra
 
Windows Phone 7 Development
Windows Phone 7 DevelopmentWindows Phone 7 Development
Windows Phone 7 DevelopmentMichele Capra
 
My Final Dissertation
My Final DissertationMy Final Dissertation
My Final DissertationMichele Capra
 

More from Michele Capra (8)

Nodeschool italy at codemotion
Nodeschool italy at codemotionNodeschool italy at codemotion
Nodeschool italy at codemotion
 
Little bits & node.js IOT for beginner
Little bits & node.js IOT for beginnerLittle bits & node.js IOT for beginner
Little bits & node.js IOT for beginner
 
Testing Windows Phone 8.1 app with unit test and Coded UI test
Testing Windows Phone 8.1 app with unit test and Coded UI testTesting Windows Phone 8.1 app with unit test and Coded UI test
Testing Windows Phone 8.1 app with unit test and Coded UI test
 
Porting business apps to Windows Phone
Porting business apps to Windows PhonePorting business apps to Windows Phone
Porting business apps to Windows Phone
 
The magic of Dynamic in Nancy Fx
The magic of Dynamic in Nancy FxThe magic of Dynamic in Nancy Fx
The magic of Dynamic in Nancy Fx
 
Developing application for Windows Phone 7 in TDD
Developing application for Windows Phone 7 in TDDDeveloping application for Windows Phone 7 in TDD
Developing application for Windows Phone 7 in TDD
 
Windows Phone 7 Development
Windows Phone 7 DevelopmentWindows Phone 7 Development
Windows Phone 7 Development
 
My Final Dissertation
My Final DissertationMy Final Dissertation
My Final Dissertation
 

Recently uploaded

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Recently uploaded (20)

A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Getting started with Windows Phone 7 and unit test

  • 1. Getting started with Tests on your WP7 App Friday, June 29, 2012
  • 2. Who am I? • Developer freelance: – C# Asp.net Mvc, Wpf, Wp7 – Python Django – Blog: orangecode.it/blog Friday, June 29, 2012
  • 3. What we’re going to see Friday, June 29, 2012
  • 4. What we’re going to see • What is a unit test Friday, June 29, 2012
  • 5. What we’re going to see • What is a unit test • How to write it Friday, June 29, 2012
  • 6. What we’re going to see • What is a unit test • How to write it • Integration Test vs Unit Test Friday, June 29, 2012
  • 7. 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
  • 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 test Friday, June 29, 2012
  • 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 suite Friday, June 29, 2012
  • 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. 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
  • 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. Integration Test • Integration testing means testing two or more dependent software modules as a group. Friday, June 29, 2012
  • 15. First Test 9 Friday, June 29, 2012
  • 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. 10 Friday, June 29, 2012
  • 17. 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
  • 18. Classic wp7 approach Your code is NOT TESTABLE! 12 Friday, June 29, 2012
  • 19. MVVM Approach •Architectural Pattern •Derived from Presentation Model pattern (Fowler) •Clear separation between UI and Logic UI ViewModel Collections, DelegateCommand, Properties 13 Friday, June 29, 2012
  • 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 14 Friday, June 29, 2012
  • 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! 15 Friday, June 29, 2012
  • 22. Test Runner • NUnit • Resharper • Test Driven.net 16 Friday, June 29, 2012
  • 23. 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
  • 24. Solution structure WP7 App View ViewModel WP7 Class Library NUnit Library (via NuGet) TestSuite 18 Friday, June 29, 2012
  • 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; } } } } 19 Friday, June 29, 2012
  • 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); } } } 20 Friday, June 29, 2012
  • 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); } 21 Friday, June 29, 2012
  • 28. Run test 22 Friday, June 29, 2012
  • 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); } } 23 Friday, June 29, 2012
  • 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 } } 24 Friday, June 29, 2012
  • 31. Run test again 25 Friday, June 29, 2012
  • 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); } 26 Friday, June 29, 2012
  • 33. Some more cases 27 Friday, June 29, 2012
  • 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; } } } } 28 Friday, June 29, 2012
  • 35. Run tests 29 Friday, June 29, 2012
  • 36. 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
  • 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); } 31 Friday, June 29, 2012
  • 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. 32 Friday, June 29, 2012
  • 39. 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
  • 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 34 Friday, June 29, 2012
  • 41. 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
  • 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 36 Friday, June 29, 2012
  • 43. Grazie a DotNET Lombardia! 37 Friday, June 29, 2012