diff --git a/pedal/assertions/testing_libraries.py b/pedal/assertions/testing_libraries.py index dc8d4c5..e5db0e0 100644 --- a/pedal/assertions/testing_libraries.py +++ b/pedal/assertions/testing_libraries.py @@ -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 \ No newline at end of file diff --git a/tests/test_testing_libraries.py b/tests/test_testing_libraries.py index ce74b2e..f477ab0 100644 --- a/tests/test_testing_libraries.py +++ b/tests/test_testing_libraries.py @@ -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) + +