小编ven*_*kat的帖子

如何在 python unittest 中每次使用不同的参数断言多个方法调用?

我正在使用 python unittest 来测试我的代码。作为我的代码的一部分,我正在使用这些

boto3.client('sts')
boto3.client('ec2')
boto3.client('ssm', arg1, arg2)
Run Code Online (Sandbox Code Playgroud)

所以我在编写测试用例之前嘲笑了 boto3 并将其作为参数。现在我可以断言 boto3.client 是否被调用。

但我想检查 boto3.client 是否使用 sts 调用,boto3.client 是否调用 wit ec2 以及 ssm、arg1、arg2。

当只有一个电话时,我可以使用boto3.client.assert_called_with('my parameters'). 但面临每次使用不同参数检查多个调用的问题。

@patch('createCustomer.src.main.boto3')
    def test_my_code(self, mock_boto3):
        # Executing main class
        mainclass(arg1, arg2)
        # Verifing
        mock_boto3.client.assert_called()
Run Code Online (Sandbox Code Playgroud)

我想实现像

mock_boto3.client.assert_called_once_with('sts')
mock_boto3.client.assert_called_once_with('ec2')
mock_boto3.client.assert_called_once_with('ssm',arg1,arg2)
Run Code Online (Sandbox Code Playgroud)

但这仅在第一个断言中给出错误,说 boto3.client 调用了 3 次,并且显示了最后一次调用的参数,即“ssm”、arg1、arg2

python mocking python-3.x python-unittest

4
推荐指数
1
解决办法
5053
查看次数

标签 统计

mocking ×1

python ×1

python-3.x ×1

python-unittest ×1