SlideShare una empresa de Scribd logo
1 de 32
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

Más contenido relacionado

La actualidad más candente

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
 

La actualidad más candente (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
 

Destacado

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
 

Destacado (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 a 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 a 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
 

Último

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 

Último (20)

Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 

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