Python Behave 101: Where BDD Meets Pythonic Wizardry
A well behaved Python painting in Van Gogh Style

Python Behave 101: Where BDD Meets Pythonic Wizardry

Behavior-Driven Development (BDD) ??

It is a software development methodology that emphasizes collaboration and communication between stakeholders, including business analysts, developers, and testers. Unlike traditional testing approaches, in BDD we first define the behaviour of our product via scenarios and then try to achieve them. BDD aims to align the development process with the desired business outcomes.

The real power of BDD lies in its ability to bridge the gap between technical and non-technical team members. By using a common language and focusing on behavior, BDD encourages collaboration, improves understanding, and promotes a shared sense of purpose. It enables stakeholders to have meaningful conversations, clarify requirements, and uncover potential issues early in the development process. With BDD, development teams can build high-quality software that aligns with business goals and meets user expectations.

For some of you who knows about Test-Driven Development, this might seem similar, below is a small venn diagram that shows the commonalities and differences between the both

No alt text provided for this image
TDD vs BDD : Image from https://test-gear.io/blog/how-to-involve-manual-testers-in-supporting-automated-tests-using-bdd

For BDD, we use the language "Gherkin" to define our scenarios by following the Given-When-Then format, for example, let us define a scenario of addition for a calculator

Given I have a calculator
When I add 5 and 7
Then the result should be 12        

Getting Started with BDD in Python using behave ??

Behave is a python package that helps us implement BDD in python. Here is a step by step guide to get you started

1. Installing Behave: We can install behave like any normal python package using pip

pip install behave        

2. Directory Structure: The following directory structure is followed for behave to capture all the required files and configurations in your your python project

├── main.py
├── features
│? ?├── steps
│? ?└── example.feature
└── behave.ini        

3. Writing Feature Files in Gherkin Syntax: Let us take the same example of calculator as above and write the same in a feature file called example.feature

Feature: Example Feature
? Scenario: Example Scenario
? ? Given I have a calculator
? ? When I add 5 and 7
? ? Then the result should be 12        

4. Running Behave Scenarios: Open your terminal or command prompt, navigate to the root directory of your BDD tests, and run the following command:

behave        

Behave will automatically discover and execute the feature files and scenarios within your features directory. It will display the test results and provide feedback on whether the scenarios pass or fail.

5. Implementing Step Definitions: After running the tests, you'll notice that Behave generates missing step definitions for your scenarios. These step definitions are Python functions that map to the steps in your feature file. You need to implement these step definitions inside the steps directory. For example, create a file named example_steps.py inside the steps directory and define the step definitions as follows:

from behave import given, when, then


@given('I have a calculator')
def step_given_calculator(context):
? ? context.calculator = Calculator()


@when('I add {num1:d} and {num2:d}')
def step_when_add_numbers(context, num1, num2):
? ? context.result = context.calculator.add(num1, num2)


@then('the result should be {expected_result:d}')
def step_then_check_result(context, expected_result):
? ? assert context.result == expected_result        

Note that you need to define the Calculator class and its add method to support the step definitions.

6. Running Behave Scenarios Again: Once you have implemented the step definitions, run the behave command again. Behave will now match the step definitions to the steps in your feature file and execute the tests accordingly.

7. Output : The terminal output provides valuable information about the progress and results of your Behave scenarios.

Feature: Example Feature
? Scenario: Example Scenario
? ? Given I have a calculator? ? ? ? ? ? ? ? PASSED
? ? When I add 5 and 7? ? ? ? ? ? ? ? ? ? ? ? PASSED
? ? Then the result should be 12? ? ? ? ? ? ? PASSED


1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined        

In this example, the execution of the "Example Scenario" passed without any failures. The terminal output indicates that 1 feature and 1 scenario passed, with a total of 3 steps passing. The summary shows a successful test execution with no failures or skipped scenarios.

Advanced behave Features ??

Above, we saw basic features of behave, but it has many advanced features that could help us even more

  1. Parameterized Scenarios
  2. Tags and Tag Expressions
  3. Background Steps
  4. Hooks
  5. Data-Driven Testing
  6. Step Decorators
  7. Custom Reporters, Integration with Allure
  8. Step Parameters and Regular Expressions

Conclusion ??

Overall, behave empowers Python developers to embrace BDD practices, write expressive and maintainable tests, and ensure the delivery of high-quality software that aligns with business goals and user expectations. By using behave you can bridge the gap between stakeholders, developers & testers and help all of them stand on the same ground and understand each other better.

References ??


Santhosh Toorpu

SWE @Zenoti | Tensorflow Certified Developer | Student @ VNR Vignanajyothi Institute of Engineering & Technology | AI enthusiast

1 年

I'll keep this in mind

要查看或添加评论,请登录

Anurag Pola的更多文章

社区洞察

其他会员也浏览了