【发布时间】:2022-01-23 20:25:42
【问题描述】:
取自这个答案:
我现在有这个代码:
test_2.py
from unittest import TestCase
import boto3
import pytest
from moto import mock_ssm
@pytest.yield_fixture
def s3ssm():
with mock_ssm():
ssm = boto3.client("ssm")
yield ssm
@mock_ssm
class MyTest(TestCase):
def setUp(self):
ssm = boto3.client("ssm")
ssm.put_parameter(
Name="/mypath/password",
Description="A test parameter",
Value="this is it!",
Type="SecureString",
)
def test_param_getting(self):
import real_code
resp = real_code.get_variable("/mypath/password")
assert resp["Parameter"]["Value"] == "this is it!"
这是我要测试的代码(或缩减示例):
real_code.py
import boto3
class ParamTest:
def __init__(self) -> None:
self.client = boto3.client("ssm")
pass
def get_parameters(self, param_name):
print(self.client.describe_parameters())
return self.client.get_parameters_by_path(Path=param_name)
def get_variable(param_name):
p = ParamTest()
param_details = p.get_parameters(param_name)
return param_details
我尝试了很多解决方案,在pytest和unittest之间切换了好几次!
每次我运行代码时,它都不会连接到 AWS,因此似乎有些东西正在影响 boto3 客户端,但它没有返回参数。如果我编辑 real_code.py 以使其内部没有类,则测试通过。
难道不能在real_code.py文件的类里面给客户端打补丁吗?如果可能的话,我试图在不编辑 real_code.py 文件的情况下做到这一点。
谢谢,
【问题讨论】:
标签: python-3.x unit-testing boto3 moto