Search code examples
pythondoctest

How can I have a doctest test that the result includes some text, rather than an exact match?


I understand that I can write a doctest like:

def foo():
    """
    >>> foo()
    'abc'
    """
    return 'abc'

However, what if my function is fetching a large HTML file? I don't want to have to put the entire contents of the returned string into the doctest. Instead I just want to be able to assert that the returned HTML includes an expected bit, something like:

"<title>The Right Page</title>" in returnedValue

Is this possible in doctests, or do I need a real unit test to check this?


Solution

  • Yes, it's possible. You can simply check if the result of the comparison is True.

    """
    >>> "<title>The Right Page</title>" in foo()
    True
    """