in five minutes Ray Toal 20111022 How to unit test Not manually thats for sure You write code that exercises your code Perform assertions that record that you get the results you expect for various inputs ID: 320716
Download Presentation The PPT/PDF document "unittest" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
unittest
in five
minutes
Ray Toal 2011-10-22Slide2
How to unit test
Not manually, that's for sure
You write code that exercises your code
Perform assertions that record
that you get the results you expect for various inputs
that exceptions are raised when they should be
Pay attention to edge casesSlide3
Test-driven development
Write some tests (yes, first)
Run them
They will fail because the code isn't written yet. That is supposed to happen. Good news is you will know the tests run.
Write some code
Now run the tests again
Work until the tests pass
Iterate!Slide4
Unit testing in Python
The module
unittest
is already there
Docs
Python 2.7
Python 3
Also see
The unit testing chapter from
Dive Into Python
Are there alternatives to unittest?
See what they say on StackOverflowSlide5
A first example
Let's write a function to interleave two lists
It will be okay if one list is longer than the other
Before we start writing the code, we should know what the function should produce for all types of inputs:
interleave([], []) ☞ []
interleave([1,5,3], ["hello"]) ☞ [1,"hello",5,3]
interleave([True], [[], 8]) ☞ [True, [], 8]Slide6
Write the test
first
(
interleavetest.py)
from interleave import interleave
import
unittest
class
TestGettingStartedFunctions(unittest.TestCase):
def test_interleave(self):
cases = [
([], [], []),
([1, 4, 6], [], [1, 4, 6]),
([], [2, 3], [2, 3]),
([1], [9], [1, 9]),
([8, 8, 3, 9], [1], [8, 1, 8, 3, 9]),
([2], [7, 8, 9], [2, 7, 8, 9]),
]
for a, b, expected in cases:
self.assertEqual(interleave(a, b), expected)Slide7
Write a stub (interleave.py)
def interleave(a, b):
return NoneSlide8
Run the test
$ python interleavetest.py
F
======================================================================
FAIL: test_interleave (__main__.TestGettingStartedFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
File "interleavetest.py", line 15, in test_interleave
self.assertEqual(interleave(a, b), expected)
AssertionError: None != []
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)Slide9
Now write the code
def interleave(a, b):
"""Return the interleaving of two sequences as a list."""
return [y for x in izip_longest(a, b) for y in x if y is not None]
Slide10
Test again
$ python interleavetest.py
E
======================================================================
ERROR: test_interleave (__main__.TestGettingStartedFunctions)
----------------------------------------------------------------------
Traceback (most recent call last):
File "interleavetest.py", line 15, in test_interleave
self.assertEqual(interleave(a, b), expected)
File "/Users/raytoal/scratch/interleave.py", line 3, in interleave
return [y for x in izip_longest(a, b) for y in x if y is not None]
NameError: global name 'izip_longest' is not defined
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1
)Slide11
Fix the code
from itertools import izip_longest
def interleave(a, b):
"""Return the interleaving of two sequences as a list."""
return [y for x in izip_longest(a, b) for y in x if y is not None]Slide12
Rerun the test
$ python interleavetest.py
.
--------------------------------------------------
--
---------
Ran 1 test in 0.000s
OKSlide13
kthx