python testing fundamentals

153
mage Python Testing Fundamentals Saturday, July 28, 2012 PyOhio Ohio State University Columbus, OH Chris Calloway University of North Carolina Department of Marine Sciences Python Testing Fundamentals PyCamp™ Programming For The People Copyright © 2012 Trizpug

Upload: cbcunc

Post on 19-May-2015

1.731 views

Category:

Documents


1 download

DESCRIPTION

Slides from PyOhio, a brief tour of unittest and doctest.

TRANSCRIPT

Page 29: Python Testing Fundamentals

Programming For The People

Unittest Module

>>> import unittest

>>> dir(unittest)

['BaseTestSuite', 'FunctionTestCase', 'SkipTest', 'TestCase', 'TestLoader', 'TestProgram', 'TestResult', 'TestSuite', 'TextTestResult', 'TextTestRunner', '_TextTestResult', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__unittest', 'case', 'defaultTestLoader', 'expectedFailure', 'findTestCases', 'getTestCaseNames', 'installHandler', 'loader', 'main', 'makeSuite', 'registerResult', 'removeHandler', 'removeResult', 'result', 'runner', 'signals', 'skip', 'skipIf', 'skipUnless', 'suite', 'util']

>>>

Copyright © 2012

TrizpugPyCamp™

Page 47: Python Testing Fundamentals

Programming For The People

TestCase Class

> python test_operator.py F..======================================FAIL: test_add (__main__.TestOperator)Test the add function.--------------------------------------Traceback (most recent call last): File "test_operator.py", line 13, in test_add self.assertEqual(operator.add(2, 2), 2 + 3)AssertionError: 4 != 5

--------------------------------------Ran 3 tests in 0.082s

FAILED (failures=1)>

Copyright © 2012

TrizpugPyCamp™

Page 49: Python Testing Fundamentals

Programming For The People

TestCase Class

> python test_operator.py E..================================================ERROR: test_add (__main__.TestOperator)Test the add function.------------------------------------------------Traceback (most recent call last): File "test_operator.py", line 13, in test_add self.assertEqual(operator.add(2, 2), 2 + "2")TypeError: unsupported operand type(s) for +:int' and 'str'

------------------------------------------------Ran 3 tests in 0.001s

FAILED (errors=1)>

Copyright © 2012

TrizpugPyCamp™

Page 57: Python Testing Fundamentals

Programming For The People

TestResult Class

> python -m unittest -hUsage: python -m unittest [options] [tests]

Options: -h, --help Show this message -v, --verbose Verbose output -q, --quiet Minimal output -f, --failfast Stop on first failure -c, --catch Catch control-C and display results -b, --buffer Buffer stdout and stderr during test runs[tests] can be a list of any number of testmodules, classes and test methods.

Copyright © 2012

TrizpugPyCamp™

Page 61: Python Testing Fundamentals

Programming For The People

TestCase Class

Copyright © 2012

TrizpugPyCamp™

Method Tests If

assertEqual(a, b) a == b

assertNotEqual(a, b) a != b

assertTrue(x) bool(x) is True

assertFalse(x) bool(x) is False

assertIs(a, b) a is b

assertIsNot(a, b) a is not b

assertIsNone(x) x is None

assertIsNotNone(x) x is not None

assertIn(a, b) a in b

assertNotIn(a, b) a not in b

assertIsInstance(a, b) isinstance(a, b)

assertNotIsInstance(a, b) not isinstance(a, b)

Page 66: Python Testing Fundamentals

Programming For The People

TestCase Class

Copyright © 2012

TrizpugPyCamp™

Method Tests If

assertAlmostEqual(a, b) round(a-b, 7) == 0

assertNotAlmostEqual(a, b) round(a-b, 7) != 0

assertGreater(a, b) a > b

assertGreaterEqual(a, b) a >= b

assertLess(a, b) a < b

assertLessEqual(a, b) a <= b

assertRegex(s, re) s matches regex

assertNotRegex(s, re) s does not match regex

assertCountEqual(a, b)a and b have the same elements in the same number, regardless of order

Page 67: Python Testing Fundamentals

Programming For The People

TestCase Class

Copyright © 2012

TrizpugPyCamp™

Method Compares

assertMultiLineEqual(a, b) Strings

assertSequenceEqual(a, b) Sequences

assertListEqual(a, b) Lists

assertTupleEqual(a, b) Tuples

assertSetEqual(a, b) Sets and Frozensets

assertDictEqual(a, b) Dictionaries