小编D H*_*aze的帖子

由于AttributeError,PyTest-Mock无法正常工作

我试图使用PyTest_Mock以便在我的Python项目中进行一些测试。我创建了一个非常简单的测试来进行测试,但是却出现了AttributeError,我不知道为什么。

模型

def square(x):
    return x * x

if __name__ == '__main__':
    res = square(5)
    print("result: {}".format(res))
Run Code Online (Sandbox Code Playgroud)

test_model.py

import pytest
from pytest_mock import mocker

import model

def test_model():
    mocker.patch(square(5))

    assert model.square(5) == 25
Run Code Online (Sandbox Code Playgroud)

运行后,python -m pytest出现故障和以下错误:

    def test_model():
>       mocker.patch(square(5))
E       AttributeError: 'function' object has no attribute 'patch'

test_model.py:7: AttributeError
Run Code Online (Sandbox Code Playgroud)

python pytest

3
推荐指数
1
解决办法
1251
查看次数

标签 统计

pytest ×1

python ×1