SlideShare a Scribd company logo
1 of 50
Download to read offline
Behaviour Driven Development
  and thinking about testing



           Malcolm Tredinnick
      malcolm.tredinnick@gmail.com
Why have I asked you here today?


     1) Testing landscape
     2) A different way to think about this
     3) Code
Chapter I: The Testing Landscape
Isolated   Integrated
Isolated                           Integrated


        Testing unit            External service
           size?                  availability
Mocks
                       Running time?
Mocks
●   Substitute for external systems & components
●   Need to be injected into the tests
    ●   “Monkey patching”
    ●   Dependency injection
Monkey patching


import socket

def setUp(...):
   socket.socket = DummySocketObject
   ...
Mocks
●   Substitute for external systems & components
●   Need to be injected into the tests
    ●   “Monkey patching”
    ●   Dependency injection
●   Need to be kept in synch with reality!
Isolated                    Integrated




           Infrastructure
ce
                                     n
                                e na
                             nt
                             ai
                            M
Isolated                           Integrated




           Infrastructure
Testing Strategies




   Some of many...
Testing Strategies
●   Formal verification
Testing Strategies
●   Formal verification
●   Test everything you can think of
Testing Strategies
●   Formal verification
●   Test everything you can think of
●   Anti-regression suite
      No bug fix without a test
Testing Strategies
●   Formal verification
●   Test everything you can think of
●   Anti-regression suite
●   Test driven development (TDD)
TDD


1.Write a (failing) test for next method or class
2.Write minimal code to make test pass
3.Rinse, wash, repeat.
Testing Strategies
●   Formal verification
●   Test everything you can think of
●   Anti-regression suite
●   Test driven development (TDD)
      “Specification, not verification”
Testing Strategies
●   Formal verification
●   Test everything you can think of
●   Anti-regression suite
●   Test driven development (TDD)
●   Documentation driven development (DDD?)
Testing Strategies
●   Formal verification
●   Test everything you can think of
●   Anti-regression suite
●   Test driven development (TDD)
●   Documentation driven development (DDD?)
●   Behaviour driven development (BDD)
Chapter II: A different way of thinking
Interlude: Creating systems...
Interlude: Creating systems...



          Specification
Interlude: Creating systems...

        Specification




                    Implementation

         Testing
Specification

●   On (e-)paper?
    ●   quick sketch
    ●   formal requirements document
    ●   mailing list discussion
●   In your head?
Specification



●   Capturing the idea is probably good
●   “What was I thinking smoking when...?”
Specification


As a [user or role],

I want [feature]

so that [something happens]
Specification


As a maze creator,

I want to be able to specify dimensions

so that a new maze of the given size is created.
BDD



What is the next most important
thing the code should do?
BDD



     What is the next most important
     thing the code should do?



Be able to
test this...              ... then implement it.
Scenario


Given that [initial context],

if and when [some event occurs]

then [ensure some outcomes]
Use case versus scenario


As a [user or role],          Given that [initial context],

I want [feature]              if and when [some event occurs]

so that [something happens]   then [ensure some outcomes]
Chapter III: Concrete code
Interlude: Naming things


 Language helps guide our brain.
 Focuses the mind.
 Perhaps too exclusively.
Interlude: Naming things


The Sapir-Whorf Hypothesis:
“[...] the structure of a language affects the ways in
which its speakers are able to conceptualise their
world [view].”
Example #1: Python's unittest module
Test titles
class BasicFieldTests (TestCase):
  def test_field_name (self):
    ...


  def test_show_hidden_initial (self):
    ...
Test titles
class Fields (TestCase):
  def should_allow_name_override (self):
     ...


class ChoiceFields (TestCase):
  def should_permit_initial_values_in_hidden_widgets (self):
     ...
Run new test titles


Write a load_test() method to pull out
all the “should” methods.

(Easy enough, but a few lines of code.)
Matching to scenarios
●   Typically, end up creating more classes than
    unittests (not a bad thing; classes are “free”).
    ●   Thinking about test names helps with class names
        and roles.
●   Context setup is done in setUp() method.
●   Normal unittest assertions work fairly cleanly.
Example #2: Doctests
Narrative style testing
Jane created a new maze with a small (5 x 5) size.

>>> from maze import Maze
>>> maze = Maze(5, 5)

Spot, her dog, did not think the new maze was the right size, but
Jane was able to convince him like this:

>>> maze.x_size
5
>>> maze.y_size
5

They found the entry and exit locations in their new maze:

>>>maze.entry_cell
(0, 0, “left”)

(etc)
Narrative style testing
●   Excellent if you are truly writing a narrative.
●   Can be difficult to maintain (but not always).
●   Avoid if large amounts of setup required.
●   Use with caution. Do use. Do not abuse.
Example #3: Domain specific languages
DSL packages



●   Lettuce
●   Freshen


                  Both available through PyPI
Freshen example


Scenario: Divide regular numbers
  Given I have entered 3 into the calculator
  And I have entered 2 into the calculator
  When I press divide
  Then the result should be 1.5 on the screen




                        (Code samples from Freshen documentation)
Backing code

from freshen import *

import calculator

@Before
def before(unused):
    scc.calc = calculator.Calculator()
    scc.result = None

@Given("I have entered (d+) into the calculator")
def enter(num):
    scc.calc.push(int(num))


                         (Code samples from Freshen documentation)
Backing code


@When("I press (w+)")
def press(button):
    op = getattr(scc.calc, button)
    scc.result = op()

@Then("the result should be (.*) on the screen")
def check_result(value):
    assert_equal(str(scc.result), value)




                        (Code samples from Freshen documentation)
Freshen templated example
Scenario Outline: Add two numbers
  Given I have entered <input_1> into the calculator
  And I have entered <input_2> into the calculator
  When I press <button>
  Then the result should be <output> on the screen


Examples:
  | input_1 | input_2 | button | output |
  | 20      | 30      | add    | 50         |
  | 2       | 5       | add    | 7          |
  | 0       | 40      | add    | 40         |


                          (Code samples from Freshen documentation)
Conclusions(?)
●   Behavioural tests are worth trying
●   Question the way you think from time to time
●   Are your tests' purpose clear to future you?
https://github.com/malcolmt/bdd-and-testing-talk




            malcolm.tredinnick@gmail.com
                     @malcolmt

More Related Content

What's hot

Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
05 junit
05 junit05 junit
05 junitmha4
 
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unitOlga Extone
 
Tdd pecha kucha_v2
Tdd pecha kucha_v2Tdd pecha kucha_v2
Tdd pecha kucha_v2Paul Boos
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done RightBrian Fenton
 
Journey's diary developing a framework using tdd
Journey's diary   developing a framework using tddJourney's diary   developing a framework using tdd
Journey's diary developing a framework using tddeduardomg23
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)vilniusjug
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Xp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And MocksXp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And Mocksguillaumecarre
 

What's hot (20)

JUNit Presentation
JUNit PresentationJUNit Presentation
JUNit Presentation
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
Unit testing
Unit testingUnit testing
Unit testing
 
Working Effectively with Legacy Code
Working Effectively with Legacy CodeWorking Effectively with Legacy Code
Working Effectively with Legacy Code
 
Java Unit Testing
Java Unit TestingJava Unit Testing
Java Unit Testing
 
05 junit
05 junit05 junit
05 junit
 
Introduction To J unit
Introduction To J unitIntroduction To J unit
Introduction To J unit
 
Tdd pecha kucha_v2
Tdd pecha kucha_v2Tdd pecha kucha_v2
Tdd pecha kucha_v2
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Journey's diary developing a framework using tdd
Journey's diary   developing a framework using tddJourney's diary   developing a framework using tdd
Journey's diary developing a framework using tdd
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
JUnit 5
JUnit 5JUnit 5
JUnit 5
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
JUnit Presentation
JUnit PresentationJUnit Presentation
JUnit Presentation
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Xp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And MocksXp Day 080506 Unit Tests And Mocks
Xp Day 080506 Unit Tests And Mocks
 

Similar to Behaviour Driven Development and Thinking About Testing

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
 
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
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With PytestEddy Reyes
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Gianluca Padovani
 
Unit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityUnit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityVictor Rentea
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
Testing Django Applications
Testing Django ApplicationsTesting Django Applications
Testing Django ApplicationsHonza Král
 
Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaSoftware Guru
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionDionatan default
 
It's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecIt's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecGiulio De Donato
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...DevDay.org
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 

Similar to Behaviour Driven Development and Thinking About Testing (20)

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
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
TDD in Python With Pytest
TDD in Python With PytestTDD in Python With Pytest
TDD in Python With Pytest
 
Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)Tdd is not about testing (C++ version)
Tdd is not about testing (C++ version)
 
Tdd in practice
Tdd in practiceTdd in practice
Tdd in practice
 
Unit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of PurityUnit Testing like a Pro - The Circle of Purity
Unit Testing like a Pro - The Circle of Purity
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Python and test
Python and testPython and test
Python and test
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Testing Django Applications
Testing Django ApplicationsTesting Django Applications
Testing Django Applications
 
Test Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y GomegaTest Driven Development en Go con Ginkgo y Gomega
Test Driven Development en Go con Ginkgo y Gomega
 
Unit testing
Unit testingUnit testing
Unit testing
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
Tdd is not about testing
Tdd is not about testingTdd is not about testing
Tdd is not about testing
 
It's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspecIt's all about behaviour, also in php - phpspec
It's all about behaviour, also in php - phpspec
 
Debugging
DebuggingDebugging
Debugging
 
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
[DevDay 2016] Real Unit Testing with mocking framework - Speaker: Phat Vu – S...
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 

More from dn

Code quality; patch quality
Code quality; patch qualityCode quality; patch quality
Code quality; patch qualitydn
 
How does this code work?
How does this code work?How does this code work?
How does this code work?dn
 
Python worst practices
Python worst practicesPython worst practices
Python worst practicesdn
 
Struggling to find an open source business model
Struggling to find an open source business modelStruggling to find an open source business model
Struggling to find an open source business modeldn
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach placesdn
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyonddn
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Pythondn
 
Spotlight on Python
Spotlight on PythonSpotlight on Python
Spotlight on Pythondn
 

More from dn (8)

Code quality; patch quality
Code quality; patch qualityCode quality; patch quality
Code quality; patch quality
 
How does this code work?
How does this code work?How does this code work?
How does this code work?
 
Python worst practices
Python worst practicesPython worst practices
Python worst practices
 
Struggling to find an open source business model
Struggling to find an open source business modelStruggling to find an open source business model
Struggling to find an open source business model
 
Testing in those hard to reach places
Testing in those hard to reach placesTesting in those hard to reach places
Testing in those hard to reach places
 
Automated testing in Python and beyond
Automated testing in Python and beyondAutomated testing in Python and beyond
Automated testing in Python and beyond
 
Patterns in Python
Patterns in PythonPatterns in Python
Patterns in Python
 
Spotlight on Python
Spotlight on PythonSpotlight on Python
Spotlight on Python
 

Recently uploaded

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfRankYa
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 

Recently uploaded (20)

Search Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdfSearch Engine Optimization SEO PDF for 2024.pdf
Search Engine Optimization SEO PDF for 2024.pdf
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 

Behaviour Driven Development and Thinking About Testing

  • 1. Behaviour Driven Development and thinking about testing Malcolm Tredinnick malcolm.tredinnick@gmail.com
  • 2. Why have I asked you here today? 1) Testing landscape 2) A different way to think about this 3) Code
  • 3. Chapter I: The Testing Landscape
  • 4. Isolated Integrated
  • 5. Isolated Integrated Testing unit External service size? availability Mocks Running time?
  • 6. Mocks ● Substitute for external systems & components ● Need to be injected into the tests ● “Monkey patching” ● Dependency injection
  • 7. Monkey patching import socket def setUp(...): socket.socket = DummySocketObject ...
  • 8. Mocks ● Substitute for external systems & components ● Need to be injected into the tests ● “Monkey patching” ● Dependency injection ● Need to be kept in synch with reality!
  • 9. Isolated Integrated Infrastructure
  • 10. ce n e na nt ai M Isolated Integrated Infrastructure
  • 11. Testing Strategies Some of many...
  • 12. Testing Strategies ● Formal verification
  • 13. Testing Strategies ● Formal verification ● Test everything you can think of
  • 14. Testing Strategies ● Formal verification ● Test everything you can think of ● Anti-regression suite No bug fix without a test
  • 15. Testing Strategies ● Formal verification ● Test everything you can think of ● Anti-regression suite ● Test driven development (TDD)
  • 16. TDD 1.Write a (failing) test for next method or class 2.Write minimal code to make test pass 3.Rinse, wash, repeat.
  • 17. Testing Strategies ● Formal verification ● Test everything you can think of ● Anti-regression suite ● Test driven development (TDD) “Specification, not verification”
  • 18. Testing Strategies ● Formal verification ● Test everything you can think of ● Anti-regression suite ● Test driven development (TDD) ● Documentation driven development (DDD?)
  • 19. Testing Strategies ● Formal verification ● Test everything you can think of ● Anti-regression suite ● Test driven development (TDD) ● Documentation driven development (DDD?) ● Behaviour driven development (BDD)
  • 20. Chapter II: A different way of thinking
  • 23. Interlude: Creating systems... Specification Implementation Testing
  • 24. Specification ● On (e-)paper? ● quick sketch ● formal requirements document ● mailing list discussion ● In your head?
  • 25. Specification ● Capturing the idea is probably good ● “What was I thinking smoking when...?”
  • 26. Specification As a [user or role], I want [feature] so that [something happens]
  • 27. Specification As a maze creator, I want to be able to specify dimensions so that a new maze of the given size is created.
  • 28. BDD What is the next most important thing the code should do?
  • 29. BDD What is the next most important thing the code should do? Be able to test this... ... then implement it.
  • 30. Scenario Given that [initial context], if and when [some event occurs] then [ensure some outcomes]
  • 31. Use case versus scenario As a [user or role], Given that [initial context], I want [feature] if and when [some event occurs] so that [something happens] then [ensure some outcomes]
  • 33. Interlude: Naming things Language helps guide our brain. Focuses the mind. Perhaps too exclusively.
  • 34. Interlude: Naming things The Sapir-Whorf Hypothesis: “[...] the structure of a language affects the ways in which its speakers are able to conceptualise their world [view].”
  • 35. Example #1: Python's unittest module
  • 36. Test titles class BasicFieldTests (TestCase): def test_field_name (self): ... def test_show_hidden_initial (self): ...
  • 37. Test titles class Fields (TestCase): def should_allow_name_override (self): ... class ChoiceFields (TestCase): def should_permit_initial_values_in_hidden_widgets (self): ...
  • 38. Run new test titles Write a load_test() method to pull out all the “should” methods. (Easy enough, but a few lines of code.)
  • 39. Matching to scenarios ● Typically, end up creating more classes than unittests (not a bad thing; classes are “free”). ● Thinking about test names helps with class names and roles. ● Context setup is done in setUp() method. ● Normal unittest assertions work fairly cleanly.
  • 41. Narrative style testing Jane created a new maze with a small (5 x 5) size. >>> from maze import Maze >>> maze = Maze(5, 5) Spot, her dog, did not think the new maze was the right size, but Jane was able to convince him like this: >>> maze.x_size 5 >>> maze.y_size 5 They found the entry and exit locations in their new maze: >>>maze.entry_cell (0, 0, “left”) (etc)
  • 42. Narrative style testing ● Excellent if you are truly writing a narrative. ● Can be difficult to maintain (but not always). ● Avoid if large amounts of setup required. ● Use with caution. Do use. Do not abuse.
  • 43. Example #3: Domain specific languages
  • 44. DSL packages ● Lettuce ● Freshen Both available through PyPI
  • 45. Freshen example Scenario: Divide regular numbers Given I have entered 3 into the calculator And I have entered 2 into the calculator When I press divide Then the result should be 1.5 on the screen (Code samples from Freshen documentation)
  • 46. Backing code from freshen import * import calculator @Before def before(unused): scc.calc = calculator.Calculator() scc.result = None @Given("I have entered (d+) into the calculator") def enter(num): scc.calc.push(int(num)) (Code samples from Freshen documentation)
  • 47. Backing code @When("I press (w+)") def press(button): op = getattr(scc.calc, button) scc.result = op() @Then("the result should be (.*) on the screen") def check_result(value): assert_equal(str(scc.result), value) (Code samples from Freshen documentation)
  • 48. Freshen templated example Scenario Outline: Add two numbers Given I have entered <input_1> into the calculator And I have entered <input_2> into the calculator When I press <button> Then the result should be <output> on the screen Examples: | input_1 | input_2 | button | output | | 20 | 30 | add | 50 | | 2 | 5 | add | 7 | | 0 | 40 | add | 40 | (Code samples from Freshen documentation)
  • 49. Conclusions(?) ● Behavioural tests are worth trying ● Question the way you think from time to time ● Are your tests' purpose clear to future you?
  • 50. https://github.com/malcolmt/bdd-and-testing-talk malcolm.tredinnick@gmail.com @malcolmt