类型错误:'list'对象不可调用 - assertWarns()



我有以下功能:

def checking_remote_local_groups(data: List[dict], groups: List[dict]) -> List[dict]:
step = 'GET_GROUPS_TO_CHECK'
groups_to_check = []
local_groups = [x['group'] for x in data]
# Using set to get differences between local and AAD groups
local_groups_set = set(local_groups)

groups_set = set(x['name'] for x in groups)
# If local AAD groups set are not in AAD groups, raise an Error on the workflow.
if local_groups_set - groups_set:
# SyncException is implemented somewhere.
raise SyncException(
f'{local_groups_set - groups_set} local group is not an AAD groupnCreate it via speckle-infra terraform',
"We need to get in line, local and AAD groups. Additional {} group received from users.yaml".format(
local_groups_set - groups_set
),
step
)
# If remote groups set are not in local groups 
if groups_set - local_groups_set:
# Just notify it to the user
warnings.warn(
f"""
{groups_set - local_groups_set} AAD group is not on users.yml as a local group
Please add it if needed
"""
)
# If remote groups are the same than local groups
elif local_groups_set == groups_set:
print("AAD and local groups are inline"
)
return groups_to_check

我想通过使用assertWarns断言进行单元测试来检查第二个if部分代码的行为(如果远程组集不在本地组中(,因为在这种情况下我会触发警告。所以我有:

class MyTestCase(unittest.TestCase):
def test_warning_checking_remote_group_not_in_local_groups(self):
# local groups
data = [{'group': 'foobar'}, {'group': 'test'}]

# remote groups
groups = [{'name': 'foobar'}, {'name': 'test'}, {'name': 'missing'}]
self.assertWarns(
UserWarning,
checking_aad_local_groups(data, groups),
[{"name": "missing"}]
)

我得到了这个输出:

test_sync.py .F...........                                               [100%]
=================================== FAILURES ===================================
______ GroupsTestCase.test_warning_checking_aad_group_not_in_local_groups ______
self = <test_sync.GroupsTestCase testMethod=test_warning_checking_remote_group_not_in_local_groups>
def test_warning_checking_remote_group_not_in_local_groups(self):
data = [{'group': 'foobar'}, {'group': 'test'}]
groups = [{'name': 'foobar'}, {'name': 'test'}, {'name': 'missing'}]
>       self.assertWarns(
UserWarning,
checking_aad_local_groups(data, groups),
[{"name": "missing"}]
)
E       TypeError: 'list' object is not callable
test_sync.py:158: TypeError
=============================== warnings summary ===============================
test_sync.py::GroupsTestCase::test_warning_checking_aad_group_not_in_local_groups
/home/runner/work/speckle-user-management/speckle-user-management/groups.py:31: UserWarning: 
*********************** WARNING ... ***********************
{'missing'} AAD group is not on users.yml as a local group
Please add it if needed
*********************** WARNING ... ***********************

warnings.warn(
-- Docs: docs.pytest.org/en/latest/warnings.html
=========================== short test summary info ============================
FAILED test_sync.py::GroupsTestCase::test_warning_checking_remote_group_not_in_local_groups

我知道我作为参数发送的词典列表可能不被接受,或者不适用于assertWarns()期望的数据类型?

尽管如此,还是显示了我所期望的消息({'missing'} AAD group is not on users.yml as a local group, Please add it if needed(,但我希望通过这次测试

使用assertWarns()的最佳方式是什么。

如果我使用assertFalse,则测试通过,尽管我不确定这是否是我能使用的最具体/更好的断言。

当您调用assertWarns(UserWarning, checking_aad_local_groups(...))时,您正在调用checking_aad_local_groups并将结果传递给assertWarns。您希望为assertWarns提供一个可调用的,以便它可以将其封装在警告检测代码中。

self.assertWarns(UserWarning, checking_aad_local_groups, (data, groups))

或者,您可以将其用作上下文管理器

with self.assertWarns() as w:
checking_aad_local_groups(data, groups)

相关内容

  • 没有找到相关文章

最新更新