[Python] Python doctests for a colored text output

Discussão em 'Python' iniciado por Stack, Maio 30, 2025.

  1. Stack

    Stack Membro Participativo

    Is it possible to write a docstring test for a function that prints out the colored text into the command line? I want to test only the content ignoring the color or to add somehow the information on color into the docstring. In the example below the test has failed, but it should not.

    Example

    from colorama import Fore


    def print_result():
    """Prints out the result.

    >>> print_result()
    Hello world
    """

    print('Hello world')

    def print_result_in_color():
    """Prints out the result.

    >>> print_result_in_color()
    Hello world
    """

    print(Fore.GREEN + 'Hello world' + Fore.RESET)


    if __name__ == '__main__':
    import doctest
    doctest.testmod()


    Output

    Failed example:
    print_result_in_color()
    Expected:
    Hello world
    Got:
    Hello world

    Continue reading...

Compartilhe esta Página