56
Evolve your coding with a little BDD

Evolve your coding with some BDD

Embed Size (px)

Citation preview

Evolve your coding with a little BDD

WHO AM I?

• Luis Majano

• Computer Engineer

• Imported from El Salvador

• CEO of Ortus Solutions

• Creator of all things Box

Why you don’t testWhy Test

TDDBDD

Getting jiggy with it

Where to start?What to test?

What not to test?

WHY PEOPLE DON’T TEST

BIGGEST LIE IN SOFTWARE DEV

Don’t worry, we will create the tests and

refactor it later!

Cost To Fix Time detected Requirements Design Building Testing Post-Release

Time Introduced

Requirements 1x 3x 5-10x 10x 10-100x

Design --- 1x 10x 15x 25-100x

Building -- -- 1x 10x 10-25x

^ Kaner, Cem; James Bach, Bret Pettichord (2001). Lessons Learned in Software Testing: A Context-Driven Approach. Wiley. p. 4. ISBN 0-471-08112-4.

BUGS COST MONEY

MANUAL TESTING

What is T.D.D

Test-driven development (TDD) is a software development process that relies on the repetition of a

very short development cycle: first the developer writes an (initially failing) automated test case that defines a desired improvement or new function,

then produces the minimum amount of code to pass that test, and finally refactors the new code to

acceptable standards

T.D.D. Process

Write Test

Mock

Write Code Verify

Refactor

T.D.D.

CFC

Validate()

Add()

List()

Save() Test%CFC%

testValidate()%

testAdd()%

testList()%

testSave()%

T.D.D. IS…

• Exercises your code• Very Developer Focused• Developer has immediate feedback• Create tests before rather than after (yea right!)• Express some behavior and ideas• Creates some documentation

T.D.D. IS NOT…

• About verifying software requirements• Easy• Let’s be truthful, TDD can be a pain in the

buttocks!• We start strong, but we finish weak, even if we

finishDeveloper Test

Paralysis

What is B.D.D.?

In software engineering, behavior-driven development (BDD) is a software development process based on test-driven development (TDD). Behavior-driven development combines the general techniques and principles of TDD with ideas from domain-driven design and object-oriented analysis and design to provide software developers and business analysts with shared tools and a shared process to collaborate on software development, with the aim of delivering "software that matters"

B.D.D is T.D.D. Evolved

• Dan North - http://dannorth.net/introducing-bdd/• Ubiquitous language

• existing or being everywhere at the same time :  constantly

• Promotes communication & collaboration between • Developers + business analysts + stakeholders

• Focuses on stories or requirements rather than on functions

• Focuses on what a system should do and not on how it should be implemented• Better readability and visibility• Verify that software works but also that it meets customer expectations

B.D.D. Process

Stories

Scenario Specs

Implement Behavior Verify

Refactor

Questions Answered

Where%to%start?%

Outside%/>%In%

What%to%test?%

User%Stories%

What%Not%to%test?%

Anything%Else%

Story Framework

Story Scenario Spec

Stories to Scenarios

As an application user I want to be welcomed with my name at login

in order to personalize my experience

Scenario: user login with valid credentials Given a user “luis" with password "secret" exists

When I login as “luis" with "secret" Then I should see the message "Welcome back luis!"

Stories to Scenarios

+Scenario 1: Account is in credit+Given the account is in creditAnd the card is validAnd the dispenser contains cashWhen the customer requests cashThen ensure the account is debitedAnd ensure cash is dispensedAnd ensure the card is returned

Stories to Scenarios

Feature: Box SizeIn order to know what size box I needAs a distribution managerI want to know the volume of the box

Scenario: Get box volumeGiven I have entered a width of 20And a height of 30And a depth of 40When I run the calculationThen the result should be 24000

Stories to Scenarios

Stories to Scenarios

Gherkins

Story to Scenario to TestBox

As an application user

I want to be welcomed with my name at login

in order to personalize my experience

Scenario: user login with valid credentials

Given a user “luis" with password "secret" exists

When I login as “luis" with "secret"

Then I should see the message "Welcome back luis!"

describe( “User login with valid credentials”, function(){ it( “should see a personalized message”, function(){

userService.login( “luis”, “secret” ) var event = execute(“user.home”) expect( event.getValue( “welcome” ) ).toBe( “Welcome back luis!” )

}) })

Story to Scenario to TestBox

As an application userI want to be welcomed with my name at loginin order to personalize my experience

Scenario: user login with valid credentialsGiven a user “luis" with password "secret" existsWhen I login as “luis" with "secret"Then I should see the message "Welcome back luis!"

scenario( “User login with valid credentials”, function(){given( “A user Luis with password secret exists”, function(){

when( “I login as ‘luis’ and ‘secret’”, function(){then( “I should see a message ‘Welcome back luis’”, function(){expect( event.getValue( “welcome” ) ).toBe( “Welcome back luis!” )

});});});});

Story to Scenario to TestBox

feature( "Box Size", function(){

describe( "In order to know what size box I needAs a distribution managerI want to know the volume of the box", function(){

scenario( "Get box volume", function(){given( "I have entered a width of 20

And a height of 30And a depth of 40", function(){when( "I run the calculation", function(){

then( "the result should be 24000", function(){expect( myObject.myFunction(20,30,40) ).toBe( 24000 );

});});});});}););

Story to Scenario to TestBox

story("As a distribution manager, I want to know the volume of the box I need", function() {

given("I have a width of 20And a height of 30And a depth of 40", function() {when("I run the calculation", function() {

then("the result should be 24000", function() {expect(myObject.myFunction(20,30,40)).toBe(24000);

});});

});});

• BDD & xUnit style testing• MXUnit Compatible• Life-Cycle methods• MockBox Integration• Asynchronous Testing

• ANT/Jenkins Integration• Custom Reporters & Runners• Dynamic Labels & Skipping• Debug Output Streams

REQUIREMENTS

• ColdFusion 9.01+, Lucee 4+

• xUnit + MXUnit Compatibility

• ColdFusion 10+, Lucee 4+

• xUnit, MXUnit, BDD

INSTALLATION

• CommandBox

• box install testbox —saveDev

• box install testbox-be —saveDev

• Manual download and create mapping:

TESTING STYLES

xUnit TDD

Unit Focused

Function Focused

Asserts

BDD Test Scenarios

Spec Focused

Nested Scenarios

Expectations

MXUNIT COMPATIBLE

• Compatible with xUnit style by MXUnit

• Migrate existing tests to TestBox

• No BDD

• How do you migrate?

• If something is not working, report it: [email protected]

What you get! API Docs

Core

Test Browser

Test Harness

Test Runner

Samples

Test HarnessAutomated test

results!

xUnit/BDD Test Bundles

Harness bootstrap

HTML Runner

ANT Runner

TEST BUNDLE CFC

• Inherits from testbox.system.BaseSpec

• TesBox will then execute all tests within 1 or more bundles

RUNNING YOUR BUNDLES

• Execute bundle via the URL• http://mysite/tests/bundle.cfc?method=runRemote

• SOAP Runner• HTTP/REST Runner• ANT Runner• NodeJS Runner• CommandBox Runner• Custom Runner

REPORTERS• ANTJunit : A specific variant of JUnit XML that works with the ANT junitreport task

• Codexwiki : Produces MediaWiki syntax for usage in Codex Wiki

• Console : Sends report to console

• Doc : Builds semantic HTML to produce nice documentation

• Dot : Builds an awesome dot report

• JSON : Builds a report into JSON

• JUnit : Builds a JUnit compliant report

• Raw : Returns the raw structure representation of the testing results

• Simple : A basic HTML reporter

• Text : Back to the 80's with an awesome text report

• XML : Builds yet another XML testing report

• Tap : A test anything protocol reporter

• Min : A minimalistic view of your test reports

TEST BUNDLE CFC

• run()

• Declare your scenario specs + suites

• Life-cycle methods

• beforeAll(), afterAll()

• Expectations Library: expect()

• Assertions Library: $assert

SUITES: DESCRIBE() YOUR TESTS

• Suites begin with a describe() block• title• closure

• A suite is composed of specs or more suites• Closures can contain

• Life-cycle methods• More suites• Specs

DESCRIBE() ALIASES

•story()• feature()•given()•when()• scenario()

feature( "Box Size", function(){

describe( "In order to know what size box I needAs a distribution managerI want to know the volume of the box", function(){

scenario( "Get box volume", function(){given( "I have entered a width of 20

And a height of 30And a depth of 40", function(){when( "I run the calculation", function(){

then( "the result should be 24000", function(){expect( myObject.myFunction(20,30,40) ).toBe( 24000 );

});});});});}););

SPECS: IT()

• At least 2 args• Title • Closure • Labels• Skip

• Closure is where you:• Code testing scenarios• 1+ expectations or assertions

• Specs tested in order declared

IT() ALIASES

• it()• then()

feature( "Box Size", function(){

describe( "In order to know what size box I needAs a distribution managerI want to know the volume of the box", function(){

scenario( "Get box volume", function(){given( "I have entered a width of 20

And a height of 30And a depth of 40", function(){when( "I run the calculation", function(){

then( "the result should be 24000", function(){expect( myObject.myFunction(20,30,40) ).toBe( 24000 );

});});});});}););

EXPECTATIONS

• Self-concatenated method calls that evaluate your SUT• Start with a call to expect( actual ) • Concatenated with matcher methods• Matchers also have negation (not) counterparts

NESTED SUITES

• Life-cycle methods bubble up• Great for grouping and recursive scenarios• Execute in descending order

EXPECTING EXCEPTIONS

• Verifies exceptions• Pass a closure to expect() and use toThrow( type, regex )• Match types and message+detail regex

AUTOMATION

Lots of TestBox Free Trainingwww.ortussolutions.com/products/testbox

Free eBooks

Lots BDD Examples: ColdBox, Couchbase SDK, TestBox

Professional Training

Thanks!

Q & A