Pytest Monkeypatch 적용하지 않는 수입 기능

0

질문

나는 모듈 설정 거칠게 다음과 같다:

# foo.py
def generate_things_based_on_other_things():
  # some nasty things here

# bar.py
from foo import generate_things_based_on_other_things as generate

def coo():
  generate()

# conftest.py
import pytest

@pytest.fixture(autouse=True)
def patch_generate(monkeypatch):
  def mock_generate():
    print("hello!")

  monkeypatch.setattr("app.bar.generate", mock_generate)

# test_bar.py
from bar import coo

def test_coo():
  coo()

당으로 이 대답을 확인 했 monkeypatch 실제 가져오스의 기능입니다. 다른 어떤 경로를 던졌 "does not exist on module"오류가 있습니다. 그러나 때 나는 실행하는 테스트를 내가 오류가 있기 때문에,본래 기능 generate 가 호출되는 것에도 불구하고 monkeypatched. 를 알아낼 수 없습니다 왜 이 패치지 않을 것 지팡이의 방법으로 기대됩니다. 제가 기대하는 이 시험을 인쇄"안녕하세요!".

monkeypatching pytest python
2021-11-23 15:16:56
1

최고의 응답

0

당신의 경로를하지 않을 것 같다 일치합니다. 당신 from bar import coo지만,사용 setattrapp.bar. 확실히,당신이 사용할 수 있는 다른 형태의 setattr 대신에,이는 객체와 특성을 개별적으로 이름,예를 들어:

import bar  # or "from app import bar", whichever is correct for you

@pytest.fixture(autouse=True)
def patch_generate(monkeypatch):
    def mock_generate():
        print("hello!")

    monkeypatch.setattr(bar, "generate", mock_generate)

이 방법은 합리적으로 될 수 있습니다 있는지 확인 패치를 적용하고 있습니다.

2021-11-26 20:08:33

다른 언어로

이 페이지는 다른 언어로되어 있습니다

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................