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_2.py (1348B)


      1 """
      2 This test suite (a module) runs tests for subpkg_1.module_2 of the
      3 cs107_package.
      4 """
      5 
      6 # Let us use `pytest` this time
      7 import pytest
      8 
      9 # project code to test (requires `cs107_package` to be in PYTHONPATH)
     10 from cs107_package.subpkg_1.module_2 import (bar)
     11 
     12 
     13 class TestFunctions:
     14     """We do not inherit from unittest.TestCase for pytest's!"""
     15 
     16     def test_bar(self):
     17         """
     18         This is just a trivial test to check the return value of function `bar`.
     19         """
     20         # assert the return value of bar() (note that this uses Python's
     21         # `assert` statement directly, no need to inherit from anything!)
     22         assert bar() == "cs107_package.subpkg_1.module_2.bar()"
     23 
     24 
     25 # ==============================================================================
     26 # A test function unrelated to `cs107_package`.  It is here to demonstrate the
     27 # feature of `pytest` used in `test_example_function` below.
     28 def example_function():
     29     """If you have code that raises exceptions, pytest can verify them."""
     30     raise RuntimeError("This function should not be called")
     31 
     32 
     33 def test_example_function():
     34     with pytest.raises(RuntimeError):
     35         example_function()
     36 
     37 
     38 # ==============================================================================
     39 
     40 if __name__ == "__main__":
     41     # can use this to run the test module standalone
     42     pytest.main()