如何模拟 BDD 步骤文件

How to Mock In a BDD Steps File

我想模拟 os.path.exists 方法的行为,以便在 os.path.exists 报告 file/folder 不存在时验证我的脚本是否正确运行。

@when("Service starts with input file that does not exist")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    json_file_path = "fake_file_path"
    mock_os_path = mock.Mock()
    mock_os_path.exists.return_value = False

    context.returncode = dicom_send_service.launch(json_file_path)

    mock_os_path.exists.assert_called_once_with(json_file_abspath)

如何将模拟注入我的脚本?我尝试使用

@mock.patch("mymodule.os.path")
@when("Service starts with input file that does not exist")
def step_impl(context, mock_os_path):

但是,当我 运行 方法 python returns:

Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/behave/model.py", line 1456, in run
    match.run(runner.context)
  File "/usr/local/lib/python2.7/dist-packages/behave/model.py", line 1903, in run
    self.func(context, *args, **kwargs)
TypeError: step_impl() takes exactly 2 arguments (1 given)

如您所见,step_impl 方法根据声明需要 2 个参数,但 BDD 仅使用 1 个参数(上下文值)调用它并且未选取模拟注释。

这是我正在测试的代码:

import os

def validate(json_file_path):
    """Method which validates the JSON file, an error message is returned if the file fails verification.

        json_file_path -- the path to the file with the message configuration details"""
    if not os.path.exists(json_file_path):
        return "Could not find file at path {0}".format(json_file_path)
    ...
    return ""

def launch(json_file_path):
    error_message = valid(json_file_path)
    if error_message:
        print(error_message)
        return 1

所以要回答我自己的问题,您必须使用 with mock.patch 语法:

with mock.patch('name of thing to mock') as name_of_mock:

所以我上面的例子会变成:

@when("Service starts with input file that does not exist")
def step_impl(context):
    """
    :type context: behave.runner.Context
    """
    json_file_path = "fake_file_path"

    # This is where the magic happens
    with mock.patch ('os.path') as mock_os_path:

        mock_os_path.exists.return_value = False

        context.returncode = dicom_send_service.launch(json_file_path)

        mock_os_path.exists.assert_called_once_with(json_file_abspath)

我已经对其进行了测试,它非常有效。比使用 Java 中的 Mockito 或 Powermock 等其他模拟框架要容易得多。