Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make the pytest doctest module ignore a file?

We use pytest to test our project and have enabled --doctest-modules by default to collect all of our doctests from across the project.

However there is one wsgi.py which may not be imported during test collection, but I cant get pytest to ignore it.

I tried putting it in the collect_ignore list in conftest.py but apparently the doctest module does not use this list.

The only thing that does work is putting the whole directory of wsgi.py into norecursedirs in the pytest config file, but this obviously hides the whole directory, which I don't want.

Is there a way to make the doctest module ignore just a certain file?

like image 570
NiklasMM Avatar asked Dec 28 '16 09:12

NiklasMM


People also ask

How do I ignore a file in pytest?

Ignore paths during test collectionThe --ignore-glob option allows to ignore test file paths based on Unix shell-style wildcards. If you want to exclude test-modules that end with _01.py , execute pytest with --ignore-glob='*_01.py' .

Can pytest run Doctests?

pytest also introduces new options: ALLOW_UNICODE : when enabled, the u prefix is stripped from unicode strings in expected doctest output. This allows doctests to run in Python 2 and Python 3 unchanged.

Can doctest be used to test Docstrings True False?

The Doctest Module finds patterns in the docstring that looks like interactive shell commands. The input and expected output are included in the docstring, then the doctest module uses this docstring for testing the processed output.


2 Answers

You can use hook to conditionally exclude some folders from test discovery. https://docs.pytest.org/en/latest/writing_plugins.html

def pytest_ignore_collect(path, config):
    """ return True to prevent considering this path for collection.
    This hook is consulted for all files and directories prior to calling
    more specific hooks.
    """
like image 129
ANDgineer Avatar answered Nov 03 '22 13:11

ANDgineer


As MasterAndrey has mentioned, pytest_ignore_collect should do the trick. Important to note that you should put conftest.py to root folder (the one you run tests from).
Example:

import sys

def pytest_ignore_collect(path):
    if sys.version_info[0] > 2:
        if str(path).endswith("__py2.py"):
            return True
    else:
        if str(path).endswith("__py3.py"):
            return True

Since pytest v4.3.0 there is also --ignore-glob flag which allows to ignore by pattern. Example: pytest --doctest-modules --ignore-glob="*__py3.py" dir/

like image 27
Grygorii Iermolenko Avatar answered Nov 03 '22 14:11

Grygorii Iermolenko