다음과 같은 기능이 있습니다.
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 group\nCreate 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
섹션 코드 ( If remote groups set are not in local groups ) 의 동작을 확인하고 싶습니다 . 그래서 나는 가지고있다:
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)
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다