LEARNING TDD/BDD

Session 4 - Acceptance Testing

Presented by Guyllaume Cardinal for Cogeco Connexion

What we've seen so far

Basics

TDD vs BDD

Benefits of TDD

Coding principles: SOLID, GRASP Single Responsability principle

Code smells

What we've seen so far

Unit Tests

Tests a single unit (class)

Focus on behavior

Mocks are used to isolate what is being tested

What we've seen so far

Integration Tests

Test a feature

Multiple classes interact together

Use abstractions to remove binding to web services and databases

Testing Pyramid

We are now in "user land"

How developers imagine users:

Acceptance tests are basically a user (or a QA person) visiting and interacting with your website and expecting certain things

gIVEN - wHEN - tHEN

The Given-When-Then formula is a template intended to guide the writing of acceptance tests for a User Story:

  • Given some context
  • When some action is carried out
  • Then a particular set of observable consequences should happen

gIVEN - wHEN - tHEN

Feature: Serve coffee
  In order to earn money
  Customers should be able to
  buy coffee at all times

  Scenario: Buy last coffee
    Given there are "1" coffees left in the machine
    And I have deposited "1" dollar
    When I press the "coffee" button
    Then I should be served a coffee

Where's the code?

Scenario: I download a file
  Given I am in a directory "/foo"
<?php
class FeatureContext extends BehatContext
{
    /**
     * @Given /^I am in a directory "([^"]*)"$/
     */
    public function iAmInADirectory($dir)
    {
        if (!file_exists($dir)) {
            mkdir($dir);
        }
        chdir($dir);
    }
}

Writen in plain english (most of the time)

Black box testing

In a real browser

Very, very, high level

Tests the "happy path"

Okay, but hold on. Why english?

Primarily a preference

Allows stakeholders and PO to understand what's tested

Personally: Behat is a good tool

Behat report

WARNING!

Acceptance tests are sloooow

Acceptance tests are very fragile

Acceptance tests can't really test edge cases

Acceptance tests should not care about the underlying system

What to test?

Website loads

CSS and JS loads

Navigation

Critical Path (vital flows of your app)

Anything that can't be covered by Unit or Integration tests

Our full testing Stack

Unit:

Integration:

Acceptance:

Test each class individually

Test whole features and class interaction

Test that the app behaves like the user expects it to

And it's all automatic!

REal world example

cogeco.ca

TDD Lunch and learn - Session 4

By Guyllaume Cardinal

TDD Lunch and learn - Session 4

Presentation done for Cogeco to show the basics of TDD. Now that Unit testing is done, we need some Integration tests to make sure classes properly interact together!

  • 604