有没有一种方法可以使用&quot&quot_called_with&quot&quot&quot那只是检查特定的单词吗?

发布于 2025-01-21 14:56:01 字数 240 浏览 3 评论 0原文

我正在尝试使用assert_called_with来断言传递给模拟对象的参数。该功能只有一个论点,但是该论点很大。 {“ A”:{“ B”:{“ C”:{“ d”:{“ key”:“ keyword”,“任何”:“ aaa”,“ nothing”,“ nothing”:“ bbbb”}}}}}}} }

我只关心“键”:“关键字”不在乎其他东西,有没有办法这样做?

I am trying to use assert_called_with to assert the argument that is passed to the mocked object. There is only one argument of that function but the argument is very large.
{"A":{"B": {"C" : {"D": {"key": "keyword", "any": "aaa", "anything": "bbbb"}}}}

I only care about "key": "keyword" exists and do not care about other stuff, is there a way to do so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

娇纵 2025-01-28 14:56:01

假设您的函数具有一个参数,那就是嵌套词典,并且应在某种级别包含特定的键/值对,则必须使用某些函数手动迭代该词典。

您可以从call_args_list获得模拟函数的所有调用参数。每个条目都包含一个带有位置参数列表和关键字参数的元组,例如call_args_list [0] [0] [0] [0]包含第一个呼叫的第一个位置参数。

假设您的测试函数总是完全有一个参数,这被称为位置参数,您可以做类似的事情:

def dict_contains_key_value(arg, key, value):
    if not isinstance(arg, dict):
        return False
    for k, v in arg.items():
        if k == key and v == value:
            return True
        if dict_contains_key_value(v, key, value):
            return True
    return False


@mock.patch("my_module.sut")
def test_called_arg(mocked):
    caller()  # the function that calls `sut()` several times
    mocked.assert_called()
    assert any(dict_contains_key_value(args[0][0], "key", "keyword")
               for args in mocked.call_args_list)

一些注释:

  • 如果您不确定该函数始终具有参数,则必须添加检查(例如,对于args [0]),
  • 如果该参数也可以称为关键字参数,则必须展开检查(args [1]关键字参数)
  • 函数dict_contains_key_value可以在需要时进行优化和扩展,这只是一个简单的示例

Assuming that your function has one argument, that is a nested dictionary and shall on some level contain the specific key/value pair, you have to iterate over that dictionary manually using some function.

You get the arguments of all calls of your mocked function from call_args_list. Each entry contains a tuple with the list of positional arguments and the keyword arguments, e.g. call_args_list[0][0][0] contains the first positional argument of the first call.

Assuming that your tested function have always exactly one arguments, that is called as a positional argument, you can do something like this:

def dict_contains_key_value(arg, key, value):
    if not isinstance(arg, dict):
        return False
    for k, v in arg.items():
        if k == key and v == value:
            return True
        if dict_contains_key_value(v, key, value):
            return True
    return False


@mock.patch("my_module.sut")
def test_called_arg(mocked):
    caller()  # the function that calls `sut()` several times
    mocked.assert_called()
    assert any(dict_contains_key_value(args[0][0], "key", "keyword")
               for args in mocked.call_args_list)

A few notes:

  • if you are not sure that the function has always an argument, you have to add a check (e.g. for args[0])
  • if the argument can also be called as a keyword argument, you have to expand the check (args[1] would give you the dictionary of keyword arguments)
  • the function dict_contains_key_value can be optimized and extended if needed, this is just a simple example
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文