Python 3

python-pixabay

Want a Python 3 Pixabay’s API wrapper that is easy to use and complete enough to avoid dealing with raw routes, json and url parameters? python-pixabay may be your option.

Advertisements
Python, Python 3

Python 3 – Test Driven Development?

Testing

You might be thinking

Another obsessive guy on software testing…

I do not being obsess over a single technique for a better quality product, but it is one of the great way to achieve that. You might be hearing that “unit testing is the best way to ensure your software to be error and bug free.

I just don’t believe that.

Best always depends on the situation you’re dealing with, and most of the times when you expect your project to grow bigger, than you would find out running and deploying your software is like putting an anvil on a glass table that that barely can handle the weight.

Although, most of us know that software testing is important, we hardly find the fun doing it. In fact, bad testing techniques can cause more harm than good to both codebase and the developer.

So, here I am to talk about Test Driven Development (famously known as TDD), and I gonna use Python 3 and unittest framework as my medium to present a demo on building a simple addition and multiplication module.

Test Driven Development Testing Model

  1. Write a test
  2. Run & watch the test fail
  3. Implement the code to pass the test
  4. Run & watch the test pass
  5. Refactor

1. Write a test

Please create a blank file, named as cal.py in current directory first,
before writing the test code below.

Note: I’m gonna separate the test code into 01_cal.py

import unittest
from cal import *


class MainTest(unittest.TestCase):
def test_add(self):
self.assertEqual(add(5, 5), 10)

def test_sub(self):
self.assertEqual(sub(2, 2), 0)


 

2. Run & watch the test fail

python3 -m unittest 01_cal.py
EE
======================================================================
ERROR: test_add (01_cal.MainTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/lala/01_cal.py", line 5, in test_add
self.assertEqual(add(5, 5), 10)
NameError: name 'add' is not defined

======================================================================
ERROR: test_sub (01_cal.MainTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/tmp/lala/01_cal.py", line 8, in test_sub
self.assertEqual(sub(2, 2), 0)
NameError: name 'sub' is not defined

----------------------------------------------------------------------
Ran 2 tests in 0.001s

FAILED (errors=2)

3. Implement the code to pass the test

Note: We editing the cal.py

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

def sub(a, b):
return a - b

4. Run and watch the test pass

python3 -m unittest 01_cal.py
..
----------------------------------------------------------------------
Ran 2 tests in 0.002s

OK

Tips: You can also use nosetests as an alternative to test your code.

 

5. Refactor

Since this is a very simple demo, we don’t need to refactor it the codebase. However, you should refactor if you sense a better and a cleaner way for easier future maintainance (you’ll never know!)

 

Closing

Test Driven Development is just another technique to aid you in development, and I can guarantee you that you can reduce your total bugs in a single version, if you use the right way and for the right job.