2017-04-12 21 views
2

私はなぜ灯具で模擬戻り値を使用できないように見えますか?pytest fixtureのpytest-mock mocker

def test_mockers(mocker): 
    mock_uuid = mocker.patch.object(uuid, 'uuid4', autospec=True) 
    mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f') 
    # this would return a different value if this wasn't the case 
    assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f' 

に上記テストパスを次の輸入

import pytest 
import uuid 

動作pytest-モック例で 。

FAILED 
phidgetrest\tests\test_taskscheduler_scheduler.py:62 (test_mockers) 
mocked_uuid = <function uuid4 at 0x0000029738C5B2F0> 

    def test_mockers(mocked_uuid): 
     # this would return a different value if this wasn't the case 
>  assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f' 
E  AssertionError: assert <MagicMock name='uuid4().hex' id='2848515660208'> == '5ecd5827b6ef4067b5ac3ceac07dde9f' 
E  + where <MagicMock name='uuid4().hex' id='2848515660208'> = <MagicMock name='uuid4()' id='2848515746896'>.hex 
E  + where <MagicMock name='uuid4()' id='2848515746896'> = <function uuid4 at 0x0000029738C5B2F0>() 
E  +  where <function uuid4 at 0x0000029738C5B2F0> = uuid.uuid4 

tests\test_taskscheduler_scheduler.py:65: AssertionError 

は期待して誰かが私は理由を理解することができます:上記以下の出力で失敗

@pytest.fixture 
def mocked_uuid(mocker): 
    mock_uuid = mocker.patch.object(uuid, 'uuid4', autospec=True) 
    mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f') 
    return mock_uuid 

def test_mockers(mocked_uuid): 
    # this would return a different value if this wasn't the case 
    assert uuid.uuid4().hex == '5ecd5827b6ef4067b5ac3ceac07dde9f' 

: は、しかし、私は多くのテストケースでこれを使用するように私は、私はちょうどフィクスチャを使用すると考えていました1つは機能し、もう1つは機能しない解決策を提供しません。

フィクスチャ[セッション、モジュール、関数]のスコープを変更しようとしましたが、なぜ失敗したのか分かりません。

+0

あなたの例は、Python 2と3の両方でうまく動作します。 –

+0

セットアップの詳細と実行方法?多分私はそれが私の環境について働いていないことを追跡することができます。私はPython 3.6を使用しています(probはそれを言及している必要があります) 私はそれがうまくいくはずだと思って、github検索でpytest-mockとpatch.objectの類似した例を示しました。 – ehindy

+0

上記のファイルを使って、 'import pytest、uuid'を追加しました。 Python 3.6とpytest 3.0.7で実行しました。あなたのスタックトレースを見ると、上に示したスニペットを実際には実行していません。 –

答えて

3

だから、犯人が見つかりました。それは本当にばかげていました。私は元のコードに問題がありますので、上記の例を実際に再入力してコピー&ペーストしました。私の治具では、私が入力した:

それはされている必要があります
mock_uuid.return_value(uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f')) 

:私はので、それが失われた非常に多くの時間が...他人のために働いていた、私の例では持っていた

mock_uuid.return_value = uuid.UUID(hex='5ecd5827b6ef4067b5ac3ceac07dde9f') 

...しかし、私はこれが将来の誰かを助けるかもしれないことを願っています。

関連する問題