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.