Budapest, 26-28 October 2016
PROPERTY BASED BDD EXAMPLES
Presented by Gaspar Nagy
© All rights reserved
gaspar@specsolutuions.eu
@gasparnagy, http://gasparnagy.com
BDD PBT
• Behavior Driven
Development
• ~Specification by
Example (SbE)
• ~Acceptance Test Driven
Development (ATDD)
• ~Keyword Driven Testing
2 © All rights reserved
• Property Based Testing
• Property Testing
• ~Random Testing
• ~Model-based Testing
Scenario: Correct easy answer scores 10
Given I register a team
When I submit a correct easy answer
Then my score should be 10
Collaboration
Automation with
Cucumber/SpecFlow
What is BDD?
This is an example!
5 © All rights reserved
Scenario: Correct easy answer scores 10
Given I register a team
When I submit a correct easy answer
Then my score should be 10
How many examples you need?
6 © All rights reserved
Scenario: Add two numbers
Given I have entered 1 into the calculator
And I have entered 2 into the calculator
When I press add
Then the result should be 3 on the screen
Scenario: Add two numbers
Given I have entered 5 into the calculator
And I have entered -7 into the calculator
When I press add
Then the result should be -2 on the screen
Scenario: Add two numbers
Given I have entered 2 into the calculator
And I have entered 0 into the calculator
When I press add
Then the result should be 2 on the screen
BDD is for…
understanding & validating
business requirements
through illustrative examples
8 © All rights reserved
PBT is for…
verifying
implementation
through checking statements about the
output for many different possible inputs
9 © All rights reserved
source: http://blog.jessitron.com/2013/04/property-based-testing-what-is-it.html
the “properties”
For example for addition…
• Commutative property: a + b = b + a
• Associative property: (a + b) + c = a + (b + c)
• Identity property: a + 0 = a
• Distributive property: a * (b + c) = a*b + a*c
We would like to verify these for ~ALL input
combinations!
10 © All rights reserved
There is a tool for doing this!
• QuickCheck (Haskell) is the canonical framework, but
there are many different ports of it to other
programming languages
• QuickCheck for Java
• PhpQuickCheck for PHP
• ScalaCheck for Scala
• FsCheck for .NET (F#, C#)
• … (see more at https://en.wikipedia.org/wiki/QuickCheck)
11 © All rights reserved
FsCheck Sample
12 © All rights reserved
When the implementation is wrong…
13 © All rights reserved
BDD PBT
• 1 + 2 = 3
• 5 + -7 = -2
• 2 + 0 = 2
14 © All rights reserved
• Commutative
• Associative
• Identity
• Distributive
Examples in SpecFlow
15 © All rights reserved
Identity property BDD style…
16 © All rights reserved
Defining constraints and expectations…
17 © All rights reserved
More real life examples…
18 © All rights reserved
Summary
• BDD turns examples into automated tests
• PBT automates rules with many different input
• The power of this two can be combined to achieve an
executable specification
• The BDD and the PBT tools can work together for this
• See http://github.com/gasparnagy/SpecFlow.FsCheck
19 © All rights reserved
QUESTIONS?
gaspar@specsolutuions.eu
@gasparnagy
http://gasparnagy.com
© All rights reserved
Special thanks to
• Ciaran McNulty
• Konstantin Kudryashov

Property Based BDD Examples (ETSI UCAAT 2016, Budapest)

  • 1.
    Budapest, 26-28 October2016 PROPERTY BASED BDD EXAMPLES Presented by Gaspar Nagy © All rights reserved gaspar@specsolutuions.eu @gasparnagy, http://gasparnagy.com
  • 2.
    BDD PBT • BehaviorDriven Development • ~Specification by Example (SbE) • ~Acceptance Test Driven Development (ATDD) • ~Keyword Driven Testing 2 © All rights reserved • Property Based Testing • Property Testing • ~Random Testing • ~Model-based Testing
  • 3.
    Scenario: Correct easyanswer scores 10 Given I register a team When I submit a correct easy answer Then my score should be 10 Collaboration Automation with Cucumber/SpecFlow What is BDD?
  • 4.
    This is anexample! 5 © All rights reserved Scenario: Correct easy answer scores 10 Given I register a team When I submit a correct easy answer Then my score should be 10
  • 5.
    How many examplesyou need? 6 © All rights reserved Scenario: Add two numbers Given I have entered 1 into the calculator And I have entered 2 into the calculator When I press add Then the result should be 3 on the screen Scenario: Add two numbers Given I have entered 5 into the calculator And I have entered -7 into the calculator When I press add Then the result should be -2 on the screen Scenario: Add two numbers Given I have entered 2 into the calculator And I have entered 0 into the calculator When I press add Then the result should be 2 on the screen
  • 6.
    BDD is for… understanding& validating business requirements through illustrative examples 8 © All rights reserved
  • 7.
    PBT is for… verifying implementation throughchecking statements about the output for many different possible inputs 9 © All rights reserved source: http://blog.jessitron.com/2013/04/property-based-testing-what-is-it.html the “properties”
  • 8.
    For example foraddition… • Commutative property: a + b = b + a • Associative property: (a + b) + c = a + (b + c) • Identity property: a + 0 = a • Distributive property: a * (b + c) = a*b + a*c We would like to verify these for ~ALL input combinations! 10 © All rights reserved
  • 9.
    There is atool for doing this! • QuickCheck (Haskell) is the canonical framework, but there are many different ports of it to other programming languages • QuickCheck for Java • PhpQuickCheck for PHP • ScalaCheck for Scala • FsCheck for .NET (F#, C#) • … (see more at https://en.wikipedia.org/wiki/QuickCheck) 11 © All rights reserved
  • 10.
    FsCheck Sample 12 ©All rights reserved
  • 11.
    When the implementationis wrong… 13 © All rights reserved
  • 12.
    BDD PBT • 1+ 2 = 3 • 5 + -7 = -2 • 2 + 0 = 2 14 © All rights reserved • Commutative • Associative • Identity • Distributive
  • 13.
    Examples in SpecFlow 15© All rights reserved
  • 14.
    Identity property BDDstyle… 16 © All rights reserved
  • 15.
    Defining constraints andexpectations… 17 © All rights reserved
  • 16.
    More real lifeexamples… 18 © All rights reserved
  • 17.
    Summary • BDD turnsexamples into automated tests • PBT automates rules with many different input • The power of this two can be combined to achieve an executable specification • The BDD and the PBT tools can work together for this • See http://github.com/gasparnagy/SpecFlow.FsCheck 19 © All rights reserved
  • 18.
    QUESTIONS? gaspar@specsolutuions.eu @gasparnagy http://gasparnagy.com © All rightsreserved Special thanks to • Ciaran McNulty • Konstantin Kudryashov