提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
pytest-mock
pytest-mock 是一个非常有用的 pytest 插件,它允许你在测试中轻松地创建 mock 对象。通过这个插件,你可以模拟(mock)出函数、类、方法等的行为,这对于接口自动化测试特别有用,因为你可以模拟外部依赖或难以测试的部分。
使用场景
假设你正在编写一个接口自动化测试,该接口依赖于一个外部服务来获取数据。为了确保你的测试能够独立运行并且不受外部服务的影响,你可以使用 pytest-mock 来模拟这个外部服务的行为。
示例代码
假设有一个简单的 Python 应用程序,它通过调用 get_data_from_service() 函数从外部服务获取数据,并使用这些数据进行一些业务逻辑处理。我们想要测试这部分逻辑,但是不想真正调用外部服务。
# app.py
import requests
def get_data_from_service():
response = requests.get('https://example.com/api/data')
return response.json()
def process_data(data):
# 假设我们想确保 data['value'] 大于 10
if data['value'] > 10:
return True
return False
测试代码
接下来,我们将编写一个测试文件 test_app.py,在这个文件中使用 pytest-mock 来模拟 get_data_from_service 函数的行为。
# test_app.py
import pytest
from app import process_data
def test_process_data(mocker):
# 模拟 get_data_from_service 函数的返回值
mocker.patch('app.get_data_from_service', return_value={'value': 20})
# 测试 process_data 函数
assert process_data(get_data_from_service()) is True
注意事项
命名空间:
当你使用 mocker.patch 时,确保提供的路径正确。如果 get_data_from_service 在不同的模块中定义,则需要提供正确的完整路径。
作用域:
默认情况下,mocks 在整个测试函数的作用域内有效。如果你需要更细粒度的控制,可以考虑使用 mocker.patch.object 或者指定 mocker.patch 的作用域。
清理:
pytest-mock 自动清理所有的 mocks,因此你不必担心清理工作。
副作用:
如果你的函数有副作用(例如修改全局状态),确保这些副作用不会影响其他测试。
Mock 的属性:
mocker.Mock 和 mocker.NonCallableMock 可以用来创建具有特定属性和方法的 mock 对象。
输出示例
当你运行测试时,如果没有问题,输出会显示测试成功。以下是使用 pytest 命令运行上述测试的示例输出:
$ pytest test_app.py
============================= test session starts ==============================
platform linux -- Python 3.10.6, pytest-7.2.0, pluggy-1.0.0
rootdir: /path/to/project
collected 1 item
test_app.py . [100%]
============================== 1 passed in 0.01s ===============================
这里,“.”表示测试成功。如果有失败或者错误,你会看到更详细的输出。
INSPIRATION
图片
高级示例
- 模拟类和对象
场景: 当你需要模拟一个类的行为时,可以使用 mocker.Mock 或 mocker.NonCallableMock。
import pytest
from app import MyClass
class TestMyClass:
def test_method(self, mocker):
# 创建一个模拟的类实例
mock_instance = mocker.Mock()
mock_instance.method.return_value = “mocked_value”
# 替换 MyClass 的实例为模拟实例
mocker.patch(‘app.MyClass’, return_value=mock_instance)
# 测试 MyClass 的方法
my_instance = MyClass()
result = my_instance.method()
assert result == “mocked_value”
- 模拟模块和包
场景: 当你需要模拟一个模块或包中的多个函数时,可以使用 mocker.patch.dict。
import pytest
from app import module
class TestModule:
def test_module_functions(self, mocker):
# 创建一个模拟的字典
mocked_module = {
'func1': mocker.Mock(return_value="mocked_func1"),
'func2': mocker.Mock(return_value="mocked_func2")
}
# 替换整个模块
mocker.patch.dict('app.module.__dict__', mocked_module)
# 测试模块中的函数
assert module.func1() == "mocked_func1"
assert module.func2() == "mocked_func2"
- 模拟外部库或第三方服务
场景: 当你需要模拟外部库或第三方服务的行为时,可以使用 mocker.patch。
import pytest
import requests
from app import get_data_from_service
class TestGetData:
def test_get_data(self, mocker):
# 模拟 requests.get
mocker.patch('requests.get', return_value=mocker.Mock(json=lambda: {"value": 20}))
# 测试 get_data_from_service
data = get_data_from_service()
assert data["value"] == 20
- 模拟异步函数
场景: 当你需要模拟异步函数时,可以使用 mocker.AsyncMock。
import pytest
import asyncio
from app import async_get_data_from_service
class TestAsyncGetData:
async def test_async_get_data(self, mocker):
# 模拟异步函数
mock_response = mocker.AsyncMock()
mock_response.json.return_value = {"value": 20}
# 替换 async_get_data_from_service 中的请求
mocker.patch('app.async_get_data_from_service', new=mock_response)
# 测试异步函数
data = await async_get_data_from_service()
assert data["value"] == 20
- 模拟异常抛出
场景: 当你需要模拟函数抛出异常时,可以使用 side_effect。
import pytest
from app import get_data_from_service
class TestGetData:
def test_get_data_exception(self, mocker):
# 模拟 requests.get 抛出异常
mocker.patch('requests.get', side_effect=requests.exceptions.RequestException)
# 测试 get_data_from_service
with pytest.raises(requests.exceptions.RequestException):
get_data_from_service()
- 模拟多个返回值
场景: 当你需要模拟函数返回不同的值时,可以使用 side_effect 传递一个列表或生成器。
import pytest
from app import get_data_from_service
class TestGetData:
def test_get_data_multiple_values(self, mocker):
# 模拟 requests.get 返回不同的值
mocker.patch('requests.get', side_effect=[{"value": 20}, {"value": 30}])
# 测试 get_data_from_service
data1 = get_data_from_service()
data2 = get_data_from_service()
assert data1["value"] == 20
assert data2["value"] == 30
- 模拟属性
场景: 当你需要模拟对象的属性时,可以使用 mocker.PropertyMock。
import pytest
from app import MyClass
class TestMyClass:
def test_property(self, mocker):
# 创建模拟的属性
mock_property = mocker.PropertyMock(return_value="mocked_value")
# 替换 MyClass 的属性
mocker.patch('app.MyClass.property', new_callable=mock_property)
# 测试 MyClass 的属性
my_instance = MyClass()
assert my_instance.property == "mocked_value"
- 模拟外部服务的认证机制
场景: 当你需要模拟外部服务的认证机制时,可以使用 mocker.patch 来模拟认证函数。
import pytest
from app import authenticate_and_get_data
class TestAuthentication:
def test_authenticate_and_get_data(self, mocker):
# 模拟 authenticate 函数
mocker.patch('app.authenticate', return_value=True)
# 测试 authenticate_and_get_data
data = authenticate_and_get_data()
assert data["value"] == 20
注意事项
https://mp.weixin.qq.com/s/aSYIlgq2tMgGyMB5l9E7yg
作用域:确保你的模拟是在适当的测试作用域内进行的,避免影响其他测试。
清理:
pytest-mock 会在每次测试后自动清理模拟的对象,但如果手动创建了模拟对象,确保正确地使用 mocker.resetall() 或
mocker.restore()。副作用:
当模拟函数时,确保没有副作用会影响到其他测试或实际的应用逻辑。
文档和调试:
使用模拟时,确保你的代码中有足够的注释,以便其他人能够理解为什么需要模拟某部分逻辑。