You can run some doctests from pytest, according to the documentation.
However, it can be a bit fiddly.

With my examples of putting doctests in text files, if I just try running:

$ pytest test_unnecessary_math.txt

I get the error:

... ModuleNotFoundError: No module named 'unnecessary_math'

It doesn’t seem to like the line “from unnecessary_math import multiply” from the .txt file, even thought he file unnecessary_math is in the same directory.

However, if I throw that same like into a conftest.py file, it seems to work:

$ echo 'from unnecessary_math import multiply' > conftest.py
$ pytest test_unnecessary_math.txt
================== test session starts ===================
collected 1 item                                         

test_unnecessary_math.txt .                        [100%]

=================== 1 passed in 0.05s ====================

Go figure.

The lesson, I guess, is that you CAN run doctest tests with pytest, but you might have to jump through a couple hoops.

More info from pytest.org about doctest and pytest

Here’s the pytest.org page on doctest:

It also covers using --doctest-modules to get pytest to check doctests that are embedded in docstrings in your code under test.

Newer post

Don't use nose