Your SlideShare is downloading. ×
unittest in 5 minutes
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

unittest in 5 minutes

767
views

Published on

A lightning talk on using test-driven development with Python's unittest module.

A lightning talk on using test-driven development with Python's unittest module.

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
767
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. unittestin five minutesRay Toal 2011-10-22
  • 2. How to unit test• Not manually, thats for sure• You write code that exercises your code• Perform assertions that record o that you get the results you expect for various inputs o that exceptions are raised when they should be• Pay attention to edge cases
  • 3. Test-driven development• Write some tests (yes, first)• Run them o They will fail because the code isnt 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!
  • 4. 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 StackOverflow
  • 5. A first example• Lets 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]
  • 6. Write the test first(interleavetest.py)from interleave import interleaveimport unittestclass 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)
  • 7. Write a stub (interleave.py)def interleave(a, b): return None
  • 8. Run the test$ python interleavetest.pyF======================================================================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.000sFAILED (failures=1)
  • 9. Now write the codedef 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]
  • 10. Test again$ python interleavetest.pyE======================================================================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.000sFAILED (errors=1)
  • 11. Fix the codefrom itertools import izip_longestdef 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]
  • 12. Rerun the test$ python interleavetest.py.-------------------------------------------------------------Ran 1 test in 0.000sOK
  • 13. kthx