Skip to content

Commit

Permalink
Sort-of support for pytest!
Browse files Browse the repository at this point in the history
  • Loading branch information
acbart committed Mar 19, 2024
1 parent 3d50935 commit a131fd0
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
46 changes: 46 additions & 0 deletions pedal/assertions/testing_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,4 +186,50 @@ def __load_tests():
fields={'failures': failures + errors, 'successes': successes,
'tests': tests},
**kwargs)
return False

@CompositeFeedbackFunction()
def ensure_pytest_tests(test_count=None, report=MAIN_REPORT, **kwargs):
"""
Ensure that the student has not failed their own tests.
This is for the specific ``pytest`` library.
Args:
test_count (int): The number of tests that the student should have written. If `None`, any number is fine.
"""
# Call the unittest.main just in case it wasn't explicitly run
# Check whether the tests were successful
# Check that the right number of tests were run
# TODO: Make all of this more flexible, if people request it.

student = get_student_data()

test_names = {name for name, value in student.items() if name.startswith("test")}
result = run(f"""
__successes, __failures = 0, 0
for __a_test in [{", ".join(test_names)}]:
try:
__a_test()
__successes += 1
except:
__failures += 1
""", report=report)
successes = evaluate("__successes", report=report)
failures = evaluate("__failures", report=report)
tests_run = successes+failures
if test_count and tests_run == 0:
return gently("You are not unit testing the result.",
title="No Student Unit Tests",
label="no_student_tests", **kwargs)
elif test_count is not None and tests_run < test_count:
return gently("You have not written enough unit tests.",
label="not_enough_tests",
title="Not Enough Student Unit Tests", **kwargs)
elif failures + successes > 0:
return gently(f"{failures}/{tests_run} of your unit tests are not passing.",
label="failing_student_tests",
title="Student Unit Tests Failing",
fields={'failures': failures, 'successes': successes,
'tests': tests_run},
**kwargs)
return False
20 changes: 20 additions & 0 deletions tests/test_testing_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,25 @@ def test_unittest_library_explicit(self):
final = simple.resolve()
self.assertEqual("""2/3 of your unit tests are not passing.""", final.message.rstrip())

class TestPyTest(ExecutionTestCase):
def test_pytest_library(self):
clear_report()
set_source("""import pytest
def divide(a, b):
return a / b
def test_divide_success():
assert divide(1, 1) == 1
def test_divide_error():
assert divide(1, 0) == 0
def test_divide_failure():
assert divide(1, 2) == 2""")
run()
ensure_pytest_tests()
final = simple.resolve()
self.assertEqual("""2/3 of your unit tests are not passing.""", final.message.rstrip())

if __name__ == '__main__':
unittest.main(buffer=False)


0 comments on commit a131fd0

Please sign in to comment.