This document provides an overview of Behavior Driven Development (BDD) and how to use the BDD framework Behat and the Drupal extension to write automated tests for Drupal. It discusses what BDD is, how to write user stories and scenarios, how to install and configure Behat and the Drupal extension, and how to write tests that interact with Drupal elements like nodes, users, and regions.
2. About me
• Bozhidar Boshnakov
• Team leader @ ProPeople
• bboshnakov91@gmail.com
• Skype: bo6nakov
• Drupal.org – bboshnakov
• linkedin.com/in/bboshnakov
3. Table of contents
• What is Behavior-Driven Development?
• Why Behavior-Driven Development?
• How to BDD?
• How to automate BDD?
• How to do it in Drupal?
5. SMART PEOPLE
Can make progress …
… without process
This is not a good thing !!!
6. Professionals
• Design, plan and prepare first
• …then do the work
• This produces better results FASTER !!!
7. Process is the difference
between
Software
Engineering &Programming
8. Test-Driven Development
…is an iterative design process
• Write a test
• Ensure the new test fails
• Write code to satisfy the test
• Ensure all tests pass
• Refactor
• Repeat
9. “BDD is about implementing an
application by describing its
behavior from the perspective of
its stakeholders.”
Dan North
10. Behavior-Driven Development
…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
16. Story narrative
In order to read more interesting articles
As a reader
I need to see related articles to the one I’m reading
17. Story narrative
In order to read more interesting articles
As a reader
I need to see related articles to the one I’m reading
18. Stories
In order to read more interesting articles
As a reader
I need to see related articles to the one I’m reading
In order to find most interesting articles faster
As a reader
I need to be presented with high-rating articles first
In order to always provide high-quality content
As a moderator
I need to be able to review articles before publishing
In order to protect reads from spam
As an administrator
I need to differentiate humans from robots
19. Stories
In order to read more interesting articles
As a reader
I need to see related articles to the one I’m reading
In order to find most interesting articles faster
As a reader
I need to be presented with high-rating articles first
In order to always provide high-quality content
As a moderator
I need to be able to review articles before publishing
In order to protect reads from spam
As an administrator
I need to differentiate humans from robots
1
4
3
2
20. Tips & tricks
“Can you give me an example?”
“Is there a case in which this example wouldn’t be true?”
“Is that the only outcome?”
“Does the user has full control?”
21. Feature
Feature: Related articles
in order to read more interesting articles
as a reader
I need to see related articles to the one I’m reading
Scenario: Seeing an article with the same word in the title
Given there is a “DrupalCamp is awesome” article
And there is a “DrupalCamp sessions” article
When I read the “DrupalCamp is awesome” article
Then the “DrupalCamp sessions” should be in the list of related articles
Scenario: Seeing an article with same tag
Given there is a “DrupalCamp is awesome” article tagged “DrupalCampSofia2014”
And there is a “Drupal 8 is great” article tagged “DrupalCampSofia2014”
When I read the “DrupalCamp is awesome” article
Then the “Drupal 8 is great” should be in the list of related articles
22. Gherkin
Gherkin is a Business Readable, Domain
Specific Language created especially for
behavior descriptions. It gives you the
ability to remove logic details from
behavior tests.
24. Gherkin Syntax
Feature: Banana Calculator
As Bob the Banana merchant,
I want a calculator that can add the amount of
bananas so that I can know how many bananas I
currently have
Scenario: Will add 2 banana amounts
Given I have 3 Bananas
When I add 5 Bananas
Then I should have 8 Banana
25.
26. In order to verify application behavior
As a software developer
I need tests
Preferably automated tests
38. Understanding Mink
One of the most important parts in the
web is a browser. A browser is the window
through which web application users
interact with the application and other
users.
40. Selenium2Driver
• Control the Browser – Session
• Cookies and Headers management
• HTTP Authentication
• Traverse the Page - Selectors
• Manipulate the Page
45. Now…
Let’s get away from the
theory and show some
action!
46. Tags
Tags are a great way to organize your
features and scenarios:
A Scenario or Feature can have as many
tags as you like, just separate them with
spaces:
47. Tags
How to execute the tags?
If a tag exists on a Feature, Behat
will assign that tag to all child
Scenarios and Scenario Outlines
too!
51. The Drupal extension
• Overrides default behaviors:
– Snippets follow Drupal coding standards
– Mink Extension steps get synonyms for readability
• Provides drivers to facilitate data setup
– Blackbox
– Drush
– Drupal API
• Adds support for Drupal:
– Regions
– Node types
– Users and Roles
– Taxonomy
– Subcontexts for Contributed Modules
52. Provides drivers for data set up
• Works with Drupal 6, 7, and 8
• Connect with no privileges (local or remote)
• Connect with Drush (local or remote)
• Connect with the Drupal API (local only)
53. Drush: alias file
http://drush.ws/examples/example.aliases.drushrc.php
63. If you have any questions or you want
access for the full installation guideline feel
free to contact me on my email address:
bboshnakov91@gmail.com
Most of the programmers are smart and it is very important when building software.
What I mean…
This is not good for big projects
This is not good for big projects
This is not good for big projects
В ТДД тестовете реално не са тестове а примери как функционалностите трябва работят! Но оставям на вас да прецените кои са силните и слабите страни на този метод на работа.
TDD Defined:
- Decide what the code will do
Write a test that will pass if the code does that thing
Run the test, see it fail
Write the code
Run the test see it pass
TDD Provides:
Design and plan before you code
Document your design
Proof that code implements design
Encourages design of testable code
“BDD is about implementing an application by describing its behavior from the perspective of its stakeholders.” – Dan North
The original developer of BDD (Dan North) came up with the notion of BDD because he was dissatisfied with the lack of any specification within TDD of what should be tested and how.
BDD – Second generation of agile methodology
User stories
Domain-Driven Development
Extreme Programmin
TDD
Acceptance Driven Test Planning
Continuous integration
“BDD is about implementing an application by describing its behavior from the perspective of its stakeholders.” – Dan North
The original developer of BDD (Dan North) came up with the notion of BDD because he was dissatisfied with the lack of any specification within TDD of what should be tested and how.
BDD – Second generation of agile methodology
User stories
Domain-Driven Development
Extreme Programmin
TDD
Acceptance Driven Test Planning
Continuous integration
Everzet Konstantin Kudryashov -> Въпроси за техният бизнес -> Започнете да доставяте софтуер който се ползва и наистина удовлетворява клиента.
Не започвайте да с tools, а с разговори и въпроси!!!!
Сега ще покажа прост пример как да опишем тези неща.
Само бърз пример за да ви покажа нагледно как изразяваме тези стъпки в програмен код.
И сега ще си поговорим как точно да сложим бехат на нашите локални машини.
Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.
This file is the Composer binary. It is a PHAR (PHP archive), which is an archive format for PHP which can be run on the command line, amongst other things.
Sometimes, Behat’s default configuration is not enough. Some day you’ll need some extended tools to configure your feature suite. For that day, Behat has a very powerful configuration system based on YAML configuration files and profiles.
Every *.feature file conventionally consists of a single feature. Lines starting with the keyword Feature: (or its localized equivalent) followed by three indented lines starts a feature. A feature usually contains a list of scenarios. You can write whatever you want up until the first scenario, which starts withScenario: (or localized equivalent) on a new line. You can use tags to group features and scenarios together, independent of your file and directory structure.
Every scenario consists of a list of steps, which must start with one of the keywords Given, When, Then, But or And (or localized one). Behat treats them all the same, but you shouldn’t. Here is an example:
There’s huge amount of browser emulators out there, like Goutte, Selenium, Sahi and others. They all do the same job, but do it very differently. They behave differently and have very different API’s. But, what’s more important - there’s actually 2 completely different types of browser emulators out there:
Headless browser emulators
Browser controllers
First type browsers are simple pure HTTP specification implementations, likeGoutte. Those browser emulators send a real HTTP requests against an application and parse the response content. They are very simple to run and configure, because this type of emulators can be written in any available programming language and can be run through console on servers without GUI. Headless emulators have both, advantages and disadvantages. Advantages are simplicity, speed and ability to run it without the need in real browser. But this type of browsers have one big disadvantage - they have no JS/AJAX support. So, you can’t test your rich GUI web applications with headless browsers.
Second browser emulators type are browser controllers. Those emulators aims to control the real browser. That’s right, a program to control another program. Browser controllers simulate user interactions on browser and are able to retrieve actual information from current browser page. Selenium and Sahi are two most famous browser controllers. The main advantage of browser controllers usage is the support for JS/AJAX interactions on page. Disadvantage is that such browser emulators require installed browser, extra configuration are usually much slower than headless counterparts.
All of one team's tests:bin/behat features/folder
A single feature:bin/behat features/folder/filename.feature
A single scenario within a feature:bin/behat features/teamname/filename.feature:7 where 7 represents the first line of the scenario. Exclude the tagname if present.
All tests with a certain tag:bin/behat --tags @tagname
All tests except a certain tag:bin/behat --tags ~tagname
Multiple tags:bin/behat --tags "@tagname1&&@tagname2&&~@tagname3"
By adding an @javascript tag above the feature (for all the scenarios) or above a single scenario, the test will use Selenium and Firefox to run the test. These defaults can be changed in the behat.yml file.
Behat tries to load behat.yml or config/behat.yml by default, or you can tell Behat where your config file is with the --config option:
Paths
Formatter
Colors
Context
ImportsEnvironment Variable
The imports block allows you to share your feature suite configuration between projects and their test suites.
All files from the imports block will be loaded by Behat and merged into yourbehat.yml config.
ProfilesExtensions
ProfilesExtensions
ProfilesExtensions
Examples
ProfilesExtensions
The RegexpMapper changes filenames according to a pattern defined by a regular expression. This is the most powerful mapper and you should be able to use it for every possible application.
The RegexpMapper changes filenames according to a pattern defined by a regular expression. This is the most powerful mapper and you should be able to use it for every possible application.