帮助测试pop项目的pytest插件
pytest-pop的Python项目详细描述
pytest插件,帮助测试pop项目
安装
使用pip安装:
pip install pytest-pop
开发安装
克隆pytest poprepo并使用pip安装:
^{pr2}$获取集线器
在您的conftest.py所以它包括你的潜艇:
@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标记
根
将测试标记为需要提升的权限。 在类UNIX系统上,如果运行测试的用户不是root用户,则将跳过测试。 在Windows系统上,如果测试不是以管理员权限运行的,则将跳过测试。在
示例:
@pytest.mark.skip_if_not_root def test_root(hub): pass
昂贵
将测试标记为昂贵。 使用“-Run priced”标志运行pytest,或将precident\u TESTS环境变量设置为“True”以运行这些测试。 默认情况下,它们将被跳过
示例:
@pytest.mark.expensive_test def test_expensive(hub): pass
破坏性eh3>
将测试标记为具有破坏性。 使用“-Run destinative”标志运行pytest,或将destroative\u TESTS环境变量设置为“True”以运行这些测试。 默认情况下,它们将被跳过
示例:
@pytest.mark.destructive_test def test_destructive(hub): pass
测井
您可以使用集线器进行日志记录,而无需在使用集线器的每个文件中设置记录器
示例:
hub.log.debug("debug message")
请确保使用“-cli log level=10”运行pytest以查看调试消息
嘲笑
通过以下方式访问完全模拟/自动指定的集线器版本:
mock_hub = hub.pop.testing.mock_hub()
一个模拟hub fixture可以作为一个fixture提供,该fixture可以替换真实的插件:
def test_thing(mock_hub): pass
在您自己的夹具中扩展模拟轮毂:
# 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
现在可以对收缩函数执行autospec断言:
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")
账户
一些项目,特别是idem cloud需要来自idem的ctx生成器的证书。 ctx固定装置存在,但除非您重写acct_文件和acct_profilefixture,否则它将无法工作:
@pytest.fixture def acct_subs() -> List[str]: return ["azurerm", "vultr"] @pytest.fixture def acct_profile() -> str: return "test_development_idem_cloud"
一旦重写这些fixture,您的测试将可以使用ctxfixture:
test_cloud_instance_present(hub, ctx): hub.state.cloud.present(ctx, "instance_name")
示例
模拟集线器执行函数:
with patch.object(mock_hub.exec, 'dummy', return_value="some result") as mock_exec: pass
为mock_hub函数设置返回值:
mock_hub.sub.function.return_value = "Pass"
模拟中心断言调用方式:
mock_hub.sub.function.assert_called_with("myinput", True)
- 项目
标签: