2016-10-17 5 views
0

@ pytest.mark.parametrizeを使用して、さまざまなテストケースと期待される出力を得ています。いくつかのテストケースで完璧に正常に通過し、他のいくつかのケースでこのエラーが発生します。それはGoogleにもできません。私は何が間違っているのか知りたい。誰かが私にこのエラーのために最強のgoogleを教えることができれば嬉しいです!コードに関するpytestでエラーが発生する

============================= 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\[email protected]=', tha='\x00\x00\x00\x00\x00\x00', tpa='\n\[email protected]\x01'),"<socket._socketobject object at 0x0000000003DC8118>",0), 
(ARP(sha='jrofalfeoiexad', spa='\[email protected]=', 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 

説明:これは、私はエラーを取得したコードです。適切な関数が定義されています。最初のテストケースでうまく動作します。最後の2つのテストケースで失敗しました。

+0

エラーの原因となっているファイルの例を表示できますか? –

+0

これは私のテストコードhttp://pastebin.com/BiU4xmcUです。私は3つのテストケースを与えたことがわかります。最初のテストケースでうまく動作します。私は残りの2つのケースでエラーを上回っています。関連するすべての関数が定義されています。私は、私の関数がarpパケットを正しく解析できて、間違ったパケットを処理できるかどうかを調べるためにこれをやっています。それで、最後の2つのテストケースでパケットの代わりにガーベジ・バリューを与えようとしたのです。今私は少なくともpytestが間違ったパケットで失敗したが、このようなエラーを出さないことを伝えたい。 –

+0

基本的に私は自分でパケットを構築し、そのまま関数とテストの入力として与えたいと思います。怖いのを聞いた。しかし、scapyインストールでエラーが発生する。コマンドプロンプトがハングアップしています。 –

答えて

3

貼り付けた例では、このファイルにビットを最小限に抑えることができます。

私たちはPython 2と同じエラーを与える
import pytest 

@pytest.mark.parametrize("foo", ['\n\xcjfeiafa1']) 
def test_escapes(foo): 
    pass 

、と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エスケープが原因です。

+0

情報ありがとうございました。 –

+0

@KalyanamRajashreeよろしくお願いします!あなたはあなたの質問を解決したら[回答を受け入れてください](http://stackoverflow.com/help/someone-answers)できますか? –

関連する問題