background preloader

Behat

Facebook Twitter

Cucumber Backgrounder · cucumber/cucumber Wiki. Or: How I Learned to Stop Worrying and Love Testing Start Behaving Introduction Cucumber is a tool that implements a Behaviour Driven Design (BDD) workflow.

Cucumber Backgrounder · cucumber/cucumber Wiki

This document deals principally with initial set up and first use of the Cucumber-Rails and Cucumber Rubygems. It takes as its background the Ruby on Rails (RoR) web application framework. Detailed discussion of Behaviour Driven (BDD), Test Driven (TDD), and Panic Driven Development (SNAFU aka Cowboy Coding) can be found elsewhere. You will find some familiarity with the Ruby language helpful and of the RoR framework somewhat less so. Details regarding installing the Cucumber Rubygem and its recommended support tools for RoR are found on this wiki under the heading Ruby on Rails.

Note that in this document I often use the terms testing and test where BDD practitioners prefer the terms behaviour and expectation. Where to Start? Before anything else make sure that you have the cucumber-rails installed properly into your RoR project. What’s in a Story? [This article has been translated into Korean by HongJoo Lee, French by Philippe Poumaroux, Spanish by Adrian Moya, Russian by Denis Oleynik, and German by Julia Kadauke.]

What’s in a Story?

Behaviour-driven development is an “outside-in” methodology. It starts at the outside by identifying business outcomes, and then drills down into the feature set that will achieve those outcomes. Each feature is captured as a “story”, which defines the scope of the feature along with its acceptance criteria. This article introduces the BDD approach to defining and identifying stories and their acceptance criteria. Introduction Software delivery is about writing software to achieve business outcomes. Usually, the business outcomes are too coarse-grained to be used to directly write software (where do you start coding when the outcome is “save 5% of my operating costs”?) Accessing contexts from each other — Behat 3.0.12 documentation.

When splitting the definitions in multiple contexts, it might be useful to access a context from another one.

Accessing contexts from each other — Behat 3.0.12 documentation

This is particularly useful when migrating from Behat 2.x to replace subcontexts. Behat allows to access the environment in hooks, so other contexts can be retrieved using a BeforeScenario hook: Integrating Symfony2 with Behat — Behat 3.0.12 documentation. Configuring Behat — Behat 3.0.12 documentation. Global Filters While it is possible to specify filters as part of suite configuration, sometimes you will want to exclude certain scenarios across the suite, with the option to override the filters at the command line.

Configuring Behat — Behat 3.0.12 documentation

This is achieved by specifying the filter in the gherkin configuration: # behat.yml default: gherkin: filters: tags: ~@wip In this instance, scenarios tagged as @wip will be ignored unless the CLI command is run with a custom filter, e.g. Configuring Test Suites — Behat 3.0.12 documentation. We already talked about configuring multiple contexts for a single test suite in a previous chapter.

Configuring Test Suites — Behat 3.0.12 documentation

Now it is time to talk about test suites themselves. A test suite represents a group of concrete features together with the information on how to test them. With suites you can configure Behat to test different kinds of features using different kinds of contexts and doing so in one run. Test suites are really powerful and behat.yml makes them that much more powerful: Suite Paths One of the most obvious settings of the suites is the paths configuration: Testing Features — Behat 3.0.12 documentation. We’ve already used this strange FeatureContext class as a home for our step definitions and hooks, but we haven’t done much to explain what it actually is.

Testing Features — Behat 3.0.12 documentation

Context classes are a keystone of testing environment in Behat. The context class is a simple POPO (Plain Old PHP Object) that tells Behat how to test your features. If *.feature files are all about describing how your application behaves, then the context class is all about how to test it. A simple mnemonic for context classes is “testing features in a context”. Feature descriptions tend to be very high level. Context Class Requirements. Hooking into the Test Process — Behat 3.0.12 documentation. You’ve learned how to write step definitions and that with Gherkin you can move common steps into a background block, making your features DRY.

Hooking into the Test Process — Behat 3.0.12 documentation

But what if that’s not enough? What if you want to execute some code before the whole test suite or after a specific scenario? Hooks to the rescue: // features/bootstrap/FeatureContext.php use Behat\Behat\Context\Context;use Behat\Testwork\Hook\Scope\BeforeSuiteScope;use Behat\Behat\Hook\Scope\AfterScenarioScope; class FeatureContext implements Context{ /** * @BeforeSuite */ public static function prepare(BeforeSuiteScope $scope) { // prepare system for test suite // before it runs } /** * @AfterScenario @database */ public function cleanDB(AfterScenarioScope $scope) { // clean database after scenarios, // tagged with @database }}

Defining Reusable Actions — Behat 3.0.12 documentation. Gherkin language provides a way to describe your application behavior in business understandable language.

Defining Reusable Actions — Behat 3.0.12 documentation

But how do you test that the described behavior is actually implemented? Or that the application satisfies your business expectations as described in the feature scenarios? Behat provides a way to map your scenario steps (actions) 1-to-1 with actual PHP code called step definitions: /** * @When I do something with :argument */public function iDoSomethingWith($argument){ // do something with $argument} Definitions Home - FeatureContext Class. Writing Features — Behat 3.0.12 documentation. Behat is a tool to test the behavior of your application, described in a special language called Gherkin.

Writing Features — Behat 3.0.12 documentation

Gherkin is a Business Readable, Domain Specific Language created specifically for behavior descriptions. It gives you the ability to remove logic details from behavior tests. Gherkin serves as your project’s documentation as well as your project’s automated tests. Building Domain Model — Behat 3.0.12 documentation. Welcome to Behat!

Building Domain Model — Behat 3.0.12 documentation

Behat is a tool to close the Behavior Driven Development (BDD) communication loop. BDD is a methodology for developing software through continuous example-based communication between developers and a business, which this application supports. This communication happens in a form that both the business and developers can clearly understand - examples. Examples are structured around the Context-Action-Outcome pattern and are written in a special format called Gherkin. The fact that Gherkin is very structural makes it very easy to automate and autotest your behaviour examples against a developing application. Example Let’s imagine that you are building a completely new e-commerce platform. Feature: Product basket In order to buy products As a customer I need to be able to put interesting products into a basket Note. Behat Documentation — Behat 2.5.3 documentation. Behat is an open source Behavior Driven Development framework for PHP 5.3+. What’s behavior driven development, you ask?

It’s a way to develop software through a constant communication with stakeholders in form of examples; examples of how this software should help them, and you, to achieve your goals. For example, imagine you’re about to create the famous UNIX ls command. Before you begin, you will have a conversation with your stakeholders (UNIX users) and they might say that even though they like UNIX a lot, they need a way to see all the files in the current working directory.

You then have a back-and-forth chat with them about how they see this feature working and you come up with your first scenario (an alternative name for example in BDD methodology):