Monday, January 30, 2017

Testing Frenzy – Can We BDD Test the Units?

I'll be honest, I don't do much testing. When it's really necessary and I'm working on big enterprise projects, I do, but in general, my personal projects are usually one-man-army proofs of concept, or fixes on already tested apps.

[author_more]

We've done our share of testing posts here at SitePoint, with more coming soon, but I wanted to show you a relatively new testing tool I found that caught my attention because of how unconventional it seemed.

Peridot Logo

Peridot

Peridot is a BDD testing framework (so behavior driven testing) but for your units of code - not your business logic.

Wait, what?

Yes.

If you're familiar with Behat, you'll recognize this syntax (it should be fairly readable even if you're not familiar with it):

Feature: adding a todo

As a user
I want my todos to be persisted
So I don't have to retype them

Scenario: adding a todo
  Given I am on "/"
  When I fill in "todo" with "Get groceries"
  And I press "add"
  And I reload the page
  Then I should see "Get groceries"

Scenario: adding a duplicate todo
  Given I have a done todo "Pick up dinner"
  And I am on "/"
  When I fill in "todo" with "Pick up dinner"
  And I press "add"
  Then I should see "Todo already exists" after waiting
  And I should see 1 "#todos li" elements

The individual phrases are defined in FeatureContext classes like so:

    /**
     * @Then I should see :arg1 after waiting
     */
    public function iShouldSeeAfterWaiting($text)
    {
        $this->getSession()->wait(10000, "document.documentElement.innerHTML.indexOf('$text') > -1");
        $this->assertPageContainsText($text);
    }
    /**
     * @Given I have a todo :arg1
     */
    public function iHaveATodo($todoText)
    {
        $collection = self::getTodoCollection();
        $collection->insert(['label' => $todoText, 'done' => false]);
    }

The framework recognizes them, substitutes the arguments for their values, and tests the conditions.

Continue reading %Testing Frenzy – Can We BDD Test the Units?%


by Bruno Skvorc via SitePoint

No comments:

Post a Comment