简体   繁体   English

找不到如何使用 pytest 测试此代码

[英]Can't find out how to test this code with pytest

I have the following piece of code and I need to test it using pytest mocker.我有以下代码,我需要使用 pytest 模拟程序对其进行测试。 I have tried tens of various combinations but couldn't find one that works.我尝试了数十种不同的组合,但找不到一种有效的组合。 Can you help?你能帮我吗?

    def authenticate(self, account_id, region, role, credential_file) -> dict:
        self.aws_credentials = get_credentials(credential_file)
        sts_client = boto3.client(
            "sts",
            aws_access_key_id=self.aws_credentials["key"],
            aws_secret_access_key=self.aws_credentials["value"],
            region_name=region,
        )
        assumed_role = sts_client.assume_role(
            RoleArn=f"arn:aws:iam::{account_id}:role/{role}",
            RoleSessionName="AssumeRoleSession1",
        )
        return assumed_role["Credentials"]

First call to get_credentials is easily patched.第一次调用 get_credentials 很容易修补。 But then I need to mock the STS client and then mock again the assume_role call.但随后我需要模拟 STS 客户端,然后再次模拟假设角色调用。 I tried this but get an assertion error我试过这个但得到一个断言错误

def test_authenticate(self, mocker):
    def fake_assume_role(RoleArn: str = "", RoleSessionName: str = ""):
        return {"Credentials": "Valid Credentials"}
        
    mocker.patch(
        "get_credentials",
        return_value={"value": "valid_value", "key": "valid_key"},
    )
    mock_sts_client = mocker.patch(
        "boto3.client")
    mock_sts_client.assume_role = fake_assume_role
    auth = pylib.authentication.AWSAuthentication()
    actual_creds = auth.authenticate(
        account_id="123456789012",
        region="far-west-1",
        role="Cyrano",
        credential_file="cred.json"
        )
    assert actual_creds == "Valid Credentials"

I did this and it worked for me:我这样做了,它对我有用:

def test_authenticate(self, mocker):
    mocker.patch(
        "get_credentials",
        return_value={"value": "valid_value", "key": "valid_key"},
    )
    mock_sts_client = mocker.Mock()
    mock_sts_client.assume_role.return_value = {"Credentials": "Valid Credentials"}
    mocker.patch('boto3.client', return_value=mock_sts_client)
    auth = AWSAuthentication()
    actual_creds = auth.authenticate(
        account_id="123456789012",
        region="far-west-1",
        role="Cyrano",
        credential_file="cred.json"
        )
    assert actual_creds == "Valid Credentials"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Pytest 在测试文件夹中找不到文件 - Pytest doesnt find file out of the test folder 使用 pytest 和 SonarQube,我如何在 if 语句中覆盖间接无法访问的代码? - Using pytest and SonarQube, How can I cover code in an if statement that circumstantially can't be reached? 如何在python中测试两个字典是否与pytest相等 - How can you test that two dictionaries are equal with pytest in python 我在使用 pytest 的网页上找不到任何元素,但我可以使用控制台找到相同的元素 - I can't find any elements on the webpage using pytest, but I can find the same elements using console pytest 可以忽略导入依赖吗? 我想对 Pi 代码进行单元测试 - Can pytest ignore import dependencies? I want to unit test Pi code 在 Python 3 中,使用 Pytest,我们如何测试退出代码:python 程序的退出代码:exit(1) 和 exit(0)? - In Python 3, using Pytest, how do we test for exit code : exit(1) and exit(0) for a python program? 使用 Python 3.7 和 Selenium,我不知道如何解决我的代码中的视口元素问题 - Using Python 3.7 and Selenium, I can't figure out how to troubleshoot my code for out of Viewport elements python pytest - 如何在其夹具方法中获取测试标记? - python pytest - How can I get a test markers inside its fixture methods? 无法通过以下代码找出错误 - can't figure out the error with the following code 使用argparse进行Pytest:如何测试用户是否提示确认? - Pytest with argparse: how to test user is prompted for confirmation?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM