SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados

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
 

Mais procurados (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
 

Destaque

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
 

Destaque (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
 

Semelhante 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
 

Semelhante 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

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
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
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
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
 
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
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 

Último (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
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
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
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
 
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
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
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
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 

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