SlideShare a Scribd company logo
Introduction to
Test First Development
               @GomesNayagam
Introduction to Developer Testing


                                    Unit Testing Frameworks



Test First Development
Benefits

 Higher quality

 Fewer defects

 Living documentation

 Well Crafted code

 Automatic regression harness
A unit test confirms functionality
of a small unit of functionality or
            component
        in a larger system.
Unit Test Frameworks
How Unit Test Frameworks Work



                               Unit Test Runner
                               EXE




                                             Unit Test
My Code         My Unit Test Lib             Common Lib
Test First Development
TFD        Details

A Jig is a form upon which
something else is built

The jig is the specification for
the thing being built

Remove the jig when ready to
Launch




For software, Jig is built just a moment before it is being used
Create a failing test   Red

                    Make the test pass      Green


           RED      Refactor     Improve the internal
                                 implementation without
                                 changing the external
                                 contract or behavior




Refactor         Green
The Tests are the Specs
TFD: Writing Unit Tests
Test              Integration vs.
Organization      Unit Tests



Testing the Sad   Unit Test
Path              Lifecycle
Unit Test or Integration Test
Test Organization - xUnit
Test Lifecycle nUnit


                       TestFixtureSetup


                       SetUp


                       Test


                       TearDown


                       TestFixtureTearDown
Setting Up A Test Project
Tests live in a separate class library project
A First Test


Using Nunit.Framework

Namespace Domain.Tests
{
       [TestFixture]
       public class FirstTestFixture
       {
                 [Test]
                 public void AFirstTest()
                 {
                             Assert.IsTrue(true,”true is true!”);
                 }
       }
}
TFD - Assertions
Use one logical assertion per test

                                     [Test]
                                     public void the_order_is_canceled()
                                     {
                                               var customer = CreateCustomer();
                                               Assert.IsNotNull(customer);

                                              customer.PlaceOrder();
                                              Assert.IsTrue(customer.HasOrder);

                                              customer.CancelOrder();
                                              Assert.IsFalse(customer.HasOrder);
                                     }
Isolating Code
Isolation Techniques




Test Doubles




Isolation by Example
Isolation Techniques

                         Test Method



              SUT




            Dependency                 Test Object
              Object
Faking out the SUT
                                       Test Method

1.Create test specific objects

2.Create the SUT (using Interface)

3. Invoke operation on the SUT

4.Check results of SUT invocation On the SUT
Testing Double
Dummy
        var person = new Person();

        person.First = “Homer”;

        person.Last = “Simpson”;

        Assert.IsNotNull(person.FullName);


                                     var order = new Order();

                                     order.AddLineItem(12, 1);

                                     order.AddLineItem.Add(21, 3);

                                     Assert.AreEqual(2, order.NumLineItems);
Stub

       public class StubRepo : IOwnerRepository {

              public IOwner FindById(int id){}

              public IOwner Save(IOwner owner) {
                      return new Owner();
              }
              public void Delete(IOwner owner){}
       }
Fake
Mock


 TypeMock

 RhinoMock

 Moq
e.g.

using System;
using System.Collections.Generic;

 namespace MoqSamples.Models
{
    public interface IProductRepository
     {
        List<IProduct> Select();
        IProduct Get(int id);
     }
    public interface IProduct
    {
       int Id {get; set;}
       string Name { get; set; }
    }
}
 // Mock a product
var newProduct = new Mock<IProduct>();
newProduct.ExpectGet(p => p.Id).Returns(1);
newProduct.ExpectGet(p => p.Name).Returns("Bushmills");

Assert.AreEqual("Bushmills", newProduct.Object.Name);
e.g.
// Mock product repository
var productRepository = new Mock<IProductRepository>();
productRepository.Expect(p => p.Get(1)).Returns(newProduct.Object)

// Act
var productReturned = productRepository.Object.Get(1);
// Assert
Assert.AreEqual("Bushmills", productReturned.Name);

// Mock product repository
var productRepository = new Mock<IProductRepository>();
productRepository
.Expect(p => p.Get(It.IsAny<int>()))
.Returns(newProduct.Object);
Summery…

                   Stick with red
                   -
Write tests in a   green
separate           -
project            refactor



                   Keep
Treat test code    practicing and
with respect       learning
Reference


Test Driven Development by Example , Kent Beck, 2002

http://code.google.com/p/moq/wiki/QuickStart

http://stephenwalther.com/blog/archive/2008/06/12/tdd-
introduction-to-moq.aspx



Source: Pluralsight,Google,WIKI

More Related Content

What's hot

Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Deepak Singhvi
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Kiki Ahmadi
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesNarendra Pathai
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Thomas Weller
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with PythonMicroPyramid .
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnitinTwentyEight Minutes
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit testEugenio Lentini
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 

What's hot (20)

Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.Testing and Mocking Object - The Art of Mocking.
Testing and Mocking Object - The Art of Mocking.
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
Junit
JunitJunit
Junit
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1Simple Unit Testing With Netbeans 6.1
Simple Unit Testing With Netbeans 6.1
 
Junit
JunitJunit
Junit
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Grails Spock Testing
Grails Spock TestingGrails Spock Testing
Grails Spock Testing
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
How and what to unit test
How and what to unit testHow and what to unit test
How and what to unit test
 
JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
 

Viewers also liked

Why Johnny Can't Unit Test His Legacy Code - And What You Can Do About It
Why Johnny Can't Unit Test His Legacy Code - And What You Can Do About ItWhy Johnny Can't Unit Test His Legacy Code - And What You Can Do About It
Why Johnny Can't Unit Test His Legacy Code - And What You Can Do About ItHoward Deiner
 
Unbox yourself Into Testing
Unbox yourself Into TestingUnbox yourself Into Testing
Unbox yourself Into Testingvodqancr
 
Inverting The Testing Pyramid
Inverting The Testing PyramidInverting The Testing Pyramid
Inverting The Testing PyramidNaresh Jain
 
Agile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingAgile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingDimitri Ponomareff
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 
Web application testing with Selenium
Web application testing with SeleniumWeb application testing with Selenium
Web application testing with SeleniumKerry Buckley
 

Viewers also liked (7)

Why Johnny Can't Unit Test His Legacy Code - And What You Can Do About It
Why Johnny Can't Unit Test His Legacy Code - And What You Can Do About ItWhy Johnny Can't Unit Test His Legacy Code - And What You Can Do About It
Why Johnny Can't Unit Test His Legacy Code - And What You Can Do About It
 
Unbox yourself Into Testing
Unbox yourself Into TestingUnbox yourself Into Testing
Unbox yourself Into Testing
 
Inverting The Testing Pyramid
Inverting The Testing PyramidInverting The Testing Pyramid
Inverting The Testing Pyramid
 
Testing web application
Testing web applicationTesting web application
Testing web application
 
Agile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated TestingAgile Testing Framework - The Art of Automated Testing
Agile Testing Framework - The Art of Automated Testing
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 
Web application testing with Selenium
Web application testing with SeleniumWeb application testing with Selenium
Web application testing with Selenium
 

Similar to Tdd & unit test

Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartGabriele Lana
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutDror Helper
 
Android testing
Android testingAndroid testing
Android testingSean Tsai
 
In search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingIn search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingAnna Khabibullina
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytestHector Canto
 
Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)Oliver Klee
 
Software engineering ⊇ Software testing
Software engineering ⊇ Software testingSoftware engineering ⊇ Software testing
Software engineering ⊇ Software testingPavel Tcholakov
 
The uniform interface is 42
The uniform interface is 42The uniform interface is 42
The uniform interface is 42Yevhen Bobrov
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3Oliver Klee
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And DrupalPeter Arato
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockRobot Media
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingVisual Engineering
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent codeDror Helper
 
Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Oliver Klee
 

Similar to Tdd & unit test (20)

Refactoring In Tdd The Missing Part
Refactoring In Tdd The Missing PartRefactoring In Tdd The Missing Part
Refactoring In Tdd The Missing Part
 
Secret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you aboutSecret unit testing tools no one ever told you about
Secret unit testing tools no one ever told you about
 
Android testing
Android testingAndroid testing
Android testing
 
In search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testingIn search of JavaScript code quality: unit testing
In search of JavaScript code quality: unit testing
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
Rc2010 tdd
Rc2010 tddRc2010 tdd
Rc2010 tdd
 
Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)Test-driven development for TYPO3 (T3DD11)
Test-driven development for TYPO3 (T3DD11)
 
Junit and testNG
Junit and testNGJunit and testNG
Junit and testNG
 
Software engineering ⊇ Software testing
Software engineering ⊇ Software testingSoftware engineering ⊇ Software testing
Software engineering ⊇ Software testing
 
The uniform interface is 42
The uniform interface is 42The uniform interface is 42
The uniform interface is 42
 
Test-driven Development for TYPO3
Test-driven Development for TYPO3Test-driven Development for TYPO3
Test-driven Development for TYPO3
 
Agile mobile
Agile mobileAgile mobile
Agile mobile
 
Testing And Drupal
Testing And DrupalTesting And Drupal
Testing And Drupal
 
Junit 5 - Maior e melhor
Junit 5 - Maior e melhorJunit 5 - Maior e melhor
Junit 5 - Maior e melhor
 
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMockUnit testing in iOS featuring OCUnit, GHUnit & OCMock
Unit testing in iOS featuring OCUnit, GHUnit & OCMock
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Workshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testingWorkshop 23: ReactJS, React & Redux testing
Workshop 23: ReactJS, React & Redux testing
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2
 
Unit testing patterns for concurrent code
Unit testing patterns for concurrent codeUnit testing patterns for concurrent code
Unit testing patterns for concurrent code
 
Test-Driven Development for TYPO3
Test-Driven Development for TYPO3Test-Driven Development for TYPO3
Test-Driven Development for TYPO3
 

Recently uploaded

UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1DianaGray10
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationZilliz
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIES VE
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...Product School
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekCzechDreamin
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesBhaskar Mitra
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor TurskyiFwdays
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Julian Hyde
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...Product School
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...Product School
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...Product School
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesThousandEyes
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonDianaGray10
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Alison B. Lowndes
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsExpeed Software
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Jeffrey Haguewood
 

Recently uploaded (20)

UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1UiPath Test Automation using UiPath Test Suite series, part 1
UiPath Test Automation using UiPath Test Suite series, part 1
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
IESVE for Early Stage Design and Planning
IESVE for Early Stage Design and PlanningIESVE for Early Stage Design and Planning
IESVE for Early Stage Design and Planning
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
AI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří KarpíšekAI revolution and Salesforce, Jiří Karpíšek
AI revolution and Salesforce, Jiří Karpíšek
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
Measures in SQL (a talk at SF Distributed Systems meetup, 2024-05-22)
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
In-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT ProfessionalsIn-Depth Performance Testing Guide for IT Professionals
In-Depth Performance Testing Guide for IT Professionals
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 

Tdd & unit test

  • 1. Introduction to Test First Development @GomesNayagam
  • 2. Introduction to Developer Testing Unit Testing Frameworks Test First Development
  • 3. Benefits  Higher quality  Fewer defects  Living documentation  Well Crafted code  Automatic regression harness
  • 4. A unit test confirms functionality of a small unit of functionality or component in a larger system.
  • 6.
  • 7. How Unit Test Frameworks Work Unit Test Runner EXE Unit Test My Code My Unit Test Lib Common Lib
  • 9. TFD Details A Jig is a form upon which something else is built The jig is the specification for the thing being built Remove the jig when ready to Launch For software, Jig is built just a moment before it is being used
  • 10. Create a failing test Red Make the test pass Green RED Refactor Improve the internal implementation without changing the external contract or behavior Refactor Green
  • 11. The Tests are the Specs
  • 13. Test Integration vs. Organization Unit Tests Testing the Sad Unit Test Path Lifecycle
  • 14. Unit Test or Integration Test
  • 16. Test Lifecycle nUnit TestFixtureSetup SetUp Test TearDown TestFixtureTearDown
  • 17. Setting Up A Test Project Tests live in a separate class library project
  • 18. A First Test Using Nunit.Framework Namespace Domain.Tests { [TestFixture] public class FirstTestFixture { [Test] public void AFirstTest() { Assert.IsTrue(true,”true is true!”); } } }
  • 19. TFD - Assertions Use one logical assertion per test [Test] public void the_order_is_canceled() { var customer = CreateCustomer(); Assert.IsNotNull(customer); customer.PlaceOrder(); Assert.IsTrue(customer.HasOrder); customer.CancelOrder(); Assert.IsFalse(customer.HasOrder); }
  • 22. Isolation Techniques Test Method SUT Dependency Test Object Object
  • 23. Faking out the SUT Test Method 1.Create test specific objects 2.Create the SUT (using Interface) 3. Invoke operation on the SUT 4.Check results of SUT invocation On the SUT
  • 25. Dummy var person = new Person(); person.First = “Homer”; person.Last = “Simpson”; Assert.IsNotNull(person.FullName); var order = new Order(); order.AddLineItem(12, 1); order.AddLineItem.Add(21, 3); Assert.AreEqual(2, order.NumLineItems);
  • 26. Stub public class StubRepo : IOwnerRepository { public IOwner FindById(int id){} public IOwner Save(IOwner owner) { return new Owner(); } public void Delete(IOwner owner){} }
  • 27. Fake
  • 29. e.g. using System; using System.Collections.Generic; namespace MoqSamples.Models { public interface IProductRepository { List<IProduct> Select(); IProduct Get(int id); } public interface IProduct { int Id {get; set;} string Name { get; set; } } } // Mock a product var newProduct = new Mock<IProduct>(); newProduct.ExpectGet(p => p.Id).Returns(1); newProduct.ExpectGet(p => p.Name).Returns("Bushmills"); Assert.AreEqual("Bushmills", newProduct.Object.Name);
  • 30. e.g. // Mock product repository var productRepository = new Mock<IProductRepository>(); productRepository.Expect(p => p.Get(1)).Returns(newProduct.Object) // Act var productReturned = productRepository.Object.Get(1); // Assert Assert.AreEqual("Bushmills", productReturned.Name); // Mock product repository var productRepository = new Mock<IProductRepository>(); productRepository .Expect(p => p.Get(It.IsAny<int>())) .Returns(newProduct.Object);
  • 31. Summery… Stick with red - Write tests in a green separate - project refactor Keep Treat test code practicing and with respect learning
  • 32. Reference Test Driven Development by Example , Kent Beck, 2002 http://code.google.com/p/moq/wiki/QuickStart http://stephenwalther.com/blog/archive/2008/06/12/tdd- introduction-to-moq.aspx Source: Pluralsight,Google,WIKI