~uninja/python-snippets/xml-snippets

« back to all changes in this revision

Viewing changes to unittest/unittests.py

  • Committer: Jono Bacon
  • Date: 2010-04-01 05:28:53 UTC
  • mfrom: (77.1.1 python-snippets)
  • Revision ID: jono@system76-pc-20100401052853-4xmid1zgyao8f1y4
Thanks David for some more testing snippets!

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#
3
 
# [SNIPPET_NAME: Unit Tests]
4
 
# [SNIPPET_CATEGORIES: unittest]
5
 
# [SNIPPET_DESCRIPTION: Example of basic Python unit testing]
6
 
# [SNIPPET_DOCS: http://docs.python.org/library/unittest.html]
7
 
# [SNIPPET_AUTHOR: David Futcher <bobbo@ubuntu.com>]
8
 
# [SNIPPET_LICENSE: MIT]
9
 
 
10
 
import random
11
 
import unittest
12
 
 
13
 
FIB_STOP = 10
14
 
 
15
 
# Adapted from "Writing generators" snippet by Josh Holland <jrh@joshh.co.uk>
16
 
def fibonacci(start=(0, 1), stop=FIB_STOP):
17
 
    a, b = start
18
 
    while stop:
19
 
        yield a
20
 
        a, b = b, a + b
21
 
        stop -= 1
22
 
 
23
 
class FibonacciGeneratorTest(unittest.TestCase):
24
 
    """ Basic unit test class to check the above Fibonacci generator """
25
 
 
26
 
    def setUp(self):
27
 
        self.fibs = []
28
 
        self.correct = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
29
 
 
30
 
        for x in fibonacci():
31
 
            self.fibs.append(x)
32
 
    
33
 
    def testStopping(self):
34
 
        # Check the generator stopped when it should have
35
 
        self.assertEqual(FIB_STOP, len(self.fibs))
36
 
 
37
 
    def testNumbers(self):
38
 
        # Check the generated list against our known correct list
39
 
        for i in range(len(self.correct)):
40
 
            self.assertEqual(self.fibs[i], self.correct[i])
41
 
 
42
 
if __name__ == '__main__':
43
 
    unittest.main()
44