top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Behavior Driven Development (BDD) in Python

Introduction

Behavior Driven Development (BDD) is a automated software testing approach in which we create test cases in simple language i.e. English, backed up by Python code. This helps to understand the test cases in scenarios among the technical and non technical teams, manager and stakeholders in on going software project. BDD is done in early stage of software development.


Installation


In BDD python, we will use behave test framework. To install behave in django as follows:

pip install behave

In Python Behave Test Framework, there are two file one is Feature File and other is Steps File.


Feature File

It is a file written in simple language i.e english to describe the configure, execute and confirm the results of a test case. In Feature file, there are feature keyword and scenario keyword. In Scenario keyword, we describe the story of a planned test case to be tested in three keywords i.e. Given, When and Then. A feature file can have multiple scenarios. The Feature file has .feature extension in python.


The format of BDD test case is as follows:

Feature: Label or title of a test case.

Scenario: A User Story is defined to plan a test case.

Given Preconditions

When Actions

Then Results


Steps File

it contains the python code function on the basis of feature file steps.


Example of BDD

I am writing a BDD test case of Calculate two numbers which is given below:

Feature File name: add_number.feature

Feature: Test Calculator Functionality

Scenario:Addition
 Given Calculator app is run
 When I input "2" and "3" to calculator
 Then I get result "5"

Scenario:Addition Negative
 Given Calculator app is run
 When I input "-2" and "3" to calculator
 Then I get result "1"

Steps File: add_number.py

from behave import given, when, then
from calculator import add

@given(u'Calculator app is run')
def step_impl(context):
    print(u'Step: Given Calculator app is run')

@when(u'I input "{a}" and "{b}" to calculator')
def step_impl(context, a, b):
    print(u'Step: When I input "{}" and "{}" to calculator'. format(a, b))
    context.result = add(a, b)
    print(u'Stored result "{}" in context'. format(context.result))

@then(u'I get result "{out}"')
def step_impl(context, out):
    if (context.result == int(out)):
        print(u'Step: Then I get right result "{}", "{}"'.format(context.result, out))
        pass
    else :
        raise Exception ("Invalid sum is returned.")

Code : calculator.py

def add(a, b):
    return int(a) + int(b)

Note: To run the test case,we input behave in run command section.

Syntax: behave -i filename.feature Syntax: behave

Output:

C:\Users\PycharmProjects\untitled>behave -i add_number.feature Feature: Test Calculator Functionality # features/add_number.feature:1 Scenario: Addition # features/add_number.feature:3 Given Calculator app is run # features/steps/add_number.py:4 When I input “2” and “3” to calculator # features/steps/add_number.py:8 Then I get result “5” # features/steps/add_number.py:14 Scenario: Addition Negative # features/add_number.feature:8 Given Calculator app is run # features/steps/add_number.py:4 When I input “-2” and “3” to calculator # features/steps/add_number.py:8 Then I get result “1” # features/steps/add_number.py:14 1 feature passed, 0 failed, 0 skipped 2 scenarios passed, 0 failed, 0 skipped 6 steps passed, 0 failed, 0 skipped, 0 undefined

Conclusion:

I hope, This article will help you to understand the Behavior Driven Development in python Django. For given code, please visit my GitHub.

345 views0 comments

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page