[英]Python unittest mocking not working as expected
I got this project structure:我得到了这个项目结构:
my_project
├── __init__.py
├── app.py
├── helpers
│ └── __init__.py
└── tests
├── __init__.py
├── integration
│ ├── __init__.py
│ └── test_app.py
└── unit
├── __init__.py
└── test_helpers.py
So far, unit testing for helpers
hasn't been complicated, i'd patch 3rd parties and some functions within helpers.到目前为止,
helpers
的单元测试并不复杂,我会修补 3rd parties 和 helpers 中的一些功能。
The integration testing in tests/integration/test_app.py
is being a bit of a blocking point because the patching isn't doing what i need it do to. tests/integration/test_app.py
中的集成测试是一个障碍点,因为补丁没有做我需要做的事情。 For example, I got a method like this:例如,我有一个这样的方法:
helpers/ init .py:助手/初始化.py:
def compute_completeness_metric(
vendor,
product,
fields,
endpoint):
body = {"vendor": vendor, "product": product, "fields": fields}
response_json = requests.post(
"http://example.com", json=body)
if response_json.status_code != 200:
response_json = "Can't compute"
else:
response_json = response_json.json()
return response_json
Now, in app.py, I call it the following way:现在,在 app.py 中,我这样称呼它:
from helpers import compute_completeness_metric
def main(req: func.HttpRequest) -> func.HttpResponse:
final_results = {}
final_results = compute_completeness_metric(vendor, product, fields, endpoint)
...
And when testing, I try to patch it the following way:在测试时,我尝试通过以下方式对其进行修补:
@patch('data_quality.helpers.compute_completeness_metric')
def test_app(self, mock_compute_completeness_metric):
mock_compute_completeness_metric.return_value = "blablabla"
But the mocked methods do not return what they're supposed to return, instead execute themselves as if they weren't mocked.但是被模拟的方法不会返回它们应该返回的内容,而是像没有被模拟一样执行它们自己。
Am I missing something?我错过了什么吗? Should I be mocking the methods within
get_rule_data_models()
?我应该是 mocking
get_rule_data_models()
中的方法吗?
TIA! TIA!
The way you mock a function depends on the way it is declared or imported.您模拟 function 的方式取决于它的声明或导入方式。
If you used如果你用过
import helpers
...
final_results = helpers.compute_completeness_metric(vendor, product, fields, endpoint)
Then mocking with @patch('data_quality.helpers.compute_completeness_metric')
would be fine然后 mocking 和
@patch('data_quality.helpers.compute_completeness_metric')
就可以了
But here you use:但是在这里你使用:
from helpers import compute_completeness_metric
...
final_results = compute_completeness_metric(vendor, product, fields, endpoint)
That means that in app.py you no longer use the symbol from the helpers
module but a local symbol that points to where helpers.compute_completeness_metric
pointed at import time.这意味着在 app.py 中,您不再使用
helpers
模块中的符号,而是指向helpers.compute_completeness_metric
在导入时指向的位置的本地符号。 That means that you have to patch the symbol in the app
module:这意味着您必须修补
app
模块中的符号:
@patch('data_quality.app.compute_completeness_metric')
def test_app(self, mock_compute_completeness_metric):
mock_compute_completeness_metric.return_value = "blablabla"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.