|
1 | 1 | import unittest
|
2 | 2 | import pytest
|
3 | 3 |
|
4 |
| - |
| 4 | +# For this first exercise, it is really important to be clear about how we are importing names for tests. |
| 5 | +# To that end, we are putting a try/catch around imports and throwing specific messages to help students |
| 6 | +# decode that they need to create and title their constants and functions in a specific way. |
5 | 7 | try:
|
6 | 8 | from lasagna import (EXPECTED_BAKE_TIME,
|
7 | 9 | bake_time_remaining,
|
8 | 10 | preparation_time_in_minutes,
|
9 | 11 | elapsed_time_in_minutes)
|
10 | 12 |
|
11 |
| - |
| 13 | +# Here, we are separating the constant import errors from the function name import errors |
12 | 14 | except ImportError as import_fail:
|
13 | 15 | message = import_fail.args[0].split('(', maxsplit=1)
|
14 | 16 | item_name = import_fail.args[0].split()[3]
|
15 | 17 |
|
16 |
| - if 'EXPECTED_BAKE_TIME' in message: |
| 18 | + if 'EXPECTED_BAKE_TIME' in item_name: |
17 | 19 | # pylint: disable=raise-missing-from
|
18 |
| - raise ImportError(f'We can not find or import the constant {item_name} in your' |
19 |
| - " 'lasagna.py' file. Did you mis-name or forget to define it?") |
| 20 | + raise ImportError(f'\n\nMISSING CONSTANT --> \nWe can not find or import the constant {item_name} in your' |
| 21 | + " 'lasagna.py' file.\nDid you mis-name or forget to define it?") from None |
20 | 22 | else:
|
21 | 23 | item_name = item_name[:-1] + "()'"
|
22 | 24 | # pylint: disable=raise-missing-from
|
23 |
| - raise ImportError("In your 'lasagna.py' file, we can not find or import the" |
24 |
| - f' function named {item_name}. Did you mis-name or forget to define it?') |
| 25 | + raise ImportError("\n\nMISSING FUNCTION --> In your 'lasagna.py' file, we can not find or import the" |
| 26 | + f' function named {item_name}. \nDid you mis-name or forget to define it?') from None |
25 | 27 |
|
26 | 28 |
|
| 29 | +# Here begins the formal test cases for the exercise. |
27 | 30 | class LasagnaTest(unittest.TestCase):
|
28 | 31 |
|
29 | 32 | @pytest.mark.task(taskno=1)
|
@@ -64,9 +67,16 @@ def test_elapsed_time_in_minutes(self):
|
64 | 67 |
|
65 | 68 | @pytest.mark.task(taskno=5)
|
66 | 69 | def test_docstrings_were_written(self):
|
| 70 | + """Validate function.__doc__ exists for each function. |
| 71 | + Check the attribute dictionary of each listed function |
| 72 | + for the presence of a __doc__ key. |
| 73 | +
|
| 74 | + :return: unexpectedly None error when __doc__ key is missing. |
| 75 | + """ |
67 | 76 | functions = [bake_time_remaining, preparation_time_in_minutes, elapsed_time_in_minutes]
|
68 | 77 |
|
69 | 78 | for variant, function in enumerate(functions, start=1):
|
70 | 79 | with self.subTest(f'variation #{variant}', function=function):
|
71 | 80 | failure_msg = f'Expected a docstring for `{function.__name__}`, but received `None` instead.'
|
| 81 | + # Check that the __doc__ key is populated for the function. |
72 | 82 | self.assertIsNotNone(function.__doc__, msg=failure_msg)
|
0 commit comments