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つは機能しない解決策を提供しません。
フィクスチャ[セッション、モジュール、関数]のスコープを変更しようとしましたが、なぜ失敗したのか分かりません。
あなたの例は、Python 2と3の両方でうまく動作します。 –
セットアップの詳細と実行方法?多分私はそれが私の環境について働いていないことを追跡することができます。私はPython 3.6を使用しています(probはそれを言及している必要があります) 私はそれがうまくいくはずだと思って、github検索でpytest-mockとpatch.objectの類似した例を示しました。 – ehindy
上記のファイルを使って、 'import pytest、uuid'を追加しました。 Python 3.6とpytest 3.0.7で実行しました。あなたのスタックトレースを見ると、上に示したスニペットを実際には実行していません。 –