Behavior Driven
Development

with Drupal

Coming up

  1. Why you should do test driven development
  2. How to get into the flow...
  3. ...writing software outside-in with bdd

About me

1. Test Driven
Development

What's TDD?

Test driven development (TDD) is a software development process where you write tests before code

Why tests?

Why develop test driven?

2. Flow

Flow

In the "flow", we're completely immersed in our programming activity when we solve difficult challenges with complex skills

Flow

Flow is the reason
why we love programming

Flow

How can we have more flow experiences in programming?

Mihaly Csikszentmihalyi

Flow conditions

  1. clear and verifiable goals

  2. immediate and relevant feedback

  3. difficult challenge and sufficient skills

=> we know when the goal is achieved

=> we know if we're doing it right

=> we know we can solve the problem

Flow activities

Flow

How can we have more flow experiences in programming?

We need

  1. clear and verifiable goals

    => write tests that describe exactly how our software should work

  2. immediate and relevant feedback

    => run tests to verify if our software works as expected

  3. (difficult challenge and sufficient skills)

TDD & Flow

TDD & Flow

separate

writing test code

from

specifying how the software should work

3. Behavior Driven
Development

BDD process

  1. spec feature
  2. write tests
  3. iterate implementation

Spec feature

User story

As a site member
I want to be welcomed with my name at login
in order to personalize my user experience
As a [system actor]
I want to/I need to [objective]
in order to/so that [rationale]

A good user story is

Spec feature

Scenarios

Given [a precondition]
When [some action by the system actor]
Then [some testable outcome is achieved]
Scenario: user login with valid credentials
  Given a user "alex" with password "secret" exists
  When I login as "alex" with "secret"
  Then I should see the message "Welcome back alex!"

Gherkin

Gherkin is a business readable language that lets you describe software’s behaviour without detailing how that behaviour is implemented.

Scripts vs. specs

bad

When I go to "user/login"
And I enter "alex" into the field "username"
And I enter "secret" into the field "password"
And I click on the button "login"

good

When I login as "alex" with "secret"

Spec feature

User story

As a site member
I want to be welcomed with my name at login
in order to personalize my user experience

Scenarios

Scenario: user login with valid credentials
  Given a user "alex" with password "secret" exists
  When I login as "alex" with "secret"
  Then I should see the message "Welcome back alex!"
  
Scenario: user login with invalid credentials
  Given a user "alex" with password "secret" exists
  When I login as "alex" with "123"
  Then I should not see the message "Welcome back alex!"

BDD process

  1. spec feature
  2. write tests
  3. iterate implementation

Tests

Step

Given a user "[.....REGEXP......]" exists

Step definition

$steps->Given('/^a user "([^"]*)" exists "([^"]*)"$/',
function($world, $name, $pass) {
  $world->drupalCreateNamedUser($name, $pass);  
});

BDD process

  1. spec feature
  2. write tests
  3. iterate implementation

iterate

BDD process

  1. spec feature

  2. write tests
  3. iterate implementation

=> who needs what and why?
=> how should the feature behave?

=> verify if the feature works as specified

=> make the feature work as specified

BDD with Drupal

Modules

TDD/BDD with Drupal

2nd condition of flow

immediate and relevant feedback

=> short test cycles

TDD/BDD with Drupal

median time in Drupal until a test is set up:

20 seconds

=> inacceptably long

SimpleTest Fixture module

BDD with Drupal resources

Thank You

/

#