TDD, Test and Behavior Driven Development and Unit Testing in Python

Reading Time: 13 Minutes

by  | December 08, 2017   TDD, Test and Behavior Driven Development and Unit Testing in Python

Overview

Test Driven Development (TDD) is a great approach for software development. TDD is nothing but the development of tests before adding a feature in code.

This approach is based on the principle that we should write small codes rather than writing long codes. In TDD, whenever we want to add more functionality in our codes, we first have to write a test for that. After that, we add new functionality with small code lines combined and then test it with our test. This approach helps us to reduce the risk of encountering significant problems at the production level.


Test Driven Development (TDD)

Test Driven Development is an approach in which we build a test first, then fail the test and finally refactor our code to pass the test.

Test Driven Development (TDD) Approach

As the name suggests, we should first add the test before adding the functionality in our code. Now our target is to make the test pass by adding new code to our program. So we refactor our code to pass the written test. This uses the following process -

  • Write a failing unit test

  • Make the unit test pass

  • Repeat


Test Driven Development (TDD) Process Cycle

Test Driven Development Process Cycle

As shown in the flow

  • First, add tests for the functionality.

  • Next, we run our test to fail.

  • Next, we write code according to the error we received.

  • Then we run the tests again to see if the test fails or passes.

  • Then refactor the code and follow the process again.


Benefits of Test Driven Development (TDD)

Now the question arises why should one opt TDD approach. Practicing TDD brings lots of benefits. Some of the benefits are listed below -

  • In TDD we build test before adding any new feature to it, that means in TDD approach our entire code is covered under the test. That’s a great benefit of TDD as compared to the code which has no test coverage.

  • In TDD one should have a specific target before adding new functionality. This means before adding any new functionality one should be clear about its outcome.

  • In an application, one method depends on the other. When we write tests before the method that means we should have clear thoughts about the interfaces between the methods. That allows us to integrate our method with the entire application efficiently and help in making our application modular too.

  • As the entire code is covered by the test that means our final application will be less buggy. This is a big advantage of the TDD approach.


Acceptance Test Driven Development (ATDD)

ATDD is short for Acceptance Test Driven Development. In this process, a user, business manager and developer, all are involved.

First, they discuss what the user wants in his product; then business manager creates sprint stories for the developer. After that, the developer writes tests before starting the projects and then starts coding for their product.

Every product/software is divided into small modules, so the developer codes for the very first module then test it and sees it getting failed. If the test passes and the code are working as per user requirements, it is moved to the next user story; otherwise, some changes are made in coding or program to make the test pass.

This process is called Acceptance Test Driven Development.


Behavior Driven Development (BDD)

Behavior Driven testing is similar to Test Driven Development, in the way that in BDD also tests are written first and tested and then more code is added to it to make the test pass.

The major area where these two differ is that tests in BDD are written in plain descriptive English type grammar.

Tests in BDD aim at explaining the behaviour of the application and are also more user-focused. These tests use examples to clarify the user requirements in a better way.

Features of Behavior Driven Development (BDD)

  • The major change is in the thought process which is to be shifted from analyzing in tests to analyzing in behaviour.

  • Ubiquitous language is used; hence it is easy to be explained.

  • BDD approach is driven by business value.

  • It can be seen as an extension to TDD; it uses natural language which is easy to understand by non-technical stakeholders also.

You May also Love to Read Test Driven Development in Scala


Behavior Driven Development (BDD) Approach

We believe that the role of testing and test automation TDD(Test Driven Development) is essential to the success of any BDD initiative. Testers have to write tests that validate the behaviour of the product or system being generated.

The test results formed are more readable by the non-technical user as well. For Behavior Driven Development to be successful, it becomes crucial to classify and verify only those behaviours that give directly to business outcomes.

Developer in the BDD environment has to identify what program to test and what not to test and to understand why the test failed. Much like Test Driven Development, BDD also recommends that tests should be written first and should describe the functionalities of the product that can be suited to the requirements.

Behavior Driven Development helps greatly when building detailed automated unit tests because it focuses on testing behaviour instead of testing implementation. The developer thus has to focus on writing test cases keeping the synopsis rather than code implementation in mind.

By doing this, even when the requirements change, the developer does not have to change the test, input and output to support it. That makes unit testing automation much faster and more reliable.

Though BDD has its own sets of advantages, it can sometimes fall prey to reductions. Development teams and Tester, therefore, need to accept that while failing a test is a guarantee that the product is not ready to be delivered to the client, passing a test also does not mean that the product is ready for launch.

It will be closed when development, testing and business teams will give updates and progress report on time. Since the testing efforts are moved more towards automation and cover all business features and use cases, this framework ensures a high defect detection rate due to higher test coverage, faster changes, and timely releases.

Benefits of Behavior Driven Development (BDD)

It is highly suggested for teams and developers to adopt BDD because of several reasons, some of them are listed below -

  • BDD provides a very accurate guidance on how to be organizing communication between all the stakeholders of a project, may it be technical or non-technical.

  • BDD enables early testing in the development process, early testing means lesser bugs later.

  • By using a language that is understood by all, rather than a programming language, a better visibility of the project is achieved.

  • The developers feel more confident about their code and that it won’t break which ensures better predictability.


Test Driven Development (TDD) with Python

Here we exploring the Test-driven development approach with Python. Python official interpreter comes with the unit test module

Python Unit Testing

These are the main methods which we use with python unit testing

Method

Checks That

assertEqual(a, b)

a == b

assertNotEqual(a, b)

a != b

assertTrue(x)

bool(x) is True

assertFalse(x)

bool(x) is False

assertIs(a, b)

a is b

assertIsNot(a, b)

a is not b

assertIsNone(x)

x is None

assertIsNotNone(x)

x is not None

assertIn(a, b)

a in b

assertNotIn(a, b)

a not in b

assertIsInstance(a, b)

isinstance(a, b)

assertNotIsInstance(a, b)

not isinstance(a, b)

You May also Love to Read Test Driven & Behavior Driven Development in Java


Test Driven Development (TDD) in Python with Examples

We are going to work on an example problem of banking. Let's say that our banking system introduced a new facility of credit money. So we have to add this to our program.

Following the TDD approach before adding this credit feature, we first write our test for this functionality.


Setting Up Environment For Test Driven Development (TDD)

This is our directory structure

Environmental Setup for Test Driven Development in Python


Writing Test For Test Driven Development (TDD)

So we write the following code for the unittest in the test/tdd_example.py


import unittest
class Tdd_Python(unittest.TestCase):
 def test_banking_credit_method_returns_correct_result(self):
 bank = Banking()
final_bal = bank.credit(1000)
self.assertEqual(1000, final_bal)

Here first we import the unittest module and then write our test method. One thing which we should notice that every test method should start with the test word.

Now we run this program


File "/PycharmProjects/TDD_Python/test/tdd_example.py", line 6, in test_banking_credit_method_returns_correct_result
bank = Banking()
NameError: name 'Banking'
is not defined

We get an error here that is Banking not defined because we have not created it.


Implementing Test Driven Development (TDD) in Python

So first we have to create our Banking class in the app/banking_app.py file


class Banking():

 def __init__(self):
 self.balance = 0

def credit(self, amount):
 pass

And now our test/tdd_example.py look like this


import unittest
from app.banking_app
import Banking

class Tdd_Python(unittest.TestCase):
 def test_banking_credit_method_returns_correct_result(self):
 bank = Banking()
final_bal = bank.credit(1000)
self.assertEqual(1000, final_bal)

if __name__ == '__main__':
 unittest.main()

Now let's run the test again


AssertionError: 1000 != None
Ran 1 test in 0.002 s
FAILED(failures = 1)

No, the test fails because we don’t get any return from the credit method in Banking class. So we have to manipulate it


class Banking():
 def __init__(self):
 self.balance = 0

def credit(self, amount):
 self.balance += amount
return self.balance

Now run the test again


Testing started at 5: 10 PM...
Ran 1 test in 0.001 s
OK

That is the success. So we add credit functionality in our banking and it works as expected

But our program is not able to handle some situations, like when a user entered a string instead of the number then our program might crash. So we have to deal with this situation.

First, we have to write our test for this


import unittest
from app.banking_app
import Banking

class Tdd_Python(unittest.TestCase):
 def setUp(self):
 self.bank = Banking()

def test_banking_credit_method_returns_correct_result(self):
 final_bal = self.bank.credit(1000)
self.assertEqual(1000, final_bal)

def test_banking_credit_method_returns_error_if_args_not_numbers(self):
 self.assertRaises(ValueError, self.bank.credit, 'two')

if __name__ == '__main__':
 unittest.main()
 
 

The output is this


Ran 2 tests in 0.002 s
FAILED(errors = 1)

The code is failing because we have not added that functionality in our app

After adding in our code the code will look like this


class Banking():
 def __init__(self):
 self.balance = 0

def credit(self, amount):
 amount_type = (int, float, complex)

if isinstance(amount, amount_type):
 self.balance += amount
return self.balance
else :
 raise ValueError

After running the test we get


Testing started at 5: 33 PM...
Launching unittests with arguments python - m unittesttdd_example.py in /PycharmProjects/TDD_Python / test
Ran 2 tests in 0.002 s
OK

Similarly, we can add more functionality to our app by following this approach. By following the same process

In the bank, there is more operation like debit money. That means we have to add another functionality of debit also.

So we start with the same approach first building test for the debit operation


import unittest
from app.banking_app
import Banking

class Tdd_Python(unittest.TestCase):
 def setUp(self):
 self.bank = Banking()

def test_banking_credit_method_returns_correct_result(self):
 final_bal = self.bank.credit(1000)
self.assertEqual(1000, final_bal)

def test_banking_credit_method_returns_error_if_args_not_numbers(self):
 self.assertRaises(ValueError, self.bank.credit, 'two')

def test_banking_debit_method_returns_correct_result(self):
 final_bal = self.bank.debit(700)
self.assertEqual(1000, final_bal)

def test_banking_debit_method_returns_error_if_args_not_numbers(self):
 self.assertRaises(ValueError, self.bank.debit, 'two')

if __name__ == '__main__':
 unittest.main()

As expected we get the error


AttributeError: 'Banking'
object has no attribute 'debit'
Ran 4 tests in 0.003 s
FAILED(errors = 2)

So we refactor our code again by adding debit money method


class Banking():
 def __init__(self):
 self.balance = 1000

def credit(self, amount):
 amount_type = (int, float, complex)

if isinstance(amount, amount_type):
 self.balance += amount
return self.balance
else :
 raise ValueError

def debit(self, amount):
 amount_type = (int, float, complex)

if isinstance(amount, amount_type):
 self.balance -= amount
return self.balance
else :
 raise ValueError
 
 

And now running the test again


Testing started at 5: 44 PM...
Launching unittests with arguments python - m unittest / PycharmProjects / TDD_Python / test / tdd_example.py / PycharmProjects / TDD_Python / test
Ran 4 tests in 0.002 s
OK


Test Driven Development (TDD) Tools For Python

  • nosetest

nosetest is a test runner package. We can easily install this by just pip command

$ pip install nose

Once it is installed successfully we can run test file simply by just

$ nosetests example_unit_test.py

And the result is as follow


(venv) xenon@dm08:~/PycharmProjects/TDD_Python$ nosetests test/tdd_example.py
----------------------------------------------------------------------
Ran 4 tests in 0.001s
OK

Or execute tests of folder

$ nosetests /path/to/tests

  • py.test

py.test is also similar to the nosetest, one thing that makes it good is that it show the output of each test in separate bottom area

We can install this by the following command

$ pip install pytest

Once it is installed successfully we can run test file simply by just


$ py.test test/example_unit_test.py

(venv) xenon @dm08: ~/PycharmProjects/TDD_Python$ py.test test / tdd_example.py === === === === === === === === == test session starts === === === === === === === === === == platform linux--Python 3.4 .3, pytest - 3.2 .5, py - 1.5 .2, pluggy - 0.4 .0 rootdir: /home/xenon / PycharmProjects / TDD_Python, inifile: collected 4 items test / tdd_example.py.... === === === === === === === === 4 passed in 0.09 seconds === === === === === === === === ===
  • unittest

unittest is default test package of the python. It is useful we don't want to install external packages. To use this just add following lines in the code


if __name__ == '__main__':
 unittest.main()

  • unittest.mock

In python, there is a library unitest.mock for testing. This is useful when we try to test a function which requires large time to complete. So we use unittest.mock library to mock that function. This library mock objects and make assertions about their uses. unitest.mock provide a Mock class, MagicMock and patch(). Here is a quick example to use them

Suppose our credit function uses too much time to complete. So instead to call the function credit in the test we should mock it to speed up the testing


import unittest
from unittest.mock
import patch
from app.banking_app
import Banking

class TestBanking(unittest.TestCase):
 @patch('app.banking_app.Banking.credit', return_value = 1700)
def test_credit(self, credit):
 self.assertEqual(credit(700), 1700)

After running this test we get the following output


(venv) xenon @dm08: ~/PycharmProjects/TDD_Python$
py.test test / mock.py === === === === === === === === = test session starts === === === === === === === === === === =
 platform linux--Python 3.4 .3, pytest - 3.2 .5, py - 1.5 .2, pluggy - 0.4 .0
rootdir: /home/xenon / PycharmProjects / TDD_Python, inifile:
 collected 1 item

test / mock.py.

=== === === === === === === === == 1 passed in 0.14 seconds === === === === === === === === ===

You May also Love to Read Test Driven & Behavior Driven Development in JavaScript and React.JS


Summary

In TDD approach the main objective is to fail first and then rectify your code. So we first build test, fail the test and then refactor our code to pass the test. This approach uses the idea that first we should finalize what we require, then built the target test for it, and then start to achieve that target. 


How Can Don Help You?

Don follows the Test Driven Development Approach in the development of Enterprise level Applications following Agile Scrum Methodology.

Data Analysis Services

Don Data Analysis services offers data preparation, statisticial analysis, creating meaningful Data Visualizations, predicting future trends and more. Python offers rich set of utilities and libraries for Data Processing and Data Analytics task. Different Data Analysis Libraries used for data analysis includes Pandas, NumPy, SciPy, IPython and more.

Data Visualization with Python

Don Data Visualization Services provides you with interactive products to visually communicate your data. Bring your data to life with our Real-Time Data Visualization dashboards. Data Visualization with Python uses widely used Visualization Packages like Matplotlib, Seaborn, ggplot, Altair, Bokeh, pygal, Plotly, geoplotlib and more.

Machine Learning As-A-Service

Unlock the value of your data to take smarter decisions. Don Machine Learning Consulting Services helps in achieving Business Insights. Access the computational and storage infrastructure and tools required to accelerate work in Deep Learning. Stay a step ahead of disruption and exceed customer expectation by implementing Predictive Analytics Solutions.