Your SlideShare is downloading. ×
Python Ireland Nov 2010 Talk: Unit Testing
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

Python Ireland Nov 2010 Talk: Unit Testing

415
views

Published on

Unit testing for those seeking instant gratification - Maciej Bliziński …

Unit testing for those seeking instant gratification - Maciej Bliziński

Abstract: Unit testing has long term benefits. However, depending on how you use it, it can have short term benefits too. This is an introductory talk, aimed at both beginner and experienced Python programmers who would like to get started testing their code.

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
415
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
6
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. Unit testing For those seeking instant gratificationMaciej Bliziński <blizinski@google.com> Python Ireland 2010-11-10
  • 2. Helps the designProvides live documentationHelps refactoring
  • 3. But Im hungry right now!
  • 4. I love to see my code run
  • 5. task: Index a list of dictionaries by one of the fieldsdef IndexBy(d_list, field_name): result = {} for d in d_list: result[d[field_name]] = d return result
  • 6. def MakePackageNameBySoname(soname): (continued...) """Find the package name based on the soname. for key in parsed: Returns a pair of pkgname, catalogname. if parsed[key]: """ keywords_pkgname[key] = SonameToStringWithChar(parsed[key], "-") def AddSeparator(d, sep): keywords_catalogname[key] = SonameToStringWithChar(parsed[key], "_") """Adds a separator based on the neighboring else: of two digits.""" keywords_pkgname[key] = "" dc = copy.copy(d) keywords_catalogname[key] = "" if dc["version"]: pkgname_list = [] if (dc["basename"][-1].isdigit() keywords_pkgname = AddSeparator(keywords_pkgname, "-") and pkgname_list.append( dc["version"][0].isdigit()): "CSW%(basename)s%(sep)s%(version)s" % keywords_pkgname) dc["sep"] = sep keywords_catalogname = AddSeparator(keywords_catalogname, "_") else: catalogname_list = [ dc["sep"] = "" "%(basename)s%(sep)s%(version)s" % keywords_catalogname, else: ] dc["sep"] = "" return pkgname_list, catalogname_list return dc soname_re = re.compile(r"(?P<basename>[w+]+([.-]+[w+]+)*)" r".so" r"(.(?P<version>[d.]+))?" r"$") m = soname_re.match(soname) if not m: # There was no ".so" component, so its hardo to figure out which one is # the name, but well try to figure out the numeric part of the soname. digits = "".join(re.findall(r"[0-9]+", soname)) alnum = "".join(re.findall(r"[a-zA-Z]+", soname)) parsed = { "basename": alnum, "version": digits, } else: parsed = m.groupdict() keywords_pkgname = {} keywords_catalogname = {} Real life example
  • 7. task: Index a list of dictionaries by one of the fieldsdef IndexBy(d_list, field_name): result = {} for d in d_list: result[d[field_name]] = d return result
  • 8. import example_1def main(): d = [{"foo": "a", "bar": "b"}, {"foo": "c", "bar": "d"}] print example_1.IndexBy(d, "foo")if __name__ == __main__: main()
  • 9. import unittestimport example_1import pprintclass IndexByUnitTest(unittest.TestCase): def testTwoElements(self): d = [{"foo": "a", "bar": "b"}, {"foo": "c", "bar": "d"}] pprint.pprint(example_1.IndexBy(d, "foo"))if __name__ == __main__: unittest.main()
  • 10. import unittestimport example_1class IndexByUnitTest(unittest.TestCase): def testTwoElements(self): d = [{"foo": "a", "bar": "b"}, {"foo": "c", "bar": "d"}] expected = { a: {foo: a, bar: b}, c: {foo: c, bar: d}, } self.assertEquals(expected, example_1.IndexBy(d, "foo"))if __name__ == __main__: unittest.main()
  • 11. blizinski@workstation ~/unit-test-talk $ python2.6 example_1_test.py.----------------------------------------------------------------------Ran 1 test in 0.000sOK
  • 12. Cultured way of playing with code
  • 13. Changing your code
  • 14. import unittestimport example_1class IndexByUnitTest(unittest.TestCase): def testTwoElements(self): d = [{"foo": "a", "bar": "b"}, {"foo": "c", "bar": "d"}, {"foo": "c", "bar": "e"}] expected = { a: {foo: a, bar: b}, c: {foo: c, bar: d}, } self.assertEquals(expected, example_1.IndexBy(d, "foo"))if __name__ == __main__: unittest.main()
  • 15. task: Index a list of dictionaries by one of the fieldsdef IndexBy(d_list, field_name): result = {} for d in d_list: result.setdefault(d[field_name], []) result[d[field_name]].append(d) return result
  • 16. Keeping a track record
  • 17. def testMakePackageNameDashesNoDashes(self): soname = "libpyglib-2.0-python.so.0" expected = ( [CSWlibpyglib2-0python0], [libpyglib2_0python0], ) self.assertEqual(expected, su.MakePackageNameBySoname(soname))def testMakePackageNameDashesNoDashesPython(self): soname = "libpython3.1.so.1.0" expected = ( [CSWlibpython3-1-1-0], [libpython3_1_1_0], ) self.assertEqual(expected, su.MakePackageNameBySoname(soname))
  • 18. The trust issueYour first steps in unit testing
  • 19. Hug me, I only look alien! (never mind my boxing gloves)
  • 20. Its about the way pieces of code fit together
  • 21. Your trust will gradually shift
  • 22. Part of the development process
  • 23. Its a tool which helps withsome of the everyday tasks.
  • 24. Further reading http://en.wikipedia.org/wiki/Unit_testing http://diveintopython.org/unit_testing/index.html Mock objects, stubs and fakesContact Maciej Bliziński <blizinski@google.com>
  • 25. ReferencesImages: http://commons.wikimedia.org/wiki/File:Pantheon_rome_inside_1-muniu.jpg by Muniu http://commons.wikimedia.org/wiki/File:Instant_miso_soup.jpg by Gleam http://www.flickr.com/photos/f-oxymoron/4203860207/sizes/l/in/photostream/ by f-oxymoron http://www.flickr.com/photos/jurvetson/1381322008/sizes/l/in/photostream/ by jurvetson http://www.flickr.com/photos/7332902@N05/3221210836/ by David ODriscoll http://www.flickr.com/photos/edsweeney/4212380812/sizes/o/in/photostream/ by Ed Sweeneyhttp://www.flickr.com/photos/jenny-pics/3230153121/sizes/l/in/photostream/ by jenny downing http://www.flickr.com/photos/oskay/265899766/ by Windell Oskay http://www.flickr.com/photos/alismith44/357361903/ by Ali West http://www.flickr.com/photos/cezaryborysiuk/3947857278/ by Cezary Borysiuk http://www.flickr.com/photos/wilhei/109403331/ by wilhei55 http://www.flickr.com/photos/antonymayfield/3221876089/ by antony_mayfield http://www.flickr.com/photos/ralphandjenny/4999895776/ by Ralph Daily