2017-12-18 27 views
1

Pytest에서는 이전 결과를 저장하고 현재/현재 결과를 여러 반복에 대해 이전과 비교해야하는 다음 작업을 수행하려고합니다. 나는 다음과 같은 방법으로 수행 한 : 나는 때마다 나는 루프 위와 같이 수행 할 때pytest의 전역 변수

@pytest.mark.parametrize("iterations",[1,2,3,4,5]) ------> for 5 iterations 
@pytest.mark.parametrize("clsObj",[(1,2,3)],indirect = True) ---> here clsObj is the instance. (clsObj.currentVal, here clsObj gets instantiated for every iteration and it is instance of **class func1**) 

presentVal = 0 
assert clsObj.currentVal > presrntVal 
clsObj.currentVal = presentVal 

presentVal가의 0에 할당 수 (이 지역 변수이기 때문에 예상). 위의 코드 대신 presentValglobal presentVal과 같이 선언하려고 시도했으며 내 테스트 케이스 위에 presentVal을 초기화했지만 잘 돌아 가지 않았습니다.

class func1(): 
    def __init__(self): 
     pass 
    def currentVal(self): 
     cval = measure() ---------> function from where I get current values 
     return cval 

사람은 사전에 pytest 또는 다른 좋은 방법

감사에서 전역 변수를 선언하는 방법을 제안 할 수 있습니다!

+0

("계수와 같은 사소한 것이 될 수 있습니다 ", range (5))'이것이 도움이되는지 확실하지 않습니다. [this] (https://stackoverflow.com/questions/42228895/how-to-parametrize-a-pytest-fixture)를 참조하십시오. –

답변

1

당신이 찾고있는 것을 "조명기"라고합니다. 다음의 예를 살펴 보라가 문제를 해결해야합니다

import pytest 

@pytest.fixture(scope = 'module') 
def global_data(): 
    return {'presentVal': 0} 

@pytest.mark.parametrize('iteration', range(1, 6)) 
def test_global_scope(global_data, iteration): 

    assert global_data['presentVal'] == iteration - 1 
    global_data['presentVal'] = iteration 
    assert global_data['presentVal'] == iteration 

당신은 본질적으로 테스트를 걸쳐 고정 인스턴스를 공유 할 수 있습니다. 그것은 데이터베이스 접근 객체와 같은 더 복잡한 물건 의도,하지만 사전 : 더 나은`@의 pytest.mark.parametrize를 사용하면 반복을 할 수있는 스타터

Scope: sharing a fixture instance across tests in a class, module or session