SlideShare a Scribd company logo
1 of 29
Download to read offline
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

More Related Content

What's hot

Introduction to BDD
Introduction to BDDIntroduction to BDD
Introduction to BDDKnoldus Inc.
Β 
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
Β 
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 CucumberRhoynar Software Consulting
Β 
Cucumber & gherkin language
Cucumber & gherkin languageCucumber & gherkin language
Cucumber & gherkin languageselvanathankapilan
Β 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDDAnuar Nurmakanov
Β 
What Is Cucumber?
What Is Cucumber?What Is Cucumber?
What Is Cucumber?QATestLab
Β 
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
Β 
Bdd – with cucumber and gherkin
Bdd – with cucumber and gherkinBdd – with cucumber and gherkin
Bdd – with cucumber and gherkinArati Joshi
Β 
API Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+CucumberAPI Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+CucumberKnoldus Inc.
Β 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with CucumberBrandon Keepers
Β 
Successfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile WorldSuccessfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile WorldSmartBear
Β 
Agile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User GroupAgile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User Groupsuwalki24.pl
Β 
Behavior driven development (bdd)
Behavior driven development (bdd)Behavior driven development (bdd)
Behavior driven development (bdd)Rohit Bisht
Β 
Introduction to Test Automation
Introduction to Test AutomationIntroduction to Test Automation
Introduction to Test AutomationPekka KlΓ€rck
Β 
Agile Testing Process
Agile Testing ProcessAgile Testing Process
Agile Testing ProcessIntetics
Β 
Behavior Driven Development (BDD)
Behavior Driven Development (BDD)Behavior Driven Development (BDD)
Behavior Driven Development (BDD)Ajay Danait
Β 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React AppAll Things Open
Β 
Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberKMS Technology
Β 

What's hot (20)

Introduction to BDD
Introduction to BDDIntroduction to BDD
Introduction to BDD
Β 
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)
Β 
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
Β 
Cucumber & gherkin language
Cucumber & gherkin languageCucumber & gherkin language
Cucumber & gherkin language
Β 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
Β 
What Is Cucumber?
What Is Cucumber?What Is Cucumber?
What Is Cucumber?
Β 
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
Β 
Bdd – with cucumber and gherkin
Bdd – with cucumber and gherkinBdd – with cucumber and gherkin
Bdd – with cucumber and gherkin
Β 
API Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+CucumberAPI Automation Testing Using RestAssured+Cucumber
API Automation Testing Using RestAssured+Cucumber
Β 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
Β 
Behavior Driven Development with Cucumber
Behavior Driven Development with CucumberBehavior Driven Development with Cucumber
Behavior Driven Development with Cucumber
Β 
Successfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile WorldSuccessfully Implementing BDD in an Agile World
Successfully Implementing BDD in an Agile World
Β 
Agile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User GroupAgile Testing - presentation for Agile User Group
Agile Testing - presentation for Agile User Group
Β 
Behavior driven development (bdd)
Behavior driven development (bdd)Behavior driven development (bdd)
Behavior driven development (bdd)
Β 
Introduction to Test Automation
Introduction to Test AutomationIntroduction to Test Automation
Introduction to Test Automation
Β 
Introduction to Agile Testing
Introduction to Agile TestingIntroduction to Agile Testing
Introduction to Agile Testing
Β 
Agile Testing Process
Agile Testing ProcessAgile Testing Process
Agile Testing Process
Β 
Behavior Driven Development (BDD)
Behavior Driven Development (BDD)Behavior Driven Development (BDD)
Behavior Driven Development (BDD)
Β 
The Many Ways to Test Your React App
The Many Ways to Test Your React AppThe Many Ways to Test Your React App
The Many Ways to Test Your React App
Β 
Behavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using CucumberBehavior Driven Development and Automation Testing Using Cucumber
Behavior Driven Development and Automation Testing Using Cucumber
Β 

Similar to 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
Β 
Unit testing on mobile apps
Unit testing on mobile appsUnit testing on mobile apps
Unit testing on mobile appsBuşra Deniz, CSM
Β 
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
Β 

Similar to 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
Β 

More from 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
Β 
Agile Contracts
Agile ContractsAgile Contracts
Agile ContractsLars 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
Β 

More from 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
Β 

Recently uploaded

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
Β 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
Β 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
Β 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
Β 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
Β 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
Β 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
Β 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
Β 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
Β 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
Β 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
Β 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
Β 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
Β 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
Β 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraΓΊjo
Β 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
Β 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
Β 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
Β 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
Β 

Recently uploaded (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
Β 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Β 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
Β 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
Β 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
Β 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Β 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Β 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
Β 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
Β 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
Β 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
Β 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Β 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Β 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
Β 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Β 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Β 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
Β 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
Β 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
Β 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
Β 

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