olly - Fotolia
Due to the increasingly fast cadence of application development and deployment, developers must write clean, error-free code. However, it is equally important to test how that code will actually behave in production. This is where a concept known as behavior-driven development comes into play.
Behavior-driven development (BDD) binds the functional requirements of an application with the source code. In doing so, it focuses development onto the behavior of the system you wish to implement. BDD is often used for microservices-based application development. Rather than writing code that can pass a test, as done in test-driven development, developers write code that implements the actual behavior of a particular application service and verify its business logic.
There are two major advantages to implementing a BDD framework:
- Reusability of BDD scenarios can help to avoid wasteful code duplication.
- Separation of concerns enables developers to decouple the BDD scenarios from the application's source code. Development and test teams thus work independently of each other on the same application.
To get the most out of a BDD framework, developers need to understand how to use it, and become accustomed to the Given-When-Then formula. The right tool also helps. This article profiles three well-known BDD tools: Cucumber, Gauge and JBehave.
How to perform BDD
The key to BDD is to write executable acceptance tests that are adept at describing the application's expected behavior across a range of potential use-case scenarios. This methodology ensures that the developers not only write testable code, but also write code that adheres to the functional requirements of the application.
There are three stages of working with BDD:
- A business analyst, or other team member with specific business domain expertise, lists the expected behavior in a feature file that they share with development teams.
- Developers then write code that implements those expected behaviors. They follow up with test code that they will use to verify that behavior against the required business logic to ensure it meets business-side needs.
- Finally, testers review the application, verifying those tests against the expected behavior documented in feature files and the BDD code created by the developers.
When gathering requirements in BDD, it's important to follow the Given-When-Then formula. This formula breaks down behavior into three steps:
- Given defines the preconditions for a certain behavior.
- When denotes when a specific application event may occur.
- Then deals with the outcome of the test when preconditions are matched against the event.
These steps are parsed and executed by the BDD framework, sometimes referred to as automated acceptance tests.
Popular BDD framework tooling
In microservices applications, business logic can span through several services. All these services get tested against business processes to verify the expected behavior. Microservices testing is a difficult task without the aid of BDD tools. Some top options include Cucumber, Gauge and JBehave.
Cucumber. Cucumber is an open source testing BDD framework that enables developers to write specifications in Gherkin, which has an English-like syntax approach. The language allows anyone -- from other developers to business analysts -- to easily read and understand the information. Cucumber supports several programming languages, including Ruby, Java and .NET, as well as software platforms like Ruby on Rails, Selenium and Spring Framework.
Gauge. Gauge is a cross-browser, open source test automation framework. When working with Gauge, developers are not restricted to a specific language or tool. Like Cucumber, it works with multiple programming languages, and there is no need to learn a new language to prepare your test automation platform. Gauge also integrates with several integrated development environments and continuous integration servers, including IntelliJ and Jenkins.
JBehave. JBehave is an open source BDD framework designed for microservices testing and Java-based development. While JBehave is relatively restricted to Java implementations, it still offers a wide array of customization options for developers to configure their BDD tests. It also comes with notably thorough and easy-to-understand documentation.