2013-04-02 1 views
1

수집 단계에서 매개 변수화가 필요한 일부 인수와 설정시 발생해야하는 매개 변수를 사용하여 매개 변수화하려는 테스트가 있습니다. 내가 간접 = True로 request.param으로 argname을 전달하려면 몇 가지 비품이 필요하기 때문에 pytest_generate_test 훅에서 metafunc.parametrize를 사용할 수 없지만 다른 인수는 indirect = False를 가져야합니다.pytest 테스트에는 수집 단계 및 설정시 매개 변수화가 필요합니다.

아이디어가 있습니까?

def pytest_generate_tests(metafunc): 
    if metafunc.function.__name__ == 'test_example': 
     argnames = [] 
     argvalues = [] 
     parameters = getattr(metafunc.function, 'paramlist',()) 
     for p in parameters: 
      if type(p) == list: 
       argnames = tuple(['myfixture'] + p) 
      else: 
       argvalues.append = tuple(['std'] + p['argvalues']) 
      argvalues.append = tuple(['pro'] + p['argvalues']) 
     # I want to do the following, but it won't work since some of the 
     # args need indirect set to true and some need indirect set to false. 
     metafunc.parametrize(argnames, argvalues, indirect=True) 
    elif 'myfixture' in metafunc.fixturenames: 
     # we have existing tests which use the fixture, but only with 
standard 
     metafunc.parametrize("myfixture", "std") 
    else: 
     # we have existing tests which use older style parametrization, 
non-fixture 
     for p in getattr(metafunc.function, 'paramlist',()): 
      metafunc.addcall(funcargs=p) 


def params(decolist): 
    def wrapper(function): 
     function.paramlist = decolist 
     return function 
    return wrapper 

@pytest.fixture 
def myfixture(request): 
    If request.param == 'std': 
     myfix = SomeObject() 
    elif request.param == 'pro': 
     myfix = SomeOtherObject() 
    def fin(): 
     myfix.close() 
    request.addfinalizer(fin) 
    return myfix 

@params([ 
    ['color', 'type'], 
    { 'argvalues': [ 'blue', 'cat'] }, 
    { 'argvalues': ['pink', 'dog'] } 
]) 
def test_example(myfixture, color, type): 
    # this is the new test we want to add 

def test_something(myfixture): 
    # existing test which only uses std fixture 

@params([ 
    {'arg1': 1, 'arg2': 2}, 
    {'arg1': 3, 'arg2': 5} 
]) 
def test_old_style(arg1, arg2): 
    # existing tests which don't use fixtures 

덕분에이를 통해 읽는 :

은 여기 내 테스트가 어떻게 생겼는지의 예와 제가하고 싶은이야! 나는 오히려 길다는 것을 안다.

답변

1

디자인 당 모든 매개 변수화는 수집시 발생합니다

관련 문제