从第 3 方模块导入任何对象,同时按需模拟其命名空间。
项目描述
PYTEST 对象获取器
提供get_object固定装置的 Pytest 插件。
强调
pytest_object_getter python 包,托管在pypi.org
可通过pip安装
可用于您的测试的get_object固定装置
从模块中动态导入对象
可选择模拟模块命名空间中存在的任何对象
在运行时构造模拟对象
在运行时改变对象的行为
针对多个平台和python版本进行了测试
平台:Ubuntu、MacOS
蟒蛇:3.6、3.7、3.8、3.9、3.10
您可以在pytest 最新文档中阅读更多关于 pytest 和固定装置的信息。
快速开始
先决条件
您需要安装Python。
安装
使用pip是安装pytest_object_getter的认可方式。
python3 -m pip install pytest_object_getter
安装后,您的测试中应该可以使用get_object pytest 固定装置。
用例
让我们看看如何编写测试并使用“get_object”夹具来模拟requests.get方法以避免实际的网络通信:
python3 -m pip install ask-pypi
import pytest
@pytest.fixture
def mock_response():
def init(self, package_name: str):
self.status_code = 200 if package_name == 'existing-package' else 404
return type('MockResponse', (), {
'__init__': init
})
@pytest.fixture
def create_mock_requests(mock_response):
def _create_mock_requests():
def mock_get(*args, **kwargs):
package_name = args[0].split('/')[-1]
return mock_response(package_name)
return type('MockRequests', (), {
'get': mock_get,
})
return _create_mock_requests
def test_fixture(get_object, create_mock_requests):
from ask_pypi import is_pypi_project
assert is_pypi_project('numpy') == True
assert is_pypi_project('pandas') == True
assert is_pypi_project('existing-package') == False
get_object('is_project', 'ask_pypi.pypi_project',
overrides={'requests': lambda: create_mock_requests()})
assert is_pypi_project('existing-package') == True
assert is_pypi_project('numpy') == False
assert is_pypi_project('pandas') == False
assert is_pypi_project('so-magic') == False
执照
免费软件:
发展
以下是与在此项目上进行开发相关的一些有用说明。
测试套件,使用pytest,位于测试目录
在多个 cpu 上并行执行单元测试
文档页面,托管在readthedocs服务器上,位于docs目录中
自动化,使用tox,由单个tox.ini文件驱动
CI Pipeline,运行在Github Actions上,定义在.github/
Job Matrix ,跨越不同平台和python 版本
平台:ubuntu-latest,macos-latest
Python解释器:3.6、3.7、3.8、3.9、3.10 _ _ _ _ _
并行作业执行,从矩阵生成,运行测试套件
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。
源分布
pytest_object_getter-1.0.2.tar.gz
(28.0 kB
查看哈希)
内置分布
关
pytest_object_getter -1.0.2.tar.gz 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 482c6e407d3e67a0de977437434f923a5fcd02503663c19bfccda9985b9d0649 |
|
| MD5 | 2f51f041e38528924b6e506fc56d775c |
|
| 布莱克2-256 | 02fed80930717c5e71028c9fd245cbf647da2062994d49d627c0b3147309abf6 |
关
pytest_object_getter -1.0.2-py3-none-any.whl 的哈希值
| 算法 | 哈希摘要 | |
|---|---|---|
| SHA256 | 1ca9f06f6827ceeb1621e49e9095401130ae051b45beb516882ca1b4af598dd7 |
|
| MD5 | 9f80f387ff32a1c661bbf53c02d2112b |
|
| 布莱克2-256 | d67346e99f3099f325310261482f9342c9cd719b08c78e23e83615cf464fa20f |