SlideShare a Scribd company logo
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

Cucumber BDD
Cucumber BDDCucumber BDD
Cucumber BDD
Pravin Dsilva
 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
MohamedSubhiBouchi
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumber
Daniel Kummer
 
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
 
What Is Cucumber?
What Is Cucumber?What Is Cucumber?
What Is Cucumber?
QATestLab
 
BDD & Cucumber
BDD & CucumberBDD & Cucumber
BDD & Cucumber
Vladimir Arutin
 
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 Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
David Ehringer
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
harinderpisces
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 
Automated Test Framework with Cucumber
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with Cucumber
Ramesh Krishnan Ganesan
 
Behavior Driven Development (BDD)
Behavior Driven Development (BDD)Behavior Driven Development (BDD)
Behavior Driven Development (BDD)
Ajay Danait
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
Knoldus Inc.
 
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
KMS Technology
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
guestc8093a6
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
nickokiss
 
Agile Testing: The Role Of The Agile Tester
Agile Testing: The Role Of The Agile TesterAgile Testing: The Role Of The Agile Tester
Agile Testing: The Role Of The Agile Tester
Declan Whelan
 
BDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVABDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVA
Srinivas Katakam
 
Introduction to Behaviour Driven Development (BDD) and Cucumber with Java
Introduction to Behaviour Driven Development (BDD) and Cucumber with JavaIntroduction to Behaviour Driven Development (BDD) and Cucumber with Java
Introduction to Behaviour Driven Development (BDD) and Cucumber with Java
Jawad Khan
 

What's hot (20)

Cucumber BDD
Cucumber BDDCucumber BDD
Cucumber BDD
 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
 
BDD testing with cucumber
BDD testing with cucumberBDD testing with cucumber
BDD testing with cucumber
 
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)
 
What Is Cucumber?
What Is Cucumber?What Is Cucumber?
What Is Cucumber?
 
BDD & Cucumber
BDD & CucumberBDD & Cucumber
BDD & Cucumber
 
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 Driven Development (TDD)
Test Driven Development (TDD)Test Driven Development (TDD)
Test Driven Development (TDD)
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Automated Test Framework with Cucumber
Automated Test Framework with CucumberAutomated Test Framework with Cucumber
Automated Test Framework with Cucumber
 
Behavior Driven Development (BDD)
Behavior Driven Development (BDD)Behavior Driven Development (BDD)
Behavior Driven Development (BDD)
 
BDD with Cucumber
BDD with CucumberBDD with Cucumber
BDD with Cucumber
 
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
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Agile Testing: The Role Of The Agile Tester
Agile Testing: The Role Of The Agile TesterAgile Testing: The Role Of The Agile Tester
Agile Testing: The Role Of The Agile Tester
 
BDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVABDD WITH CUCUMBER AND JAVA
BDD WITH CUCUMBER AND JAVA
 
Introduction to Behaviour Driven Development (BDD) and Cucumber with Java
Introduction to Behaviour Driven Development (BDD) and Cucumber with JavaIntroduction to Behaviour Driven Development (BDD) and Cucumber with Java
Introduction to Behaviour Driven Development (BDD) and Cucumber with Java
 

Similar to Test and Behaviour Driven Development (TDD/BDD)

Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
Lars Thorup
 
Tddbdd workshop
Tddbdd workshopTddbdd workshop
Tddbdd workshop
BestBrains
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
Jon 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 Hows
atesgoral
 
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
Baskar 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 Framework
Peter Kofler
 
Developers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomonDevelopers Testing - Girl Code at bloomon
Developers Testing - Girl Code at bloomon
Ineke Scheffers
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
Brett 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 apps
Buş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 2017
Ortus Solutions, Corp
 
Qt test framework
Qt test frameworkQt test framework
Qt test framework
ICS
 
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
seleniumconf
 
Testing Ext JS and Sencha Touch
Testing Ext JS and Sencha TouchTesting Ext JS and Sencha Touch
Testing Ext JS and Sencha Touch
Mats 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 applications
Ortus 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 right
Dmytro 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 code
Pascal Larocque
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
Nguyen 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 week
Lars Thorup
 
SQL or NoSQL - how to choose
SQL or NoSQL - how to chooseSQL or NoSQL - how to choose
SQL or NoSQL - how to choose
Lars Thorup
 
Super fast end-to-end-tests
Super fast end-to-end-testsSuper fast end-to-end-tests
Super fast end-to-end-tests
Lars Thorup
 
Extreme Programming - to the next-level
Extreme Programming - to the next-levelExtreme Programming - to the next-level
Extreme Programming - to the next-level
Lars Thorup
 
Advanced Javascript Unit Testing
Advanced Javascript Unit TestingAdvanced Javascript Unit Testing
Advanced Javascript Unit Testing
Lars 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 Testing
Lars 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 Evolution
Lars 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 Testing
Lars 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 Sinon
Lars Thorup
 
Continuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScriptContinuous Integration for front-end JavaScript
Continuous Integration for front-end JavaScript
Lars Thorup
 
Automated Performance Testing
Automated Performance TestingAutomated Performance Testing
Automated Performance Testing
Lars Thorup
 
Agile Contracts
Agile ContractsAgile Contracts
Agile Contracts
Lars Thorup
 
High Performance Software Engineering Teams
High Performance Software Engineering TeamsHigh Performance Software Engineering Teams
High Performance Software Engineering Teams
Lars Thorup
 
Elephant Carpaccio
Elephant CarpaccioElephant Carpaccio
Elephant Carpaccio
Lars 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 QUnit
Lars Thorup
 
Introduction to Automated Testing
Introduction to Automated TestingIntroduction to Automated Testing
Introduction to Automated Testing
Lars 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

Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
Claudio Di Ciccio
 
Things to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUUThings to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUU
FODUU
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
kumardaparthi1024
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
Jason Packer
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
David Brossard
 
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdfAI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
Techgropse Pvt.Ltd.
 

Recently uploaded (20)

Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”“I’m still / I’m still / Chaining from the Block”
“I’m still / I’m still / Chaining from the Block”
 
Things to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUUThings to Consider When Choosing a Website Developer for your Website | FODUU
Things to Consider When Choosing a Website Developer for your Website | FODUU
 
GenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizationsGenAI Pilot Implementation in the organizations
GenAI Pilot Implementation in the organizations
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024Columbus Data & Analytics Wednesdays - June 2024
Columbus Data & Analytics Wednesdays - June 2024
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
OpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - AuthorizationOpenID AuthZEN Interop Read Out - Authorization
OpenID AuthZEN Interop Read Out - Authorization
 
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdfAI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
AI-Powered Food Delivery Transforming App Development in Saudi Arabia.pdf
 

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