一个帮助测试pop项目的pytest插件
项目描述
一个帮助测试pop项目的pytest插件
安装
使用pip安装
pip install pytest-pop
开发安装
克隆pytest-pop仓库并使用pip安装
git clone https://gitlab.com/saltstack/pop/pytest-pop.git pip install -e pytest-pop
获取Hub
在您的conftest.py中扩展hub fixture,使其包括您的子项目
@pytest.fixture(scope="function") def hub(hub): # TODO Add dynes that will be used for your tests for dyne in ("exec", "states"): hub.pop.sub.add(dyne_name=dyne) if dyne in ("corn", "exec", "states"): hub.pop.sub.load_subdirs(getattr(hub, dyne), recurse=True) args = [ # TODO patch in whatever cli args are necessary to run your test ] with mock.patch("sys.argv", ["pytest-pop"] + args): hub.pop.config.load(["pytest_pop"], "pytest_pop") yield hub # TODO Hub cleanup pass
标记
利用来自pytest-salt-factoroies的pytest标记
root
将测试标记为需要提升权限。在类Unix系统中,如果运行测试的用户不是root,则测试将被跳过。在Windows系统中,如果测试不是以管理员权限运行的,则测试将被跳过。
示例
@pytest.mark.skip_if_not_root def test_root(hub): pass
expensive
将测试标记为耗时。使用带有‘–run-expensive’标志的pytest运行,或将环境变量EXPENSIVE_TESTS设置为“True”以运行这些测试。默认情况下,它们将被跳过
示例
@pytest.mark.expensive_test def test_expensive(hub): pass
破坏性
将测试标记为破坏性。使用带有‘–run-destructive’标志的pytest运行,或将环境变量DESTRUCTIVE_TESTS设置为“True”以运行这些测试。默认情况下,它们将被跳过
示例
@pytest.mark.destructive_test def test_destructive(hub): pass
日志记录
您可以使用hub进行日志记录,而无需在每个使用hub的文件中设置记录器
示例
hub.log.debug("debug message")
确保以‘–cli-log-level=10’运行pytest,以便看到调试信息
模拟
使用以下方式获取hub的完整模拟/自动指定版本:
mock_hub = hub.pop.testing.mock_hub()
一个名为mock_hub的固定装置,其中包含对真实插件的一些常见替换,可作为固定装置使用
def test_thing(mock_hub): pass
在您自己的固定装置中扩展模拟hub
# Scope the mock_hub to a function so that the autospec gets reset after each use. @pytest.fixture(scope="function") def mock_hub(mock_hub, hub): # replace mocked functions with necessary real ones # extend this on a per-module or per-function basis if necessary mock_hub.sub.func = hub.sub.func yield mock_hub
现在可以对合约函数进行自动指定断言
import project.sub.plugin as plugin def test_cmd_run(mock_hub): plugin.func(mock_hub, "arg") mock_hub.sub.plugin.func.assert_called_with("arg")
ACCT
某些项目,特别是idem-cloud,需要来自idem的ctx生成器的凭据。存在ctx固定装置,但除非您覆盖了acct_file和acct_profile固定装置,否则它将不起作用
@pytest.fixture def acct_subs() -> List[str]: return ["azurerm", "vultr"] @pytest.fixture def acct_profile() -> str: return "test_development_idem_cloud"
一旦覆盖了这些固定装置,ctx固定装置将可用于您的测试
test_cloud_instance_present(hub, ctx): hub.state.cloud.present(ctx, "instance_name")
示例
模拟hub执行函数
with patch.object(mock_hub.exec, 'dummy', return_value="some result") as mock_exec: pass
为mock_hub函数设置返回值
mock_hub.sub.function.return_value = "Pass"
模拟hub断言被调用时
mock_hub.sub.function.assert_called_with("myinput", True)
项目详情
下载文件
下载适合您的平台文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分发
构建分发
pytest-pop-12.0.0.tar.gz的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | d0a1645795879ed7a46f66102e6f1fccf6332eaa6e4190463379b61622f97fda |
|
MD5 | cab3901e88c31db729d4aba27dfecb84 |
|
BLAKE2b-256 | 01c532e7e4d53271993098cc94fe59d4d204de31ab1f06d668cd531e59556715 |
pytest_pop-12.0.0-py3-none-any.whl的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | d2628a217d2fedf6b8a7e068be93cbc7f76d2378788d974d69450337425033e8 |
|
MD5 | f74b3c3e0730310d7f2f40dc78734f6f |
|
BLAKE2b-256 | 2aed800a0eac02c60860abdc767e69e6d7b3e6050c2aa38a4058b9f4fd1f5d80 |