【问题标题】:Colors sequences ignored during python test在 python 测试期间忽略颜色序列
【发布时间】:2019-02-26 11:31:33
【问题描述】:

相关: timofurrer/colorful#23


我正在重新实现pure prompt in python,以便它可以支持更多的外壳。

但是,在测试颜色时,我得到了“意外”的行为,我不明白的行为。

测试输出

    def test_prompt_symbol_is_colored_for_successful_command():
        assert str(prompt.prompt_symbol()) == str(colors.primary('❯'))
>       assert str(prompt.prompt_symbol()) == '\x1b[38;2;155;48;255m❯\x1b[39m'
E       AssertionError: assert '❯' == '\x1b[38;2;155;48;255m❯\x1b[39m'
E         - ❯
E         + ❯   ← this one is purple

_colors_test.py_

def test_prompt_symbol_is_colored_for_successful_command():
    assert str(prompt.prompt_symbol()) == str(colors.primary('❯'))
    assert str(prompt.prompt_symbol()) == '\x1b[38;2;155;48;255m❯\x1b[39m'

我运行测试:

pytest -v tests/

colors.py

import colorful

primary = colorful.purple1
mute = colorful.gray

prompt.py

from pure import colors

def prompt_symbol(last_command_status=0):
    symbol = colors.primary('❯') if last_command_status == 0 else colors.danger('❯')
    return symbol

问题

第一个断言成功,而第二个断言失败,尽管它们应该是等效的。当我否定第一个断言时,转义序列不存在:

>       assert str(prompt.prompt_symbol()) != str(colors.primary('❯'))
E       AssertionError: assert '❯' != '❯'
E        +  where '❯' = str(<colorful.core.ColorfulString object at 0x7f545276f080>)
E        +    where <colorful.core.ColorfulString object at 0x7f545276f080> = <function prompt_symbol at 0x7f54527b79d8>()
E        +      where <function prompt_symbol at 0x7f54527b79d8> = prompt.prompt_symbol
E        +  and   '❯' = str(<colorful.core.ColorfulString object at 0x7f545276f0b8>)
E        +    where <colorful.core.ColorfulString object at 0x7f545276f0b8> = <colorful.core.Colorful.ColorfulStyle object at 0x7f54527c2278>('❯')
E        +      where <colorful.core.Colorful.ColorfulStyle object at 0x7f54527c2278> = colors.primary

在 python REPL 中手动执行命令给了我:

>>> from pure import colors, prompt 
>>> str(colors.primary('❯'))
'\x1b[38;2;155;48;255m❯\x1b[39m'
>>> str(prompt.prompt_symbol())
'\x1b[38;2;155;48;255m❯\x1b[39m'

【问题讨论】:

    标签: python colors terminal pytest


    【解决方案1】:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-17
      • 1970-01-01
      • 2020-06-23
      • 2018-06-05
      • 2015-09-22
      • 2017-10-16
      • 1970-01-01
      • 2020-03-30
      相关资源
      最近更新 更多