Introduction to TDD using python
TDD
(Test Driven Development)
"approach to development which combines test-first development where you write a test before you write just enough production code to fulfill that test and refactoring.

“The act of writing a unit test is more an act of design than of verification. It is also more an act of documentation than of verification. The act of writing a unit test closes a remarkable number of feedback loops, the least of which is the one pertaining to verification of function” - Robert C. Martin
Traditional software process
Design
Implement
Test
Traditional software process
Issues
- Long phase execution
- Manual testing
- Lost or regression bugs
- The bigger the codebase the longer the time to test and more probability to break things
TDD Flow
Test / Design
Implement
Benefits
- Much less regression test bugs
- Drastically reduced time to identify issues
- Easy refactoring verification.
- Better software design.
Python testing tools
Unittest
Doctest
py.test
Nose
Tox
Unittest2
Mock
Lets see some code
import unittest
from operations.operations import factorial, summation
class TddInPythonExample(unittest.TestCase):
def test_factorial_operation_returns_correct_result(self):
result = factorial(2)
self.assertEqual(2, result)
def test_summation_operation_returns_correct_result(self):
result = summation(2)
self.assertEqual(3, result)
test_operations.py
# coding=utf-8
def factorial(number):
return 0
def summation(number):
return 0
operations.py
/usr/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --multiproc
--qt-support --client 127.0.0.1 --port 64753 --file /Applications/PyCharm.app/Contents
/helpers/pycharm/utrunner.py /Users/jhonjairoscalablepath/PycharmProjects/
python-lettuce-sample-2/tests/test_operations.py::TddInPythonExample::
test_summation_operation_returns_correct_result true
Testing started at 4:42 PM ...
pydev debugger: process 55353 is connecting
Connected to pydev debugger (build 143.1919)
Failure
Traceback (most recent call last):
File "/Users/jhonjairoscalablepath/PycharmProjects/python-lettuce-sample-2/tests
/test_operations.py", line 13, in test_summation_operation_returns_correct_result
self.assertEqual(3, result)
AssertionError: 3 != 0
Process finished with exit code 0
Test fail !!! :(
Don't be sad!
Now you have things to do :D
operations.py
# coding=utf-8
def factorial(number):
number = int(number)
if (number == 0) or (number == 1):
return 1
else:
return number*factorial(number-1)
def summation(number):
number = int(number)
if number == 0:
return 0
else:
return number+summation(number-1)
/usr/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py --multiproc
--qt-support --client 127.0.0.1 --port 64816 --file /Applications/PyCharm.app/
Contents/helpers/pycharm/utrunner.py /Users/jhonjairoscalablepath/PycharmProjects/
python-lettuce-sample-2/tests/test_operations.py::TddInPythonExample::
test_summation_operation_returns_correct_result true
Testing started at 4:45 PM ...
pydev debugger: process 55376 is connecting
Connected to pydev debugger (build 143.1919)
Process finished with exit code 0
Test pass !!! :'D
ATDD
(Acceptance Test Driven Development)
is a communication tool between the customer, developer, and tester to ensure that the requirements are well-defined

"Customer"
or
Product Owner

Tester

Developer

requirements written as acceptance tests
focus on writing acceptance tests before developers begin coding
"the product owner, customer or domain expert is able to specify new functionality by writing new acceptance tests or test cases, without needing to consult developers"
What's Next
BDD?
TDD vs BDD
TDD

Product Owner

Tester

Developer
TDD
"We have a coverage of 95% of the code!!!"
Emmm ok, cool.
BDD

Product Owner

Tester

Developer
BDD
"We have completed 95% of the features!!!"
THAT'S AWESOME!!!!!
Yes, I can notice that.
"Not only test your code at the granular level with unit tests, but also test your application end to end, using acceptance tests"
TDD
"the unit tests of a class"
"the specifications of the behavior of the class"
BDD
TDD
"focussed on developers and testers"
"focused on customer"
BDD
TDD
"focussed on implementation"
"focused on behavior"
BDD
TDD
"test code blocks"
"test what customer expects from the platform"
BDD
Resources
More resources
http://inviqa.com/insights/bdd-guide
http://dannorth.net/introducing-bdd/
http://dannorth.net/whats-in-a-story/
http://pythonhosted.org/behave/philosophy.html
BDD Testing a Restful Web Application in Python
http://www.slideshare.net/Siddhi/test-driven-development-with-python
http://chrismdp.com/2013/01/bdd-is-not-cucumber/
http://lizkeogh.com/2015/12/14/bdd-a-three-headed-monster/
https://www.toptal.com/freelance/your-boss-won-t-appreciate-tdd-try-bdd
Copy of Introduction to TDD and BDD using python
By swapps
Copy of Introduction to TDD and BDD using python
Review of TDD and BDD main characteristics , how do they get along and how they are implemented in python
- 945