Python unit test that nothing was executed

Solution 1:

"nothing extra is executed .."

Using coverage you can see what has been executed and therefore also see what has not been executed.

You'll get a report and see what your tests didn't cover. For example:

$ coverage report -m
Name                      Stmts   Miss  Cover   Missing
-------------------------------------------------------
my_program.py                20      4    80%   33-35, 39
my_other_module.py           56      6    89%   17-23
-------------------------------------------------------
TOTAL                        76     10    87%

It also supports the generation of HTML files so you get a visualization too if needed. In the docs find this example.

If you run my_method(5), coverage will therefore show you what parts of your program weren't executed.

".. or changed outside my_method"

I have never done something like this and I am not sure if it's possible in a sensible way but I found this question which refers to tracemalloc. Maybe this module can help you.