【发布时间】:2018-08-06 18:44:50
【问题描述】:
我正在尝试使用 PyTest_Mock 在我的 Python 项目中进行一些测试。我创建了一个非常简单的测试来尝试它,但是我得到一个 AttributeError 并且我不知道为什么。
model.py
def square(x):
return x * x
if __name__ == '__main__':
res = square(5)
print("result: {}".format(res))
test_model.py
import pytest
from pytest_mock import mocker
import model
def test_model():
mocker.patch(square(5))
assert model.square(5) == 25
在运行python -m pytest 后,我遇到了一个失败并出现以下错误:
def test_model():
> mocker.patch(square(5))
E AttributeError: 'function' object has no attribute 'patch'
test_model.py:7: AttributeError
【问题讨论】:
-
你能告诉我们目录结构吗?树。
-
这两个文件和一个空的
__init__.py文件都在根目录中