2012-06-13 3 views
2

나는 py.test를 사용하여 몇 가지 테스트를 작성하고 테스트에서는 funcargs를 사용한다. 이 funcargs는 자신의 설정 및 분해 분석은이 같은 conftest.py에 정의 :py.test : teeownown을 호출하기 위해 KeyboardInterrupt를 얻는다

conftest.py :

def pytest_funcarg__resource_name(request): 
    def setup(): 
    # do setup 
    def teardown(): 
    # do teardown 

누군가가 잎 테스트 실행을 중지하려면 Ctrl + C를 사용하는 경우 내 문제입니다 모든 것이 teardowned. 후크 pytest_keyboard_interrupt하지만 거기에서 무엇을 해야할지 모르겠다.

멍청한 질문에 사과드립니다.

답변

3

전체 예제를 제공하지 않으므로 내가 누락 된 것일 수 있습니다. 당신이 이것을 실행하면

def pytest_funcarg__res(request): 
    def setup(): 
     print "res-setup" 
    def teardown(val): 
     print "res-teardown" 
    return request.cached_setup(setup, teardown) 

def test_hello(res): 
    raise KeyboardInterrupt() 

는 "py.test는"당신이 얻을 :

============================= test session starts ============================== 
platform linux2 -- Python 2.7.3 -- pytest-2.2.5.dev4 
plugins: xdist, bugzilla, pep8, cache 
collected 1 items 

tmp/test_keyboardinterrupt.py res-setup 
res-teardown 


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! KeyboardInterrupt !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 
/home/hpk/p/pytest/tmp/test_keyboardinterrupt.py:10: KeyboardInterrupt 

것을 보여준다 그러나 여기 그것이 작동하는 방법의 예() 도우미 request.cached_setup를 사용한다 테스트 실행 중에 KeyboardInterrupt가 발생하면 setup과 teardown이 호출됩니다.

+1

고마워요! 문제의 원인을 찾도록 도와 주셨습니다. 내 문제는'Setup' 과정에서'KeyboardInterrupt'가 발생했을 때입니다. 이 경우'teardown'은 호출되지 않습니다. 'teardown '은'setup'단계에서 생성 될 것으로 예상되는 내용을 제거 할 수 있기 때문에 이것은 합리적입니다. 그러나 이것은 물건을 더러운 상태로 남겨 둘 수 있습니다. 이걸 어떻게 처리하니? – Parham

+0

@Parham : 새로운 질문 게시에 적합한 후보로 들립니다. – Flimm

관련 문제