pytest 错误
Errors with pytest
我将@pytest.mark.parametrize 与各种测试用例和预期输出一起使用。它在很少的测试用例中通过得很好,在其他一些情况下给出了这个错误。甚至无法 google 它。我想知道可能出了什么问题。如果有人能告诉我如何至少 google 这个错误,我会很高兴!
============================= test session starts =============================
platform win32 -- Python 2.7.12, pytest-3.0.3, py-1.4.31, pluggy-0.4.0 -- c:\python27\python.exe
cachedir: .cache
rootdir: C:\Python27, inifile:
collected 0 items / 1 errors
=================================== ERRORS ====================================
___________________ ERROR collecting test_mod_pppoe_disc.py ___________________
lib\site-packages\py_path\local.py:650: in pyimport
import(modname)
lib\site-packages\pytest-3.0.3-py2.7.egg_pytest\assertion\rewrite.py:131: in find_module
source_stat, co = _rewrite_test(self.config, fn_pypath)
lib\site-packages\pytest-3.0.3-py2.7.egg_pytest\assertion\rewrite.py:322: in _rewrite_test
tree = ast.parse(source)
lib\ast.py:37: in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
E ValueError: invalid \x escape
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!
=========================== 1 error in 0.21 seconds ===========================
@pytest.mark.parametrize("test_input1,test_input2,expected", [
(ARP(sha='D\x85\x00\xa2}\xad', spa='\n\xc4@=', tha='\x00\x00\x00\x00\x00\x00', tpa='\n\xc4@\x01'),"<socket._socketobject object at 0x0000000003DC8118>",0),
(ARP(sha='jrofalfeoiexad', spa='\nenkajf@=', tha='\x00\x00\x00\x02jfcalkfel', tpa='\n\xcjfeiafa1'),"<socket._socketobject object at 0x0000000003D2BD48>",0),
(ARP(eioakn iejfoeajoijea),"<socket._socketobject object at 0x0000000003DC8118>",0)
])
def test_mod_arp(test_input1,test_input2,expected):
assert mod_arp(test_input1,test_input2) == expected
代码说明:这是我出错的代码。我定义了适当的功能。第一个测试用例工作正常。最后两个测试用例失败。
您粘贴的示例可以稍微缩小到此文件中:
import pytest
@pytest.mark.parametrize("foo", ['\n\xcjfeiafa1'])
def test_escapes(foo):
pass
这给了我们与 Python 2 相同的错误,而与 Python 3 的错误更清楚:
/usr/lib/python3.5/site-packages/_pytest/python.py:410: in _importtestmodule
mod = self.fspath.pyimport(ensuresyspath=importmode)
/usr/lib/python3.5/site-packages/py/_path/local.py:650: in pyimport
__import__(modname)
E File "/home/florian/tmp/foo.py", line 3
E @pytest.mark.parametrize("foo", ['\n\xcjfeiafa1'])
E ^
E SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \xXX escape
发生这种情况的原因是您的 \xcj
转义,这不是有效的转义。
我将@pytest.mark.parametrize 与各种测试用例和预期输出一起使用。它在很少的测试用例中通过得很好,在其他一些情况下给出了这个错误。甚至无法 google 它。我想知道可能出了什么问题。如果有人能告诉我如何至少 google 这个错误,我会很高兴!
============================= test session starts ============================= platform win32 -- Python 2.7.12, pytest-3.0.3, py-1.4.31, pluggy-0.4.0 -- c:\python27\python.exe
cachedir: .cache
rootdir: C:\Python27, inifile:
collected 0 items / 1 errors
=================================== ERRORS ====================================
___________________ ERROR collecting test_mod_pppoe_disc.py ___________________
lib\site-packages\py_path\local.py:650: in pyimport
import(modname)
lib\site-packages\pytest-3.0.3-py2.7.egg_pytest\assertion\rewrite.py:131: in find_module
source_stat, co = _rewrite_test(self.config, fn_pypath)
lib\site-packages\pytest-3.0.3-py2.7.egg_pytest\assertion\rewrite.py:322: in _rewrite_test
tree = ast.parse(source)
lib\ast.py:37: in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
E ValueError: invalid \x escape
!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!! =========================== 1 error in 0.21 seconds ===========================
@pytest.mark.parametrize("test_input1,test_input2,expected", [
(ARP(sha='D\x85\x00\xa2}\xad', spa='\n\xc4@=', tha='\x00\x00\x00\x00\x00\x00', tpa='\n\xc4@\x01'),"<socket._socketobject object at 0x0000000003DC8118>",0),
(ARP(sha='jrofalfeoiexad', spa='\nenkajf@=', tha='\x00\x00\x00\x02jfcalkfel', tpa='\n\xcjfeiafa1'),"<socket._socketobject object at 0x0000000003D2BD48>",0),
(ARP(eioakn iejfoeajoijea),"<socket._socketobject object at 0x0000000003DC8118>",0)
])
def test_mod_arp(test_input1,test_input2,expected):
assert mod_arp(test_input1,test_input2) == expected
代码说明:这是我出错的代码。我定义了适当的功能。第一个测试用例工作正常。最后两个测试用例失败。
您粘贴的示例可以稍微缩小到此文件中:
import pytest
@pytest.mark.parametrize("foo", ['\n\xcjfeiafa1'])
def test_escapes(foo):
pass
这给了我们与 Python 2 相同的错误,而与 Python 3 的错误更清楚:
/usr/lib/python3.5/site-packages/_pytest/python.py:410: in _importtestmodule
mod = self.fspath.pyimport(ensuresyspath=importmode)
/usr/lib/python3.5/site-packages/py/_path/local.py:650: in pyimport
__import__(modname)
E File "/home/florian/tmp/foo.py", line 3
E @pytest.mark.parametrize("foo", ['\n\xcjfeiafa1'])
E ^
E SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \xXX escape
发生这种情况的原因是您的 \xcj
转义,这不是有效的转义。