0

I am teaching an introductory python course (python 3+Jupyter), and have been formulating assignments using nbgrader. For those not familiar, this basically means marking student's code via a set of assert statements. If the assert doesn't pass, they don't get the mark.

One of the tests that I want to perform is to check that students are writing their own tests. As a very simple example, let's imagine that they're supposed to be defining my_function, and all the tests that they want to run on it are supposed to be a series of assert statements inside a function do_tests() which will return True if all the tests pass.

One thing that I can obviously require of their code is that do_tests() passes simply by calling assert do_tests(). I can also check that it fails if I del my_function. However, I also want to check a bit more detail about the content of do_tests(). As a first step, I simply wanted to count the number of assert statements that they have used within the definition, and was intending to use unittest.mock.patch, trying to adapt the code from here. However, I could not figure out how to mock assert. I tried something like

from unittest.mock import patch
with patch('__main__.assert') as mock_assert:
    do_tests()

but I just get an error that main does not have a method assert, and couldn't work out what module assert should be a part of.

As a crude interim, I have ended up doing

import inspect
lines = inspect.getsource(do_tests)
assert lines.count("\n    assert")>=3,"You don't appear to have enough assert statements"

but obviously that doesn't give me access to any other features that mocking might offer.

DaftWullie
  • 101
  • 2
  • https://stackoverflow.com/questions/24478727/how-to-list-available-tests-with-python – Joe Nov 25 '19 at 14:29
  • https://stackoverflow.com/questions/9004455/how-can-i-extract-a-list-of-testcases-from-a-testsuite – Joe Nov 25 '19 at 14:31
  • [assert](https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement) is a statement, not a `builtin`. I'm not sure mock has a way to patch them. – currand60 Nov 25 '19 at 14:34
  • You could tell them to use one assert per test and count tests as mentioned above. – Joe Nov 25 '19 at 14:45

0 Answers0