Created
March 30, 2012 02:54
-
-
Save pckujawa/2246059 to your computer and use it in GitHub Desktop.
methods of unit testing in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Taken from http://artifex.org/~hblanks/talks/2011/pep20_by_example.html | |
################################### 7 ################################## | |
""" Write out the tests for a factorial function. """ | |
#----------------------------------------------------------------------- | |
def factorial(n): | |
""" | |
Return the factorial of n, an exact integer >= 0. | |
>>> [factorial(n) for n in range(6)] | |
[1, 1, 2, 6, 24, 120] | |
>>> factorial(30) | |
265252859812191058636308480000000L | |
>>> factorial(-1) | |
Traceback (most recent call last): | |
... | |
ValueError: n must be >= 0 | |
""" | |
pass | |
if __name__ == '__main__' and '--test' in sys.argv: | |
import doctest | |
doctest.testmod() | |
#----------------------------------------------------------------------- | |
import unittest | |
def factorial(n): | |
pass | |
class FactorialTests(unittest.TestCase): | |
def test_ints(self): | |
self.assertEqual( | |
[factorial(n) for n in range(6)], [1, 1, 2, 6, 24, 120]) | |
def test_long(self): | |
self.assertEqual( | |
factorial(30), 265252859812191058636308480000000L) | |
def test_negative_error(self): | |
with self.assertRaises(ValueError): | |
factorial(-1) | |
if __name__ == '__main__' and '--test' in sys.argv: | |
unittest.main() | |
#----------------------------------------------------------------------- | |
print 'Readability counts.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from a nice example of 'pythonic' style at http://artifex.org/~hblanks/talks/2011/pep20_by_example.html