【问题标题】:pytest unicode syntaxerror with '£' symbol带有'£'符号的pytest unicode语法错误
【发布时间】:2016-08-05 19:57:46
【问题描述】:

我有一个测试文件:test_pytest.py:

def test_pytest():
    s = '£'
    assert True

使用 pytest,我这样运行它:python -m pytest test_pytest.py

结果:

> python -m pytest test_pytest.py
============================= test session starts =============================
platform win32 -- Python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1
rootdir: c:\temp, inifile:
collected 0 items / 1 errors

=================================== ERRORS ====================================
_______________________ ERROR collecting test_pytest.py _______________________
c:\projects\aio-rpc\venv\lib\site-packages\_pytest\python.py:611: in _importtestmodule
    mod = self.fspath.pyimport(ensuresyspath=importmode)
c:\projects\aio-rpc\venv\lib\site-packages\py\_path\local.py:650: in pyimport
    __import__(modname)
E     File "c:\temp\test_pytest.py", line 3
E   SyntaxError: (unicode error) 'utf-8' codec can't decode byte 0xa3 in position 0: invalid start byte
=========================== 1 error in 0.07 seconds ===========================

什么给了?

请注意,评论 s='£' 允许测试通过

【问题讨论】:

  • 有时隐藏字符不会复制到 SO,您可以尝试将此处发布的代码复制粘贴回您的测试文件,看看是否仍然会产生错误?
  • 确实如此,我尝试用vim和记事本复制。我猜你最终无法重现它?
  • 不,抱歉,我特别困惑的是,将其注释掉会修复 SyntaxError

标签: python unit-testing testing pytest python-3.5


【解决方案1】:

我真傻,我的 Vim 使用 latin1 编码而不是 utf-8 保存文件。将文件的编码更改为 utf-8 解决了该问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-05-27
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    相关资源
    最近更新 更多