cs107-lecture-examples

Example codes used during Harvard CS107 lectures
git clone https://git.0xfab.ch/cs107-lecture-examples.git
Log | Files | Refs | README | LICENSE

test_module_1.py (1312B)


      1 """
      2 This test suite (a module) runs tests for subpkg_1.module_1 of the
      3 cs107_package.
      4 """
      5 
      6 # Here we use `unittest` from the Python standard library
      7 import unittest
      8 
      9 # project code to test (requires `cs107_package` to be in PYTHONPATH)
     10 from cs107_package.subpkg_1.module_1 import (Foo, foo)
     11 
     12 
     13 # test classes must start with `Test` (classes are required for `unittest` tests)
     14 class TestTypes(
     15     unittest.TestCase
     16 ):  # test classes must inherit from unittest.TestCase
     17 
     18     # test methods (functions) must be prepended with `test_`.
     19     def test_class_Foo(self):
     20         """
     21         This is just a trivial test to check that `Foo` is initialized
     22         correctly.  More tests associated to the class `Foo` could be written in
     23         this method.
     24         """
     25         f = Foo(1, 2)  # create instance
     26         self.assertEqual(f.a, 1)  # check attribute `a`
     27         self.assertEqual(f.b, 2)  # check attribute `b`
     28 
     29 
     30 class TestFunctions(unittest.TestCase):
     31 
     32     def test_function_foo(self):
     33         """
     34         This is just a trivial test to check the return value of function `foo`.
     35         """
     36         # assert the return value of foo()
     37         self.assertEqual(foo(), "cs107_package.subpkg_1.module_1.foo()")
     38 
     39 
     40 if __name__ == '__main__':
     41     # can use this to run the test module standalone
     42     unittest.main()