• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
unittest in 5 minutes
 

unittest in 5 minutes

on

  • 974 views

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.

Statistics

Views

Total Views
974
Views on SlideShare
974
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    unittest in 5 minutes unittest in 5 minutes Presentation Transcript

    • unittestin five minutesRay Toal 2011-10-22
    • 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
    • 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!
    • 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
    • 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]
    • 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)
    • Write a stub (interleave.py)def interleave(a, b): return None
    • 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)
    • 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]
    • 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)
    • 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]
    • Rerun the test$ python interleavetest.py.-------------------------------------------------------------Ran 1 test in 0.000sOK
    • kthx