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:
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.
Share this highlighthttp://www.safaribooksonline.com/a/python-testing-with/20917068/