2017-09-15 1 views
1

내가 시험이 같은 다른기구에 의존하는 pytest.fixture과 스위트,이 사용 테스트 매개 변수화 : 일반 시험에 대한Pytest가 고정

@pytest.fixture 
def params(): 
    return {'foo': 'bar', 'baz': 1} 

@pytest.fixture 
def config(): 
    return ['foo', 'bar', 'baz'] 

@pytest.client 
def client(params, config): 
    return MockClient(params, config) 

을, 난 그냥 client에 전달하고 그것을 잘 작동합니다 :

그러나 매개 변수화 된 테스트의 경우 해당 픽스처를 사용하는 것은 정말 어색합니다. 모든 조명기 메소드를 직접 호출해야하는데, 이는 어느 정도 목적을 저지합니다. (paramsconfig 조명기는 다른 곳에서 사용되므로 client으로 붕괴시키고 싶지는 않습니다.

@pytest.mark.parametrize('thing,expected', [ 
    (client(params(), config()).method_with_args(arg1, arg2), 100), 
    (client(params(), config()).method_with_args(arg2, arg4), 200), 
]) 
def test_parameters(thing, expected): 
    assert thing == expected 

이 클리너를 만드는 방법이 있습니까? 이 지저분한 코드가 반복적 인 유사한 테스트보다 낫지는 않습니다.

답변

1

메서드 호출 결과 대신 인수를 매개 변수화하는 방법은 어떻습니까?

@pytest.mark.parametrize('args,expected', [ 
    ((arg1, arg2), 100), 
    ((arg2, arg4), 200), 
]) 
def test_parameters(client, args, expected): 
    assert client.method_with_args(*args) == expected 
관련 문제