python mock.patch()不模拟导入的类模块

发布于 2025-01-26 22:45:57 字数 1832 浏览 3 评论 0原文

我有一些看起来像这样的代码(删节):

## ./module_1/lambda_function.py

from shared.graphql_helpers import GraphQLClient

gql_client = GraphQLClient('http://host_url.test/')

post_event_to_consumer(event):
   response = gql_client.make_query(mutation, { 'data': event, {})

lambda_hanbdler(event, context):
   post_event_to_consumer(event['detail']

导入的模块是这样的:

## ./shared/graphql_helpers.py

import requests

class GraphQLClient:
    def __init__(self, url):
      self.host_url = url

    def make_query(self, query, variables, headers):
        request = requests.post(self.host_url, json={'query': query, 'variables': variables}, headers=headers)
        if request.status_code != 200:
            raise Exception()
        elif request.json()['errors']:
            raise Exception()
        else:
            return request.json()['data']

项目结构是这样的:

module_1
    __init__.py
    lambda_function.py
    lambda_function_test.py
shared
    __init__.py
    graphql_helpers.py

我的问题是我试图在lambda_function in graphqlclient lambda_function_test.py < /code>,但似乎不起作用。

我的单元测试看起来像这样:

## ./module_1/lambda_function_test.py

import os
from unittest import mock, TestCase

class TestLambdaFunction(TestCase):

    @mock.patch('module_1.lambda_function.GraphQLClient')
    def test_post_event_to_compplex(self, mock_gql_client):
        mock_gql_client = mock.Mock()
        mock_gql_client.make_query.return_value = {}

        result = post_event_to_consumer(json_data['detail'])

        assert result == None

根据我在线阅读的内容,patch模拟了SUT的命名空间中的导入,但是,当我运行测试时,它会导入原始的GraphQlClclient并抛出ConnectionError。我还尝试从路径上删除module_1,但这也不起作用。

不确定这里是什么不正确的,但是任何帮助将不胜感激。

I have a some code that looks like this (abridged):

## ./module_1/lambda_function.py

from shared.graphql_helpers import GraphQLClient

gql_client = GraphQLClient('http://host_url.test/')

post_event_to_consumer(event):
   response = gql_client.make_query(mutation, { 'data': event, {})

lambda_hanbdler(event, context):
   post_event_to_consumer(event['detail']

The module imported is such:

## ./shared/graphql_helpers.py

import requests

class GraphQLClient:
    def __init__(self, url):
      self.host_url = url

    def make_query(self, query, variables, headers):
        request = requests.post(self.host_url, json={'query': query, 'variables': variables}, headers=headers)
        if request.status_code != 200:
            raise Exception()
        elif request.json()['errors']:
            raise Exception()
        else:
            return request.json()['data']

The project structure is such:

module_1
    __init__.py
    lambda_function.py
    lambda_function_test.py
shared
    __init__.py
    graphql_helpers.py

My problem is that I am trying to patch GraphQLClient in lambda_function_test.py but it does not seem to be working.

My unit test looks like this:

## ./module_1/lambda_function_test.py

import os
from unittest import mock, TestCase

class TestLambdaFunction(TestCase):

    @mock.patch('module_1.lambda_function.GraphQLClient')
    def test_post_event_to_compplex(self, mock_gql_client):
        mock_gql_client = mock.Mock()
        mock_gql_client.make_query.return_value = {}

        result = post_event_to_consumer(json_data['detail'])

        assert result == None

From what I read online, patch mocks the import in the SUT's namespace, however, when I run the test, it imports the original GraphQLClient and throws a ConnectionError. I also tried removing module_1 from the path but that didn't work either.

Not sure what's incorrect here, but any help would be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文