Behaviour Driven Development in WordPress

slid.es/tmf/bdd-wp

Automated Tests in WordPress

Unit Tests

  • 1 test per "unit"
    most common example: Calculator
  • prepare testing environment
  • set up your object (i.e. instantiation)
  • mock dependencies
  • execute
  • assert result

WordPress & Dependencies

lots of globals, exits and more bad practices, that make testing hard...

Calculator

?

...but how do I test my website?

A more suitable testing approach: BDD

  • Dan North introduced BDD in 2006 
  • TDD problems: where should we start, what and
    how should we test?
  • Describe the features in a common language
    understood by all stakeholders: clients, PMs, Devs...
  • Prioritize features by business value
  • Describe the desired and undesired behaviours of these features with scenarios (examples)
  • Use these stories as a recipe for automated tests
  • Implement the features

Gherkin

The common language for

describing features and scenarios

Feature: Read published blog posts
    In order to published read blog posts
    As a reader
    I need to be able to see a list of published posts and read each post individually

    Background:
        Given there are posts
            | post_title      | post_content              | post_status | post_author |
            | Just my article | The content of my article | publish     | 1           |
            | My draft        | This is just a draft      | draft       | 1           |

    Scenario: List my blog posts
        Given I am on the homepage
        Then I should see "Just my article"
        And I should not see "My draft"

    Scenario: Read a blog post
        Given I am on the homepage
        When I follow "Just my article"
        Then I should see "The content of my article"

Take these features written in Gherkin and try to execute the scenarios as tests 

Common steps can be written in reusable context classes

Mink: browser abstraction

Missing Link: WordPress Extension

A Behat Extension for common WordPress tasks in feature descriptions

Demo

Read more:

Thanks!

@tforrer

github.com/tmf

slid.es/tmf/bdd-wp

Made with Slides.com