|
1 | 1 | import check50
|
2 | 2 | import re
|
| 3 | +import ast |
3 | 4 |
|
4 | 5 |
|
5 | 6 | @check50.check()
|
@@ -82,23 +83,65 @@ def test_number_functions():
|
82 | 83 | @check50.check(test_student_file_passes)
|
83 | 84 | def test_named_functions():
|
84 | 85 | """test_jar.py defines test_init, test_str, test_deposit, and test_withdraw"""
|
85 |
| - with open("test_jar.py") as t: |
86 |
| - contents = t.read() |
87 |
| - |
88 |
| - funcs = ["test_init", "test_str", "test_deposit", "test_withdraw"] |
89 |
| - for func in funcs: |
90 |
| - matches = re.search(rf"def\s+{func}\s*\(", contents) |
91 |
| - if not matches: |
92 |
| - raise check50.Failure(f"{func} not found in test_jar.py") |
| 86 | + result = check50.run("pytest test_jar.py --collect-only -q").stdout() |
| 87 | + collected_tests = [] |
| 88 | + for line in result.strip().split('\n'): |
| 89 | + if '::test_' in line: |
| 90 | + test_name = line.split('::')[1].strip() |
| 91 | + collected_tests.append(test_name) |
| 92 | + |
| 93 | + required_funcs = ["test_init", "test_str", "test_deposit", "test_withdraw"] |
| 94 | + missing_funcs = [] |
| 95 | + for func in required_funcs: |
| 96 | + if func not in collected_tests: |
| 97 | + missing_funcs.append(func) |
| 98 | + |
| 99 | + if missing_funcs: |
| 100 | + raise check50.Failure( |
| 101 | + f"Function(s) not collected by pytest in test_jar.py: {', '.join(missing_funcs)}") |
93 | 102 |
|
94 | 103 |
|
95 | 104 | @check50.check(test_student_file_passes)
|
96 | 105 | def test_valid_testing():
|
97 | 106 | """test_jar.py contains implemented functions"""
|
98 |
| - |
99 |
| - # https://stackoverflow.com/questions/845058/how-to-get-the-line-count-of-a-large-file-cheaply-in-python |
100 |
| - with open("test_jar.py", "rb") as t: |
101 |
| - num_lines = sum(1 for _ in t) |
| 107 | + with open("test_jar.py") as f: |
| 108 | + contents = f.read() |
102 | 109 |
|
103 |
| - if num_lines < 20: |
104 |
| - raise check50.Failure("test_jar.py functions not implemented") |
| 110 | + try: |
| 111 | + tree = ast.parse(contents) |
| 112 | + except SyntaxError: |
| 113 | + raise check50.Failure("test_jar.py contains syntax errors") |
| 114 | + |
| 115 | + required_funcs = ["test_init", "test_str", "test_deposit", "test_withdraw"] |
| 116 | + implemented_funcs = [] |
| 117 | + |
| 118 | + for node in ast.walk(tree): |
| 119 | + if isinstance(node, ast.FunctionDef) and node.name in required_funcs: |
| 120 | + has_test_content = False |
| 121 | + |
| 122 | + # Walk through all nodes in the function to find test-related code |
| 123 | + for child in ast.walk(node): |
| 124 | + # Check for assert statements |
| 125 | + if isinstance(child, ast.Assert): |
| 126 | + has_test_content = True |
| 127 | + break |
| 128 | + # Check for pytest.raises or with statements (for context managers) |
| 129 | + elif isinstance(child, ast.With): |
| 130 | + has_test_content = True |
| 131 | + break |
| 132 | + # Check for calls to pytest functions or assert methods |
| 133 | + elif isinstance(child, ast.Call): |
| 134 | + if isinstance(child.func, ast.Attribute): |
| 135 | + # Check for pytest.* or self.assert* calls |
| 136 | + if (hasattr(child.func.value, 'id') and child.func.value.id == 'pytest') or \ |
| 137 | + child.func.attr.startswith('assert'): |
| 138 | + has_test_content = True |
| 139 | + break |
| 140 | + |
| 141 | + if has_test_content: |
| 142 | + implemented_funcs.append(node.name) |
| 143 | + |
| 144 | + missing_or_empty = set(required_funcs) - set(implemented_funcs) |
| 145 | + if missing_or_empty: |
| 146 | + raise check50.Failure( |
| 147 | + f"Missing or empty function(s) in test_jar.py: {', '.join(sorted(missing_or_empty))}") |
0 commit comments