0

Learn about BDD and behat

1. Introduce TDD(Test Driven Development)

TDD is an iterative design process include the steps:

TDD-300x291.png

Step 1: Write a test and ensure the new test fails

Step 2: Write code to satisfy the test and ensure all tests pass

Step 3: Refactor

Step 4: Repeat

2. The problem while using and practices TDD

Programmers wanted to know:

  • Where to start

  • How much to test in one go

  • What to call their tests

  • How to understand why a test fails

These problems have been resolved in the Behavior Driven Development (BDD), So What is BDD ?

3. Introduce BDD

3.1. Definition

Behavior Driven Development (BDD)

  • Builds upon TDD

  • Write test cases in a natural language

  • Understood by developers and business folks alike

  • Helps relate domain language of requirements to the code

  • Do this with user stories and scenarios

  • User stories describe a feature's benefit in context

  • Scenarios are executable acceptance criteria

  • A story’s behavior is simply its acceptance criteria– if the system fulfills all the acceptance criteria,it’s behaving correctly; if it doesn’t, it isn’t.

3.2. Objective of BDD

  • Software development towards bringing the business value

  • Write test code first, then write programs

  • Make small portions

  • Create conditions to safely improve the code (refactor)

3.3. Benefits

  • Reduce time to write programs

  • Encourage collaboration across teams

  • Increased modularity, flexibility, and easy to expand

3.4. BDD had answers to some of those TDD questions

What to call their tests?

  • A sentence describing the next behavior in which you are interested.

When a test fails?

  • I had introduced a bug. Solution: Fix the bug.

  • The intended behaviour was still relevant but had moved elsewhere. Solution: Move the test and maybe change it.

  • The behaviour was no longer correct – the premise of the system had changed. Solution: Delete the test.

Where to start:

  • Identify the next most important thing

  • Identify the value of the new features

  • Prioritize new features

  • Formulate the behaviour method name

How much to test in one go:

  • You can only describe so much behavior in a single sentence

4. The structure of Behavior Driven Development (BDD)

  • BDD user stories and scenarios so A story template in BDD this look like

Title (one line describing the story)

As a [role]

I want [feature]

So that [benefit]

  • The structure of one scenario this look like

Scenario 1: Title

Given [context]

And [context]

When [event]

And [some more context]...

Then [outcome]

And[another outcome]...

  • Example: An ATM machine

vd_atm2-300x216.png

5. Introduce Behat with Mink

behat_mink-300x225.png

6. Install Behat with Mink on Linux

6.1. Install LAMP

Run commands

  • sudo apt-get update

  • sudo apt-get install apache2

  • sudo apt-get install php5 libapache2-mod-php5 php5-mcrypt

  • sudo apt-get install php5-cgi php5-cli php5-common php5-curl php5-dbg php5-dev php5-gd php5-gmp php5-ldap php5-xsl

Reference https://www.digitalocean.com/community/tutorials/how-to-install-linux-apache-mysql-php-lamp-stack-on-ubuntu

6.2. Install Java

Run commands

  • sudo add-apt-repository ppa:webupd8team/java

  • sudo apt-get update

  • sudo apt-get install oracle-java7-installer

Reference http://askubuntu.com/questions/335283/how-to-reslove-unmet-dependencies-can-not-install-or-remove-packages-software

6.3. Create composer.json file in the project root

composer.json_-300x142.png

6.4. Download composer.phar and run install command

download_composer.phar_-300x35.png

6.5. Run Behat

run_behat-300x25.png

6.6. Activate MinkExtension

Active_Mink_Extention-300x73.png

6.7. Check Mink extension

api_mink_extention-300x24.png

7. Install and Using selenium 2

7.1. Download latest Selenium server

Download latest Selenium server file http://www.seleniumhq.org/download/

download_jar2-300x132.png

7.2. Run Selenium2 jar before test suites

run_selenium2-300x25.png

7.3. Create and run scenario

script_run_selenium-300x74.png

8. The API of MinkContext

8.1. MinkContext Defines Steps for making requests

Mink_content_making_request-300x91.png

8.2. MinkContext Defines Steps for interacting with forms

mink_content_form-300x121.png

mink_content_form2-300x121.png

8.3. MinkContext Defines Steps for querying the DOM

mink_content_dom2-300x147.png

8.4. MinkContext Defines Steps for examining responses

mink_respon-300x138.png

9. A Note About Step Results

  • Success: a definition was found and executing it did not throw an Exception
  • Undefined: a definition couldn't be found; all subsequent steps will be Skipped
  • Pending: the definition threw the special PendingException, which means you have work to do; skip remaining steps
  • Failure: a definition throws an Exception; Behat will skip remaining steps and terminate with exit status 1

– By default, Behat relies on PHPUnit for assertions, but you can roll your own.

  • Skipped: steps which were never executed
  • Ambiguous: multiple definitions matched a step
  • Redundant: multiple definitions share the same pattern 10. Compare Behat and selenium WebDrive

10.1: Selenium webdrive

The good:

  • Can automate most browsers and mobile devices in many different programming languages

  • It’s pretty widely adopted across other frameworks as well, and can integrate with BDD tools such as Cucumber

  • Selenium is pretty easy to pick up with some programming knowledge

  • Used Selenium with Java with only minor frustration, and Selenium with Ruby and Rspec combined well with minimal set-up for an easy-to-read output and code

The bad:

  • Can do almost anything but doesn’t mean you should

  • The maintenance cost is very high and it’ll take you a while to figure out how to make each browser’s driver work with your code

  • You still have to manually check those browsers for visual bugs.

  • Difficult to find API docs

You should use this if:

  • QA team doesn’t have a lot of programming experience and wants to use the language they’re most comfortable with

  • It’s easy to get support, and you don’t have to learn a new language’s syntax

  • This approach is also good if you’re a developer making Selenium tests for your product - you can write in the same language your product is in without having to switch contexts as much

10.2. Behat

The good:

  • Can automate most browsers

  • Combine your acceptance criteria with your automated acceptance tests

  • You to think through your requirements and makes it easy for non-developers to read your code and run your tests

  • The syntax is easy to read for people who don’t code

  • Easy to find API docs

The bad:

  • The maintenance cost is very high

You should use this if:

  • Product manager and QA at the same time for the same product, and probably should have used Behat instead of Selenium

11. Demo

I have file wikiSearch.feature include story and scenarios

wikiSearch-300x168.png

Run command: bin/behat features/wikiSearch.feature

result_search1-283x300.png

video illustrating run wikiSearch.feature file

https://www.youtube.com/watch?v=zZAbccDiScM&feature=youtu.be


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí