0

사용자의 권한을 테스트하기 위해 테스트 케이스를 작성해야합니다. 각 테스트 케이스에서 UserA는 다른 권한을 가지며 검사를 수행합니다. setupAmethod에서 userA 다른 권한을 제공하기 위해 admin 역할을 사용하고 싶습니다. setup_method에 매개 변수를 전달하면 각 테스트 케이스가 시작되기 전에 다른 테스트 케이스를 가질 수 있습니까? 다음과 같은 것이 있지만 setup_method에 매개 변수를 전달하는 방법을 모르겠습니다.pytest에 대해 setup_method에 매개 변수를 전달하는 방법

class TestPermission(): 

    @classmethod 
    class setup_method(self, permission): 
     login as amdin 
     provide permission to userA 
     logout 
     login as userA 

    @classmethod 
    class teardown_method(self): 
     logout as userA 

    @fixure(permission1) 
    class test_permissionA(self): 
     assert drive.find_element_by_xpath('//div[@id="permission1"]') is True 
     assert drive.find_element_by_xpath('//div[@id="permission2"]') is False 
     assert drive.find_element_by_xpath('//div[@id="permission3"]') is False 

    @fixure(permission2) 
    class test_permissionB(self): 
     assert drive.find_element_by_xpath('//div[@id="permission1"]') is False 
     assert drive.find_element_by_xpath('//div[@id="permission2"]') is True 
     assert drive.find_element_by_xpath('//div[@id="permission3"]') is False 

답변

1

현재 매개 변수화기구를 사용해야합니다

https://docs.pytest.org/en/latest/fixture.html#fixture-parametrize 그래서 최종 코드는 다음과 같이 보일 것입니다 :

@pytest.fixture(scope="function", params=[{'permission': 'permission1', 'expected_result': {'perm1': True, 'perm2': False, 'perm3': False}}, {'permission': 'permission2', 'expected_result': {'perm1': False, 'perm2': True, 'perm3': False}}]) 
def test_cases(request): 
    admin_user.set_permission_to_userA(request.param.get('permission')) 
    return request.param 

def test_userA_permissions(test_cases): 
    login_with_userA() 
    assert drive.find_element_by_xpath('//div[@id="permission1"]') is test_cases.get('expected_result').get('perm1') 
    assert drive.find_element_by_xpath('//div[@id="permission2"]') is test_cases.get('expected_result').get('perm2') 
    assert drive.find_element_by_xpath('//div[@id="permission3"]') is test_cases.get('expected_result').get('perm3') 

으로는 데이터가 구동되는 하나 개의 시험이 발생합니다.

+0

다른 권한에 따라 어설 션 결과를 사용자 정의해야하는 경우 어떻게해야합니까? 어쨌든 각 테스트 케이스에 전달할 매개 변수를 지정할 수 있습니까? userA_permission처럼 a, b, c를 확인해야합니다. userB_permission, d, e, f를 확인해야합니다. – jacobcan118

+0

이 경우 예상되는 결과로 dict 대신 목록을 사용할 수 있습니다. 또는 귀하의 필요에 맞는 다른 복잡한 물건. – askalozubov

+0

그것이 작동하지 않는다면 각 권한에 대해 별도의 테스트를 할 수 있지만 테스트 내에서 (fixtures를 사용하지 않고) 첫 번째 단계로 userA에게 set 권한을 호출 할 수 있습니다 : def set_permission_to_userA (permission) : login_with_admin set_permission (허가) 데프 test_premission_one() set_permission_to_userA (permission1) login_with_userA() verifications_here() DEF test_premission_two() set_permission_to_userA (permission2) login_with_userA() verifications_here() 0 ' – askalozubov