SlideShare ist ein Scribd-Unternehmen logo
1 von 29
Downloaden Sie, um offline zu lesen
Test & Behaviour
Driven Development
Lars Thorup
ZeaLake Software Consulting


March, 2012
Who is Lars Thorup?

●   Software developer/architect
    ●   C++, C# and JavaScript
    ●   Test Driven Development

●   Coach: Teaching agile and
    automated testing

●   Advisor: Assesses software
    projects and companies

●   Founder and CEO of
    BestBrains and ZeaLake
Why are we here today?
●   What is TDD/BDD?
    ●   Express expected behaviour before writing code

●   Why is TDD/BDD a good thing?
    ●   Enjoy more efficient and predictable course of development
    ●   Find and fix bugs faster
    ●   Prevent bugs from reappearing
    ●   Improve the design of our software
    ●   Reliable documentation

●   How do we do TDD/BDD?
    ●   Write test programs
    ●   Run the tests automatically
Workflow of TDD/BDD
    Think, talk
                  Idea

                          Test
                                   Failing
                                    test



                                                   Code


                      Good                   Succeeding
                     design      Refactor       test
BDD or TDD?
●   Behaviour first
    ●   makes more sense than "Test first"

●   Structure of test programs
    ●   Given <precondition>
    ●   When <invocation>
    ●   Then <expectation>

●   High level as well as low level
    ●   Testing user stories and requirements
    ●   Testing class design and algorithms

●   Communicate intent

●   Fast feedback
Different kinds of automated tests
●   Unit tests
    ●   Test individual pieces of code and the interaction between code
        blocks


●   System tests / acceptance tests
    ●   Verify the behaviour of the entire system against the requirements


●   Performance tests
    ●   Test non functional requirements
Unit tests or system tests?
●   Unit tests are efficient
    ●   Fast to run (hundreds per second)
    ●   Robust and predictable
    ●   Can be easy to write
    ●   Is written together with the code it is testing


●   System tests are thorough
    ●   Tests all layers together
    ●   Most efficient way to create a set of tests for existing code
    ●   Can be easier to read for non-technical people
Can we automate performance tests?
●   Performance tests are brittle
    ●   Tip: create performance trend curves instead
How do we run the tests automatically?
●   From our programming environment (IDE)
    ●   Command line: make test
    ●   Right click | Run Tests

●   On every commit
    ●   Setup a build server
    ●   Jenkins, TeamCity
    ●   Let the build server run all tests
    ●   Get build notifications
    ●   Keep the build green
    ●   Fixing a broken build has priority over any other development task
How can tests help improve our design?
●   The software design will evolve over time

●   A refactoring improves the design without changing
    behavior

●   Tests ensure that behavior is not
    accidentally changed


●   Without tests, refactoring is scary
    ●   and with no refactoring, the design decays over time


●   With tests, we have the courage to refactor
    ●   so we continually keep our design healthy
Are we wasting developer time writing tests?
●   No

●   Time spent writing tests is not taken from time spent coding
    ●   ... but from time otherwise spent on manual testing and debugging

●   The cost of a bug keeps
    increasing until we fix it

●   Find bugs faster
    ●   Avoid losing customer confidence
    ●   Free QA to do exploratory testing
        so they find the hard-to-find bugs
    ●   Spend less time trying to figure out
        what is causing the bug and how to fix it

●   Avoid spending time testing again
How do we get started?
●   When we have a lot of existing code without tests
    ●   Create a set of system tests to get a safety net


●   When we are writing new code
    ●   Write unit tests in conjunction with the new code


●   Set up a standard test environment for our specific
    application
    ●   Test data: Automate the creation of standard testdata in a local
        database
    ●   External dependencies: Write stubs to use in the tests
Maintainability
●   Stick to a pattern for your tests
    ●   E.g. Given-When-Then

●   Focus on readability over code duplication in test code

●   Write reusable helper classes (builders) to simplify tests
What does a real-world project look like?
●   wizerize.com
    ●   Web application: C# and JavaScript
    ●   3½ years of development, 3½ years in production
    ●   2-4 developers
    ●   40% test code, 60% production code (in lines of code)
    ●   71% code coverage of unit tests
    ●   872 unit tests – run in 1½ minute
    ●   72 system tests – run in 20 minutes
    ●   No functional errors seen by end users in production (yet)
Where can I read more?
●   http://googletesting.blogspot.com/

●   http://testdrivendeveloper.com/

●   http://codesheriff.blogspot.com/

●   http://www.zealake.com/category/test/
But what about:
●   Stubs & mocks

●   Test data

●   UI testing

●   SQL testing

●   JavaScript testing

●   Web Service testing

●   Legacy code
What is good design?
●   One element of good design is loose coupling
    ●   Use interfaces (for static languages)
    ●   Inject dependencies
                                   public void Trigger()
●   Avoid using new:               {
                                       var emailSvc = new EmailSvc();
                                       emailSvc.SendEmail();
                                   }


●   Inject dependencies instead:
               private IEmailSvc emailSvc;
               public Notifier(IEmailSvc emailSvc)
               {
                   this.emailSvc = emailSvc;
               }

               public void Trigger()
               {
                   emailSvc.SendEmail();
Stubs and mocks
●   When testing an object X, that depends on an object Y
    ●   replace the real Y with a fake Y

●   Benefits
    ●   Only test one thing (X) at a time
                                                              NotifierTest
    ●   Faster tests (Y may be slow)
    ●   Simpler (Y may depend on Z etc)

●   Examples:                                IEmailSvc          Notifier
    ●   Time
    ●   Database
                                    EmailSvcStub   EmailSvc
    ●   Email
    ●   HttpContext
Stubs
●   Hand crafted

●   More effort to write

●   Easier to maintain

●   Can be more "black box" than mocks
Mocks
●   Mocks are automatically generated stubs

●   Easy to use

●   More "magical"

●   More effort to maintain

●   Will be more "white-box" than stubs

●   Example frameworks:
    ●   Moq
    ●   NSubstitute
Stubs - example
                              public class EmailSvcStub : IEmailSvc
                              {
                                  public int NumberOfEmailsSent { get; set; }

                                  public void SendEmail()
                                  {
                                      ++NumberOfEmailsSent;
                                  }
                              }


 [Test]
 public void Trigger()
 {
     // setup
     var emailSvc = new EmailSvcStub();
     var notifier = new Notifier(emailSvc);

     // invoke
     notifier.Trigger();

     // verify
     Assert.That(emailSvc.NumberOfEmailsSent, Is.EqualTo(1));
 }
Mocks - example




 [Test]
 public void Trigger()
 {
     // setup
     var emailSvc = Substitute.For<IEmailSvc>();
     var notifier = new Notifier(emailSvc);

     // invoke
     notifier.Trigger();

     // verify
     emailSvc.Received(1).SendEmail();
 }
Test data
●   Each developer his/her own database

●   Standard test data
    ●   Created before running tests

●   Test data builders
    ●   Stubbed database
    ●   Real database
Test data builder - example
[Test]
public void GetResponseMedia()
{
    // given
    var stub = new StubBuilder
    {
        Questions = new [] {
            new QuestionBuilder { Name = "MEDIA" },
        },
        Participants = new[] {
            new ParticipantBuilder { Name = "Lars", Votes = new [] {
                new VoteBuilder { Question = "MEDIA", Responses =
                    new ResponseBuilder(new byte [] {1, 2, 3}) },
            }},
        },
    }.Build();
    var voteController = new VoteController(stub.Session);

     // when
     var result = voteController.GetResponseMedia(vote.Id, true) as MediaResult;

     // then
     Assert.That(result.Download, Is.True);
     Assert.That(result.MediaLength, Is.EqualTo(3));
     Assert.That(TfResponse.ReadAllBytes(result.MediaStream), Is.EqualTo(new byte[] {1, 2, 3}));
}
Web UI testing
●   Control a browser from the tests using a seperate tool

●   Tools
    ●   Selenium
    ●   WatiN
    ●   Cucumber + capybara

●   Minimize system level testing
    ●   Web UI tests are brittle and slow
    ●   Hard to integrate into continuous integration

●   Maximize JavaScript unit testing
SQL testing
●   Test stored procedure, constraints, functions and triggers

●   Use your backend testing framework (like NUnit)
    ●   Easy to integrate in your Continuous Integration process

●   Consider using a dedicated framework

●   Or write your own
JavaScript testing
●   Use a JavaScript unit testing framework
    ●   QUnit
    ●   jsTestDriver
    ●   Jasmine
Web Service testing
●    Use your backend testing framework (like NUnit)

●    Use a JSON friendly version of WebClient:
    // when
    var votes = jsonClient.Get("Vote", "GetVotes", new { questionId = questionId });

    // then
    Assert.That(votes.Length, Is.EqualTo(1));
    var vote = votes[0];
    Assert.That(vote.ResponseText, Is.EqualTo("3"));
    Assert.That(vote.ParticipantName, Is.EqualTo("Lars Thorup"));


●    Input converted from .NET anonymous type to JSON

●    Output converted from JSON to .NET dynamic type

●    https://github.com/larsthorup/JsonClient
Legacy code
●   Add pinning tests
    ●   special kinds of unit tests for legacy
        code
    ●   verifies existing behaviour
    ●   acts as a safety net

●   Can be driven by change requests

●   Refactor the code to be able to write
    unit tests

●   Add unit test for the change request

●   Track coverage trend for existing
    code
    ●   and make sure it grows

Weitere ähnliche Inhalte

Was ist angesagt?

Bdd – with cucumber and gherkin
Bdd – with cucumber and gherkinBdd – with cucumber and gherkin
Bdd – with cucumber and gherkinArati Joshi
 
Behavior driven development (bdd)
Behavior driven development (bdd)Behavior driven development (bdd)
Behavior driven development (bdd)Rohit Bisht
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010guest5639fa9
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Mindfire Solutions
 
An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)Suman Guha
 
Introduction to BDD
Introduction to BDDIntroduction to BDD
Introduction to BDDKnoldus Inc.
 
Basic Guide to Manual Testing
Basic Guide to Manual TestingBasic Guide to Manual Testing
Basic Guide to Manual TestingHiral Gosani
 
Writing Test Cases 20110808
Writing Test Cases 20110808Writing Test Cases 20110808
Writing Test Cases 20110808slovejoy
 
BDD presentation
BDD presentationBDD presentation
BDD presentationtemebele
 
BDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world applicationBDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world applicationJohn Ferguson Smart Limited
 
Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber Knoldus Inc.
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
Agile QA presentation
Agile QA presentationAgile QA presentation
Agile QA presentationCarl Bruiners
 
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
 
Agile QA and Testing process
Agile QA and Testing processAgile QA and Testing process
Agile QA and Testing processGloria Stoilova
 

Was ist angesagt? (20)

Bdd – with cucumber and gherkin
Bdd – with cucumber and gherkinBdd – with cucumber and gherkin
Bdd – with cucumber and gherkin
 
Behavior driven development (bdd)
Behavior driven development (bdd)Behavior driven development (bdd)
Behavior driven development (bdd)
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)
 
An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)An introduction to Behavior-Driven Development (BDD)
An introduction to Behavior-Driven Development (BDD)
 
Introduction to BDD
Introduction to BDDIntroduction to BDD
Introduction to BDD
 
Test automation proposal
Test automation proposalTest automation proposal
Test automation proposal
 
Cucumber BDD
Cucumber BDDCucumber BDD
Cucumber BDD
 
Basic Guide to Manual Testing
Basic Guide to Manual TestingBasic Guide to Manual Testing
Basic Guide to Manual Testing
 
Test Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and CucumberTest Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and Cucumber
 
Writing Test Cases 20110808
Writing Test Cases 20110808Writing Test Cases 20110808
Writing Test Cases 20110808
 
BDD presentation
BDD presentationBDD presentation
BDD presentation
 
BDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world applicationBDD in Action – principles, practices and real-world application
BDD in Action – principles, practices and real-world application
 
Selenium with Cucumber
Selenium  with Cucumber Selenium  with Cucumber
Selenium with Cucumber
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
 
Agile QA presentation
Agile QA presentationAgile QA presentation
Agile QA presentation
 
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
 
Agile QA and Testing process
Agile QA and Testing processAgile QA and Testing process
Agile QA and Testing process
 
Introduction to Agile Testing
Introduction to Agile TestingIntroduction to Agile Testing
Introduction to Agile Testing
 

Ähnlich wie Test and Behaviour Driven Development (TDD/BDD)

Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Tddbdd workshop
Tddbdd workshopTddbdd workshop
Tddbdd workshopBestBrains
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Howsatesgoral
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBaskar K
 
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
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonIneke Scheffers
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code cleanBrett Child
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017Ortus Solutions, Corp
 
Qt test framework
Qt test frameworkQt test framework
Qt test frameworkICS
 
Using Selenium to Improve a Teams Development Cycle
Using Selenium to Improve a Teams Development CycleUsing Selenium to Improve a Teams Development Cycle
Using Selenium to Improve a Teams Development Cycleseleniumconf
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchMats Bryntse
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsOrtus Solutions, Corp
 
Art of unit testing: how to do it right
Art of unit testing: how to do it rightArt of unit testing: how to do it right
Art of unit testing: how to do it rightDmytro Patserkovskyi
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development IntroductionNguyen Hai
 

Ähnlich wie Test and Behaviour Driven Development (TDD/BDD) (20)

Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Tddbdd workshop
Tddbdd workshopTddbdd workshop
Tddbdd workshop
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Unit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and HowsUnit Testing - The Whys, Whens and Hows
Unit Testing - The Whys, Whens and Hows
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 
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
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
Unit testing on mobile apps
Unit testing on mobile appsUnit testing on mobile apps
Unit testing on mobile apps
 
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
North Virginia Coldfusion User Group Meetup - Testbox - July 19th 2017
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
 
Using Selenium to Improve a Teams Development Cycle
Using Selenium to Improve a Teams Development CycleUsing Selenium to Improve a Teams Development Cycle
Using Selenium to Improve a Teams Development Cycle
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Art of unit testing: how to do it right
Art of unit testing: how to do it rightArt of unit testing: how to do it right
Art of unit testing: how to do it right
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
 

Mehr von Lars Thorup

100 tests per second - 40 releases per week
100 tests per second - 40 releases per week100 tests per second - 40 releases per week
100 tests per second - 40 releases per weekLars Thorup
 
SQL or NoSQL - how to choose
SQL or NoSQL - how to chooseSQL or NoSQL - how to choose
SQL or NoSQL - how to chooseLars Thorup
 
Super fast end-to-end-tests
Super fast end-to-end-testsSuper fast end-to-end-tests
Super fast end-to-end-testsLars Thorup
 
Extreme Programming - to the next-level
Extreme Programming - to the next-levelExtreme Programming - to the next-level
Extreme Programming - to the next-levelLars Thorup
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit TestingLars Thorup
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingLars Thorup
 
Put "fast" back in "fast feedback"
Put "fast" back in "fast feedback"Put "fast" back in "fast feedback"
Put "fast" back in "fast feedback"Lars Thorup
 
Database Schema Evolution
Database Schema EvolutionDatabase Schema Evolution
Database Schema EvolutionLars Thorup
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingLars Thorup
 
Javascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and SinonJavascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and SinonLars Thorup
 
Continuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptContinuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptLars Thorup
 
Automated Performance Testing
Automated Performance TestingAutomated Performance Testing
Automated Performance TestingLars Thorup
 
High Performance Software Engineering Teams
High Performance Software Engineering TeamsHigh Performance Software Engineering Teams
High Performance Software Engineering TeamsLars Thorup
 
Elephant Carpaccio
Elephant CarpaccioElephant Carpaccio
Elephant CarpaccioLars Thorup
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Lars Thorup
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitLars Thorup
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated TestingLars Thorup
 

Mehr von Lars Thorup (18)

100 tests per second - 40 releases per week
100 tests per second - 40 releases per week100 tests per second - 40 releases per week
100 tests per second - 40 releases per week
 
SQL or NoSQL - how to choose
SQL or NoSQL - how to chooseSQL or NoSQL - how to choose
SQL or NoSQL - how to choose
 
Super fast end-to-end-tests
Super fast end-to-end-testsSuper fast end-to-end-tests
Super fast end-to-end-tests
 
Extreme Programming - to the next-level
Extreme Programming - to the next-levelExtreme Programming - to the next-level
Extreme Programming - to the next-level
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit Testing
 
Advanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit TestingAdvanced QUnit - Front-End JavaScript Unit Testing
Advanced QUnit - Front-End JavaScript Unit Testing
 
Put "fast" back in "fast feedback"
Put "fast" back in "fast feedback"Put "fast" back in "fast feedback"
Put "fast" back in "fast feedback"
 
Database Schema Evolution
Database Schema EvolutionDatabase Schema Evolution
Database Schema Evolution
 
Advanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit TestingAdvanced Jasmine - Front-End JavaScript Unit Testing
Advanced Jasmine - Front-End JavaScript Unit Testing
 
Javascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and SinonJavascript unit testing with QUnit and Sinon
Javascript unit testing with QUnit and Sinon
 
Continuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptContinuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScript
 
Automated Performance Testing
Automated Performance TestingAutomated Performance Testing
Automated Performance Testing
 
Agile Contracts
Agile ContractsAgile Contracts
Agile Contracts
 
High Performance Software Engineering Teams
High Performance Software Engineering TeamsHigh Performance Software Engineering Teams
High Performance Software Engineering Teams
 
Elephant Carpaccio
Elephant CarpaccioElephant Carpaccio
Elephant Carpaccio
 
Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++Automated Testing for Embedded Software in C or C++
Automated Testing for Embedded Software in C or C++
 
Unit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnitUnit Testing in JavaScript with MVC and QUnit
Unit Testing in JavaScript with MVC and QUnit
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
 

Kürzlich hochgeladen

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesThousandEyes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integrationmarketing932765
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Kaya Weers
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 

Kürzlich hochgeladen (20)

Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyesHow to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
How to Effectively Monitor SD-WAN and SASE Environments with ThousandEyes
 
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS:  6 Ways to Automate Your Data IntegrationBridging Between CAD & GIS:  6 Ways to Automate Your Data Integration
Bridging Between CAD & GIS: 6 Ways to Automate Your Data Integration
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)Design pattern talk by Kaya Weers - 2024 (v2)
Design pattern talk by Kaya Weers - 2024 (v2)
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 

Test and Behaviour Driven Development (TDD/BDD)

  • 1. Test & Behaviour Driven Development Lars Thorup ZeaLake Software Consulting March, 2012
  • 2. Who is Lars Thorup? ● Software developer/architect ● C++, C# and JavaScript ● Test Driven Development ● Coach: Teaching agile and automated testing ● Advisor: Assesses software projects and companies ● Founder and CEO of BestBrains and ZeaLake
  • 3. Why are we here today? ● What is TDD/BDD? ● Express expected behaviour before writing code ● Why is TDD/BDD a good thing? ● Enjoy more efficient and predictable course of development ● Find and fix bugs faster ● Prevent bugs from reappearing ● Improve the design of our software ● Reliable documentation ● How do we do TDD/BDD? ● Write test programs ● Run the tests automatically
  • 4. Workflow of TDD/BDD Think, talk Idea Test Failing test Code Good Succeeding design Refactor test
  • 5. BDD or TDD? ● Behaviour first ● makes more sense than "Test first" ● Structure of test programs ● Given <precondition> ● When <invocation> ● Then <expectation> ● High level as well as low level ● Testing user stories and requirements ● Testing class design and algorithms ● Communicate intent ● Fast feedback
  • 6. Different kinds of automated tests ● Unit tests ● Test individual pieces of code and the interaction between code blocks ● System tests / acceptance tests ● Verify the behaviour of the entire system against the requirements ● Performance tests ● Test non functional requirements
  • 7. Unit tests or system tests? ● Unit tests are efficient ● Fast to run (hundreds per second) ● Robust and predictable ● Can be easy to write ● Is written together with the code it is testing ● System tests are thorough ● Tests all layers together ● Most efficient way to create a set of tests for existing code ● Can be easier to read for non-technical people
  • 8. Can we automate performance tests? ● Performance tests are brittle ● Tip: create performance trend curves instead
  • 9. How do we run the tests automatically? ● From our programming environment (IDE) ● Command line: make test ● Right click | Run Tests ● On every commit ● Setup a build server ● Jenkins, TeamCity ● Let the build server run all tests ● Get build notifications ● Keep the build green ● Fixing a broken build has priority over any other development task
  • 10. How can tests help improve our design? ● The software design will evolve over time ● A refactoring improves the design without changing behavior ● Tests ensure that behavior is not accidentally changed ● Without tests, refactoring is scary ● and with no refactoring, the design decays over time ● With tests, we have the courage to refactor ● so we continually keep our design healthy
  • 11. Are we wasting developer time writing tests? ● No ● Time spent writing tests is not taken from time spent coding ● ... but from time otherwise spent on manual testing and debugging ● The cost of a bug keeps increasing until we fix it ● Find bugs faster ● Avoid losing customer confidence ● Free QA to do exploratory testing so they find the hard-to-find bugs ● Spend less time trying to figure out what is causing the bug and how to fix it ● Avoid spending time testing again
  • 12. How do we get started? ● When we have a lot of existing code without tests ● Create a set of system tests to get a safety net ● When we are writing new code ● Write unit tests in conjunction with the new code ● Set up a standard test environment for our specific application ● Test data: Automate the creation of standard testdata in a local database ● External dependencies: Write stubs to use in the tests
  • 13. Maintainability ● Stick to a pattern for your tests ● E.g. Given-When-Then ● Focus on readability over code duplication in test code ● Write reusable helper classes (builders) to simplify tests
  • 14. What does a real-world project look like? ● wizerize.com ● Web application: C# and JavaScript ● 3½ years of development, 3½ years in production ● 2-4 developers ● 40% test code, 60% production code (in lines of code) ● 71% code coverage of unit tests ● 872 unit tests – run in 1½ minute ● 72 system tests – run in 20 minutes ● No functional errors seen by end users in production (yet)
  • 15. Where can I read more? ● http://googletesting.blogspot.com/ ● http://testdrivendeveloper.com/ ● http://codesheriff.blogspot.com/ ● http://www.zealake.com/category/test/
  • 16. But what about: ● Stubs & mocks ● Test data ● UI testing ● SQL testing ● JavaScript testing ● Web Service testing ● Legacy code
  • 17. What is good design? ● One element of good design is loose coupling ● Use interfaces (for static languages) ● Inject dependencies public void Trigger() ● Avoid using new: { var emailSvc = new EmailSvc(); emailSvc.SendEmail(); } ● Inject dependencies instead: private IEmailSvc emailSvc; public Notifier(IEmailSvc emailSvc) { this.emailSvc = emailSvc; } public void Trigger() { emailSvc.SendEmail();
  • 18. Stubs and mocks ● When testing an object X, that depends on an object Y ● replace the real Y with a fake Y ● Benefits ● Only test one thing (X) at a time NotifierTest ● Faster tests (Y may be slow) ● Simpler (Y may depend on Z etc) ● Examples: IEmailSvc Notifier ● Time ● Database EmailSvcStub EmailSvc ● Email ● HttpContext
  • 19. Stubs ● Hand crafted ● More effort to write ● Easier to maintain ● Can be more "black box" than mocks
  • 20. Mocks ● Mocks are automatically generated stubs ● Easy to use ● More "magical" ● More effort to maintain ● Will be more "white-box" than stubs ● Example frameworks: ● Moq ● NSubstitute
  • 21. Stubs - example public class EmailSvcStub : IEmailSvc { public int NumberOfEmailsSent { get; set; } public void SendEmail() { ++NumberOfEmailsSent; } } [Test] public void Trigger() { // setup var emailSvc = new EmailSvcStub(); var notifier = new Notifier(emailSvc); // invoke notifier.Trigger(); // verify Assert.That(emailSvc.NumberOfEmailsSent, Is.EqualTo(1)); }
  • 22. Mocks - example [Test] public void Trigger() { // setup var emailSvc = Substitute.For<IEmailSvc>(); var notifier = new Notifier(emailSvc); // invoke notifier.Trigger(); // verify emailSvc.Received(1).SendEmail(); }
  • 23. Test data ● Each developer his/her own database ● Standard test data ● Created before running tests ● Test data builders ● Stubbed database ● Real database
  • 24. Test data builder - example [Test] public void GetResponseMedia() { // given var stub = new StubBuilder { Questions = new [] { new QuestionBuilder { Name = "MEDIA" }, }, Participants = new[] { new ParticipantBuilder { Name = "Lars", Votes = new [] { new VoteBuilder { Question = "MEDIA", Responses = new ResponseBuilder(new byte [] {1, 2, 3}) }, }}, }, }.Build(); var voteController = new VoteController(stub.Session); // when var result = voteController.GetResponseMedia(vote.Id, true) as MediaResult; // then Assert.That(result.Download, Is.True); Assert.That(result.MediaLength, Is.EqualTo(3)); Assert.That(TfResponse.ReadAllBytes(result.MediaStream), Is.EqualTo(new byte[] {1, 2, 3})); }
  • 25. Web UI testing ● Control a browser from the tests using a seperate tool ● Tools ● Selenium ● WatiN ● Cucumber + capybara ● Minimize system level testing ● Web UI tests are brittle and slow ● Hard to integrate into continuous integration ● Maximize JavaScript unit testing
  • 26. SQL testing ● Test stored procedure, constraints, functions and triggers ● Use your backend testing framework (like NUnit) ● Easy to integrate in your Continuous Integration process ● Consider using a dedicated framework ● Or write your own
  • 27. JavaScript testing ● Use a JavaScript unit testing framework ● QUnit ● jsTestDriver ● Jasmine
  • 28. Web Service testing ● Use your backend testing framework (like NUnit) ● Use a JSON friendly version of WebClient: // when var votes = jsonClient.Get("Vote", "GetVotes", new { questionId = questionId }); // then Assert.That(votes.Length, Is.EqualTo(1)); var vote = votes[0]; Assert.That(vote.ResponseText, Is.EqualTo("3")); Assert.That(vote.ParticipantName, Is.EqualTo("Lars Thorup")); ● Input converted from .NET anonymous type to JSON ● Output converted from JSON to .NET dynamic type ● https://github.com/larsthorup/JsonClient
  • 29. Legacy code ● Add pinning tests ● special kinds of unit tests for legacy code ● verifies existing behaviour ● acts as a safety net ● Can be driven by change requests ● Refactor the code to be able to write unit tests ● Add unit test for the change request ● Track coverage trend for existing code ● and make sure it grows