가져온 모듈에서 함수를 @patch
하는 방법을 알고 싶습니다.
여기까지가 제가 있는 곳입니다.
app/mocking.py:
app.my_module import get_user_name def test_method(): return get_user_name() if __name__ == "__main__": print "Starting Program... " test_method()
app/my_module/__init__.py:
def get_user_name(): "모의되지 않은 사용자" 반환
test/mock-test.py:
import unittest from app.mocking import test_method def mock_get_user(): return "이 어리석은 조롱" @patch("app.my_module.get_user_name") class MockingTestTestCase(unittest.TestCase): def test_mock_stubs(self, mock_method): mock_method.return_value = "이 어리석은 조롱") Equals = test_method() self.assert. (ret, "Mocked This Silly") if __name__ == "__main__": unittest.main()
내 예상대로 작동하지 않습니다. "patched" 모듈은 단순히 get_user_name
의 모의되지 않은 값을 반환합니다. 테스트 중인 네임스페이스로 가져오는 다른 패키지의 메서드를 어떻게 조롱합니까?