Search code examples
pythondjangounit-testingtestingdocstring

How can I prevent django test from running examples in docstrings?


In my code I have:

def fn1():
    """
    fn1 description

    Parameters
    ----------
    components_list : list
        List of IDs of the reference groups

    Return
    ------
    ret : str
        A string representing the aggregate expression (See example)

    Example
    -------
    >>> reference_groups = [1,2,3,4]
    >>> expression = suite.getComponentsExpression(reference_groups)
        (tier1 and tier2) or (my_group)
    """ 
    #some code here

When I run: ./manage.py test, it tries to run the example. Is there a way to prevent this from happening? e.g. maybe CLI option to skip docstrings


Solution

  • Django 1.6 introduced a new test runner, which does not run doctests. So one answer to prevent tests is to upgrade Django!

    If that's not possible, the proper fix would be to subclass DjangoTestSuiteRunner, disable the doc test loading, and tell Django to use your new test runner with the [TEST_RUNNER] setting.

    Or if that's too tricky, a quick hack would be to change your docstrings so that they don't look like doctests, e.g.

    >.> reference_groups = [1,2,3,4]