Toggle Menu

Insights > Tech Tips > How Can WordPress Developers Do Behavior-Driven Development (BDD)?

How Can WordPress Developers Do Behavior-Driven Development (BDD)?

I was recently invited to speak at a WordPress in Government event.  The subject of my short presentation was Behavior-Driven WordPress Development.  Over the years I’ve learned that PHP has a bad reputation for rambling, sloppy spaghetti code.  PHP is a powerful language I’ve come to terms with over the years, but I definitely understand […]

By

June 28, 2013

I was recently invited to speak at a WordPress in Government event.  The subject of my short presentation was Behavior-Driven WordPress Development.  Over the years I’ve learned that PHP has a bad reputation for rambling, sloppy spaghetti code.  PHP is a powerful language I’ve come to terms with over the years, but I definitely understand the argument.  While the WordPress community has some QA standards, they are not exempt from the broader problem.

Since joining Excella I’ve had the opportunity to work with some exceptional, professional developers who expose me to best practices like behavior-driven development (BDD) which might change that bad reputation, or at least make us WordPress developers much more productive.

Skeptical?  Don’t be!  Consider BDD one more tool in your WordPress toolbox.  Let’s start at the beginning…

So what is BDD anyway?

Behavior-driven development is a common practice in many programming communities where developers write and test software against the way it will actually be used.  For example, in my current work I’m extending WordPress to quickly switch custom post types and taxonomies on and off.  Without behavior testing, I would write some code, push it to my development server, hit refresh on the page, fill in the long form manually and then refresh a few different pages to see if it worked.  It’s a lot of keystrokes, clicking, and manual entry.  Behavior testing automates that whole process.

Here is an example of how to approach BDD using a few tools I like.

Step 1: Write the Test

Behavior testing involves a simple language called Gherkin that interfaces with the Python programs behave, Selenium, and a Chrome WebDriver to run tests like this:

Feature:  A simple About page

As a member of the public

I want to know more information about who runs this website

@about_link_test

Scenario:  Test the About link

Given I visit the homepage

When I click the “About” link

Then I should be directed to the “About” page

Step 2: Run the Test (Grab a coffee)

If the feature I’m testing is whether the “About” link works properly, I just type behave -t=about_link_test into my terminal and behave opens Google Chrome to the homepage, sends a click to the “About” link, then watches whether the right page loaded and then quits.  Simple.  In the terminal, behave also tells you what it’s doing.

@about_link_test

Scenario:  Test the About link

Given I visit the homepage….passed

When I click the “About link”….passed

Then I should be directed to the “About” page

Step 3:  Evaluate and (maybe) Run Again

If the test was successful, the terminal will print out something like:

1 feature tested

1 scenario passed

3 steps passed

As the project goes deeper and further, the work you do down the line may break something you did earlier.  BDD lets you keep the old tests as you write the new ones so you don’t forget, or neglect, to test the whole project, instead of just the current feature.  When I was running the test, I had three content types I wanted to add and I wanted to test whether a content type could be deactivated and reactivated so I taught behave how to log into WordPress, find the plugin, and then activate and deactivate the content types.

Aside from the convenience of letting your test run on autopilot, it also highlights failures, allowing you to isolate bugs faster and see exactly what went wrong.  This test, combined with a close watch on the http and MySQL error logs, dramatically improved my development time and helped me find unexpected bugs a lot more quickly.

For more information about how to get your own BDD suite set up, check out Selenium, its web browser drivers, and behave.  Exact installation procedures will vary depending on your development environment, but installation instructions are available via each package’s webpage:

How do you use behavior-driven development?  Please share your experiences with us in the comments below!

You Might Also Like

Professional Development

Inspecting and Adapting Myself

“Between stimulus and response, there is a space. In that space is our power to...

Agile Transformation

9 Methods to Get More from Your Agile Adoption

Getting started with Agile is straightforward; succeeding with it is challenging. You can’t just introduce...

Agile Transformation

9 Signs You’re Struggling with Agile Adoption

Your team adopted Agile to get better—to accelerate delivery and improve performance—but now that you’ve...