import unittest from fizzbuzz import * # both of the above commands import the named libraries. The difference is # that in order to refer to something in the unittest module, you must # indicate that you are referring to a member of that module with dot # notation, eg unittest.TestCase, the functions on fizzbuzz don't require # the contextual information. # Python's unit test framework comes with the main system. # Library: pyunit (called unittest in the python 2.7 release) # Documentation: # python library docs: http://docs.python.org/library/unittest.html # pyunit docs: http://pyunit.sourceforge.net/pyunit.html class FizzBuzz(unittest.TestCase): def setUp(self): """Setup is run every time a given test is executed.""" self.justBy3 = [num for num in range(1, 101) if isfizz(num) and not isbuzz(num)] self.justBy5 = [num for num in range(1, 101) if isbuzz(num) and not isfizz(num)] self.by3and5 = [num for num in range(1, 101) if isfizz(num) and isbuzz(num)] self.notBy3and5 = [num for num in range(1, 101) if not (isfizz(num) or isbuzz(num))] def testFizzBy3(self): """Verifies that if a number is divisble by 3, it returns fizz""" for num in self.justBy3: result = fizzbuzzSingle(num) self.assertEqual(result, "fizz") def testComprehensionAndFor(self): """Verifies that the results of fizzbuzz through comprehension and for loops are the same.""" for num in range(1, 101): forResult = fizzbuzzFor(num) compResult = fizzbuzzComprehension(num) self.assertEqual(forResult, compResult) def testMustBePositive(self): """fizzbuzzSingle should throw an error when given a non-positive value""" try: fizzbuzzSingle(0) except AssertionError: pass else: fail("Expected an assertion error, non-positive should fail.") if __name__ == "__main__": # Definitely a nice feature, this automatically finds all of the classes # that are subclassed from TestCase and runs any method that starts with # 'test'. unittest.main()