2016-11-02 3 views
0

저는 python과 테스트를 처음 사용합니다. 자동화 테스트를해야합니다. 내 webApplication 테스트하려면 다음 코드를 사용하고 있습니다. 나는 Excel을 사용하여 입력 데이터를 읽거나 Assert를 사용하지 않고 프로그래밍 로직을 사용하여 Pass 또는 Fail로 테스트 출력을 작성할 수 있습니다.Selenium과 Python의 출력 캡처

내가 assert를 사용할 때 콘솔에서 출력을 얻지 만 캡처 할 수 없어 Excel에 넣을 수 없습니다. 내가 사용하고

코드는 누군가가 내가 조직적인 방법으로 엑셀에서 그들을 밀어 수 있도록 몇 가지 변수에이 출력을 캡처하는 나를 도울 수

xample$ python assert.py 
Traceback (most recent call last): 
File "assert.py", line 10, in <module> 
    assert 2 + 2 == 5, error 
AssertionError: Houstan you got some error 

콘솔

에서

from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions 
from selenium.common.exceptions import TimeoutException 
from selenium.webdriver.common.keys import Keys 
import unittest 
import os 

error = "Houstan you got some error" 
assert 2 + 2 == 5, 'error' 

출력이다. 데이터가 다른 변수 또는 목록에 저장 될 수 있습니다.

testdoc.xls

파일 ----------- 라인 ------ 오류

assert.py --- (10) ------ Houstan 당신 약간의 오류가 있습니다

+0

당신이 로깅 라이브러리를 사용하여 생각 해 봤나 예제와 함께 가득? – Eduard

답변

0

traceback 모듈은 오류 정보를 형식화하고 처리하는 데 유용합니다. 예를 들어

import unittest 
import os 
import traceback 

try: 
    error = "Houstan you got some error" 
    assert 2 + 2 == 5, 'error' 
except Exception: 
    var = traceback.format_exc() 
    # now your traceback is in the variable var 
    print ('not failed', var) 
    # process the var as you want, then re raise the error 
    raise 

문서는

+0

감사합니다. –

0

로깅 라이브러리 사용을 고려하십시오.

import logging 

logging.basicConfig(filename='foo.log', 
        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', 
        level=logging.INFO) 
try: 
    2 + 2 == 5 
except Exception as e: 
    logging.error('error msg', exc_info=True)