2011-01-12 6 views
29

일부 상태 변수를 검사하기 위해 테스트 함수 내에 일부 로깅 문을 넣고 싶습니다.py.test 테스트 내에서 로깅

나는 다음과 같은 코드를 가지고 :

import pytest,os 
import logging 

logging.basicConfig(level=logging.DEBUG) 
mylogger = logging.getLogger() 

############################################################################# 

def setup_module(module): 
    ''' Setup for the entire module ''' 
    mylogger.info('Inside Setup') 
    # Do the actual setup stuff here 
    pass 

def setup_function(func): 
    ''' Setup for test functions ''' 
    if func == test_one: 
     mylogger.info(' Hurray !!') 

def test_one(): 
    ''' Test One ''' 
    mylogger.info('Inside Test 1') 
    #assert 0 == 1 
    pass 

def test_two(): 
    ''' Test Two ''' 
    mylogger.info('Inside Test 2') 
    pass 

if __name__ == '__main__': 
    mylogger.info(' About to start the tests ') 

    pytest.main(args=[os.path.abspath(__file__)]) 

    mylogger.info(' Done executing the tests ') 

나는 다음과 같은 출력을 얻을 다음 '__name__ == __main__' 블록 만 로깅 메시지가 콘솔로 전송받을

[bmaryada-mbp:/Users/bmaryada/dev/platform/main/proto/tests/tpch $]python minitest.py 
INFO:root: About to start the tests 
======================================================== test session starts ========================================================= 
platform darwin -- Python 2.6.2 -- pytest-2.0.0 
collected 2 items 

minitest.py .. 

====================================================== 2 passed in 0.01 seconds ====================================================== 
INFO:root: Done executing the tests 

공지 사항.

pytest가 테스트 메소드에서 콘솔 로깅을 강제로 내보내도록 할 수 있습니까? 나를 위해

+2

py.test의 작성자가 게시 한 [this answer] (http://stackoverflow.com/a/11877951/148680)를 참조하십시오. 그는 고도의 다양성을 제공하는 pytest 플러그인을 제안합니다. – chb

답변

17

작품, 여기에 내가 얻을 출력입니다 : [싹둑가 -> 예는 올바르지]

편집 : 당신이 표준 출력을 캡처하지 않습니다 때문에 py.test하는 -s 옵션을 통과해야 것으로 보인다. 여기 (py.test가 설치되어 있지 않음) python pytest.py -s pyt.py을 사용하면 충분합니다. 코드에 대한

는 당신이 필요 argsmain-s을 전달하는 것입니다 :

pytest.main(args=['-s', os.path.abspath(__file__)]) 

capturing output에 py.test 설명서를 참조하십시오.

+0

죄송합니다. 서둘러 코드를 붙여 넣었습니다. '문제'에 주목하려면 'test_one'함수에서 'assert 0 == 1'을 제거하십시오. 어떤 오류가있을 때만 (나는 거짓 주장을함으로써 강제로), py.test는 로깅 정보를 출력하는 것으로 보인다. – superselector

+0

문제는 없지만 프로그래밍 방식을 찾고 명령 줄에서 수정하는 방법을 발견했습니다. – TryPyPy

+1

로깅 출력을 기본 암시 적 stderr 대신 일부 파일로 리디렉션 할 수도 있습니다. – hpk42