O'Reilly logo
  • Chris Kang thinks this is interesting:

The problem is that pytest treats each docstring with code as a different test. The import in the top docstring will allow the first part to pass, but the code in the docstrings of the functions will fail:


Cover of Python Testing with pytest


Each set of doc string are independent. So, if each such independent doc string doesn't contain everything it needs to run the test, it will fail. You could add "import" statement on each docstring section.