5、pytest -- 猴子补丁
有时候,测试用例需要调用某些依赖于全局配置的功能,或者这些功能本身又调用了某些不容易测试的代码(例如:网络接入)。fixture monkeypatch
可以帮助你安全的设置/删除一个属性、字典项或者环境变量,甚至改变导入模块时的sys.path
路径。
monkeypatch
提供了以下方法:
monkeypatch.setattr(obj, name, value, raising=True)
monkeypatch.delattr(obj, name, raising=True)
monkeypatch.setitem(mapping, name, value)
monkeypatch.delitem(obj, name, raising=True)
monkeypatch.setenv(name, value, prepend=False)
monkeypatch.delenv(name, raising=True)
monkeypatch.syspath_prepend(path)
monkeypatch.chdir(path)
所有的修改将在测试用例或者fixture
执行完成后撤销。raising
参数表明:当设置/删除操作的目标不存在时,是否上报KeyError
和AttributeError
异常。
1. 修改函数功能或者类属性
使用monkeypatch.setattr()
可以将函数或者属性修改为你希望的行为,使用monkeypatch.delattr()
可以删除测试用例使用的函数或者属性;
参考以下三个例子:
在这个例子中,使用
monkeypatch.setattr()
修改Path.home
方法,在测试运行期间,它一直返回的是固定的Path("/abc")
,这样就移除了它在不同平台上的依赖;测试运行完成后,对Path.home
的修改会被撤销;# src/chapter-5/test_module.py from pathlib import Path def getssh():
return Path.home() / ".ssh" def test_getssh(monkeypatch):
def mockreturn():
return Path("/abc") # 替换 Path.home
# 需要在真正的调用之前执行
monkeypatch.setattr(Path, "home", mockreturn) # 将会使用 mockreturn 代替 Path.home
x = getssh()
assert x == Path("/abc/.ssh")
在这个例子中,使用
monkeypatch.setattr()
结合类,模拟函数的返回对象;假设我们有一个简单的功能,访问一个
url
返回网页内容:# src/chapter-5/app.py from urllib import request def get(url):
r = request.urlopen(url)
return r.read().decode('utf-8')
我们现在要去模拟
r
,它需要一个.read()
方法返回的是bytes
的数据类型;我们可以在测试模块中定义一个类来代替r
:# src/chapter-5/test_app.py from urllib import request from app import get # 自定义的类模拟 urlopen 的返回值
class MockResponse: # 永远返回一个固定的 bytes 类型的数据
@staticmethod
def read():
return b'luizyao.com' def test_get(monkeypatch):
def mock_urlopen(*args, **kwargs):
return MockResponse() # 使用 request.mock_urlopen 代替 request.urlopen
monkeypatch.setattr(request, 'urlopen', mock_urlopen) data = get('https://luizyao.com')
assert data == 'luizyao.com'
你可以继续为实际的场景构建更具有复杂度的
MockResponse
;例如,你可以包含一个总是返回True
的ok
属性,或者根据输入的字符串为read()
返回不同的值;我们也可以通过
fixture
跨用例共享:# src/chapter-5/test_app.py import pytest # monkeypatch 是 function 级别作用域的,所以 mock_response 也只能是 function 级别,
# 否则会报 ScopeMismatch
@pytest.fixture
def mock_response(monkeypatch):
def mock_urlopen(*args, **kwargs):
return MockResponse() # 使用 request.mock_urlopen 代替 request.urlopen
monkeypatch.setattr(request, 'urlopen', mock_urlopen) # 使用 mock_response 代替原先的 monkeypatch
def test_get_fixture1(mock_response):
data = get('https://luizyao.com')
assert data == 'luizyao.com' # 使用 mock_response 代替原先的 monkeypatch
def test_get_fixture2(mock_response):
data = get('https://bing.com')
assert data == 'luizyao.com'
注意:
- 测试用例使用的
fixture
由原先的mock_response
替换为monkeypatch
; - 因为
monkeypatch
是function
级别作用域的,所以mock_response
也只能是function
级别,否则会报ScopeMismatch: You tried to access the 'function' scoped fixture 'monkeypatch' with a 'module' scoped request object
错误; - 如果你想让
mock_response
应用于所有的测试用例,可以考虑将它移到conftest.py
里面,并标记autouse=True
;
- 测试用例使用的
在这个例子中,使用
monkeypatch.delattr()
删除urllib.request.urlopen()
方法;# src/chapter-5/test_app.py @pytest.fixture
def no_request(monkeypatch):
monkeypatch.delattr('urllib.request.urlopen') def test_delattr(no_request):
data = get('https://bing.com')
assert data == 'luizyao.com'
执行:
λ pipenv run pytest --tb=native --assert=plain --capture=no src/chapter-5/test_app.
py::test_delattr
=============================== test session starts ================================
platform win32 -- Python 3.7.3, pytest-5.1.3, py-1.8.0, pluggy-0.13.0
rootdir: D:\Personal Files\Projects\pytest-chinese-doc
collected 1 item src\chapter-5\test_app.py F ===================================== FAILURES =====================================
___________________________________ test_delattr ___________________________________
Traceback (most recent call last):
File "D:\Personal Files\Projects\pytest-chinese-doc\src\chapter-5\test_app.py", line 78, in test_delattr
data = get('https://bing.com')
File "D:\Personal Files\Projects\pytest-chinese-doc\src\chapter-5\app.py", line 26, in get
r = request.urlopen(url)
AttributeError: module 'urllib.request' has no attribute 'urlopen'
================================ 1 failed in 0.04s =================================
注意:
避免删除内置库中的方法,如果一定要这么做,最好加上
--tb=native --assert=plain --capture=no
;修改
pytest
使用到的库,可能会污染pytest
本身,建议使用MonkeyPatch.context()
,它返回一个MonkeyPatch
对象,结合with
限制这些修改只发生在包裹的代码中。def test_stdlib(monkeypatch):
with monkeypatch.context() as m:
m.setattr(functools, "partial", 3)
assert functools.partial == 3
2. 修改环境变量
使用monkeypatch
的setenv()
和delenv()
方法,可以在测试中安全的设置/删除环境变量;
# src/chapter-5/test_env.py
import os
import pytest
def get_os_user():
username = os.getenv('USER')
if username is None:
raise IOError('"USER" environment variable is not set.')
return username
def test_user(monkeypatch):
monkeypatch.setenv('USER', 'luizyao')
assert get_os_user() == 'luizyao'
def test_raise_exception(monkeypatch):
monkeypatch.delenv('USER', raising=False)
pytest.raises(IOError, get_os_user)
monkeypatch.delenv()
的raising
要设置为False
,否则可能会报KeyError
;
你也可以使用fixture
,实现跨用例共享:
import pytest
@pytest.fixture
def mock_env_user(monkeypatch):
monkeypatch.setenv("USER", "TestingUser")
@pytest.fixture
def mock_env_missing(monkeypatch):
monkeypatch.delenv("USER", raising=False)
# notice the tests reference the fixtures for mocks
def test_upper_to_lower(mock_env_user):
assert get_os_user_lower() == "testinguser"
def test_raise_exception(mock_env_missing):
with pytest.raises(OSError):
_ = get_os_user_lower()
3. 修改字典
使用monkeypatch.setitem()
方法可以在测试期间安全的修改字典中特定的值;
DEFAULT_CONFIG = {"user": "user1", "database": "db1"}
def create_connection_string(config=None):
config = config or DEFAULT_CONFIG
return f"User Id={config['user']}; Location={config['database']};"
我们可以修改数据库的用户或者使用其它的数据库:
import app
def test_connection(monkeypatch):
monkeypatch.setitem(app.DEFAULT_CONFIG, "user", "test_user")
monkeypatch.setitem(app.DEFAULT_CONFIG, "database", "test_db")
expected = "User Id=test_user; Location=test_db;"
result = app.create_connection_string()
assert result == expected
可以使用monkeypatch.delitem
删除指定的项:
import pytest
import app
def test_missing_user(monkeypatch):
monkeypatch.delitem(app.DEFAULT_CONFIG, "user", raising=False)
with pytest.raises(KeyError):
_ = app.create_connection_string()
最新文章
- 深入浅出UML类图(一)
- 【帖子】怎么彻底删除kafka的topic,然后重建?
- 【py】安装ipython-notebook
- Android Screen Monitor抓取真机屏幕
- 在KALI LINUX中安装JAVA JDK
- 用js 做大图轮播方法(一)
- [google面试CTCI] 1-5.替换字符串中特定字符
- Git详解之一:Git起步
- C#基础拾遗系列之二:C#7.0新增功能点
- Dynamics 365使用代码发送邮件给指定邮箱地址
- 随心测试_软测基础_002_<;测试工程师_核心技能体系>;
- Angular组件——投影
- H3C交换机引发的奇葩故障
- 每天一个linux命令(9):touch
- vue自制switch滑块
- metamask源码学习-contentscript.js
- cucumber java从入门到精通(2)用代码定义步骤
- CMD命令利用tasklist与taskkill关闭程序
- c:forEach标签
- ERP行业销售如何挖掘潜在客户?
热门文章
- Event Hub小白入门指南
- Ng项目安装到指定盘符
- Spark 学习笔记之 Streaming Window
- MongoDB 学习笔记之 匹配完整数组
- CRS-2674: Start of &#39;ora.cssd&#39; on &#39;rac2&#39; failed 引发的rac集群服务起不来问题
- Json模块(dumps、loads、dump、load)函数篇
- Python3入门机器学习经典算法与应用☝☝☝
- css 文字间距
- Ubuntu php安装xdebug
- 经验分享:程序员如何快速定位问题(BUG)