自動(dòng)化測(cè)試用例的調(diào)試信息非常有用促王,可以讓我們知道現(xiàn)在的運(yùn)行情況到犀盟,執(zhí)行到哪步以及相應(yīng)的出錯(cuò)信息等,可以在pytest里面蝇狼,有時(shí)并不會(huì)輸出所有信息阅畴,比如默認(rèn)情況下pass的測(cè)試用例是沒有print輸出的。本文將介紹如何在pytest里面實(shí)時(shí)顯示所有的log信息迅耘。
1. 用print輸出log信息
slowTest_print.py
import time
def test_1():
print 'test_1'
time.sleep(1)
print 'after 1 sec'
time.sleep(1)
print 'after 2 sec'
time.sleep(1)
print 'after 3 sec'
assert 1, 'should pass'
def test_2():
print 'in test_2'
time.sleep(1)
print 'after 1 sec'
time.sleep(1)
print 'after 2 sec'
time.sleep(1)
print 'after 3 sec'
assert 0, 'failing for demo purposes'
運(yùn)行上述程序贱枣,pytest會(huì)capture所有的輸出,保存直到所有的測(cè)試用例都執(zhí)行結(jié)束颤专,并且只輸出那些失敗的測(cè)試用例的信息纽哥,對(duì)于成功的測(cè)試用例,沒有print的信息顯示栖秕。
從下面的運(yùn)行結(jié)果春塌,如果需要查看test_1()的運(yùn)行情況,沒有l(wèi)og信息可看,print沒有顯示摔笤。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -v slowTest_print.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0 -- C:\Python27\python.exe
cachedir: .cache
metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:
plugins: metadata-1.3.0, html-1.14.2
collected 2 items
slowTest_print.py::test_1 PASSED
slowTest_print.py::test_2 FAILED
================================== FAILURES ===================================
___________________________________ test_2 ____________________________________
def test_2():
print 'in test_2'
time.sleep(1)
print 'after 1 sec'
time.sleep(1)
print 'after 2 sec'
time.sleep(1)
print 'after 3 sec'
> assert 0, 'failing for demo purposes'
E AssertionError: failing for demo purposes
E assert 0
slowTest_print.py:22: AssertionError
---------------------------- Captured stdout call -----------------------------
in test_2
after 1 sec
after 2 sec
after 3 sec
===================== 1 failed, 1 passed in 6.45 seconds ======================
C:\Users\yatyang\PycharmProjects\pytest_example>
我們可以用‘-s’參數(shù)或者 ‘–capture=no’够滑,這樣就可以輸出所有測(cè)試用的print信息。但是pytest還是會(huì)等著所有的測(cè)試用例都執(zhí)行完畢才會(huì)顯示運(yùn)行結(jié)果吕世≌么ィ可以看到下面的test_1也顯示出print的相關(guān)信息。
C:\Users\yatyang\PycharmProjects\pytest_example>py.test --capture=no slowTest_print.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:
plugins: metadata-1.3.0, html-1.14.2
collected 2 items
slowTest_print.py test_1
after 1 sec
after 2 sec
after 3 sec
.in test_2
after 1 sec
after 2 sec
after 3 sec
F
================================== FAILURES ===================================
___________________________________ test_2 ____________________________________
def test_2():
print 'in test_2'
time.sleep(1)
print 'after 1 sec'
time.sleep(1)
print 'after 2 sec'
time.sleep(1)
print 'after 3 sec'
> assert 0, 'failing for demo purposes'
E AssertionError: failing for demo purposes
E assert 0
slowTest_print.py:22: AssertionError
===================== 1 failed, 1 passed in 6.17 seconds ======================
2. Python Logging用法
一般情況下命辖,一些程序的調(diào)試過程中我們會(huì)讓它輸出一些信息况毅,特別是一些大型的程序,我們通過這些信息可以了解程序的運(yùn)行情況尔艇,python提供了一個(gè)日志模塊logging尔许,它可以把我們想要的信息全部保存到一個(gè)日志文件中,方便查看终娃。
import logging
logging.debug('This is debug message')
logging.info('This is info message')
logging.warning('This is warning message')
屏幕上打印:
WARNING:root:This is warning message
默認(rèn)情況下味廊,logging將日志打印到屏幕,日志級(jí)別為WARNING棠耕;
日志級(jí)別大小關(guān)系為:CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET余佛,當(dāng)然也可以自己定義日志級(jí)別。
3. 在pytest中用logging代替print
我們現(xiàn)在來看看在pytest的測(cè)試用例里面用logging的輸出代替print窍荧,有什么不同辉巡。
slowTest_logging.py
import time
import logging
logging.basicConfig(level=logging.DEBUG)
def test_1():
log = logging.getLogger('test_1')
time.sleep(1)
log.debug('after 1 sec')
time.sleep(1)
log.debug('after 2 sec')
time.sleep(1)
log.debug('after 3 sec')
assert 1, 'should pass'
def test_2():
log = logging.getLogger('test_2')
time.sleep(1)
log.debug('after 1 sec')
time.sleep(1)
log.debug('after 2 sec')
time.sleep(1)
log.debug('after 3 sec')
assert 0, 'failing for demo purposes'
運(yùn)行結(jié)果如下,log信息的顯示是不是可讀性更好了呢∪锿耍可是pytest還是要等所有的結(jié)果都運(yùn)行完畢才完全輸出到屏幕上郊楣,沒法看到實(shí)時(shí)的運(yùn)行情況。比如現(xiàn)在要測(cè)試一個(gè)新的image瓤荔,不知道quality如何净蚤,如果測(cè)試用例非常多,測(cè)試人員就得一直等茉贡,也許前面的一些測(cè)試用都失敗就可以停止執(zhí)行了塞栅。那怎么實(shí)現(xiàn)實(shí)時(shí)顯示呢?請(qǐng)看方法4腔丧。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest slowTest_logging.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:
plugins: metadata-1.3.0, html-1.14.2
collected 2 items
slowTest_logging.py .F
================================== FAILURES ===================================
___________________________________ test_2 ____________________________________
def test_2():
log = logging.getLogger('test_2')
time.sleep(1)
log.debug('after 1 sec')
time.sleep(1)
log.debug('after 2 sec')
time.sleep(1)
log.debug('after 3 sec')
> assert 0, 'failing for demo purposes'
E AssertionError: failing for demo purposes
E assert 0
slowTest_logging.py:25: AssertionError
---------------------------- Captured stderr call -----------------------------
DEBUG:test_2:after 1 sec
DEBUG:test_2:after 2 sec
DEBUG:test_2:after 3 sec
===================== 1 failed, 1 passed in 6.37 seconds ======================
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -s slowTest_logging.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:
plugins: metadata-1.3.0, html-1.14.2
collected 2 items
slowTest_logging.py DEBUG:test_1:after 1 sec
DEBUG:test_1:after 2 sec
DEBUG:test_1:after 3 sec
.DEBUG:test_2:after 1 sec
DEBUG:test_2:after 2 sec
DEBUG:test_2:after 3 sec
F
================================== FAILURES ===================================
___________________________________ test_2 ____________________________________
def test_2():
log = logging.getLogger('test_2')
time.sleep(1)
log.debug('after 1 sec')
time.sleep(1)
log.debug('after 2 sec')
time.sleep(1)
log.debug('after 3 sec')
> assert 0, 'failing for demo purposes'
E AssertionError: failing for demo purposes
E assert 0
slowTest_logging.py:25: AssertionError
===================== 1 failed, 1 passed in 6.18 seconds ======================
4. pytest用logging和--capture=no實(shí)現(xiàn)實(shí)時(shí)輸出log信息
請(qǐng)自己去運(yùn)行下面的程序吧,可以看到該程序是實(shí)時(shí)輸出當(dāng)前測(cè)試用例執(zhí)行的情況作烟。
C:\Users\yatyang\PycharmProjects\pytest_example>pytest -s slowTest_logging.py
============================= test session starts =============================
platform win32 -- Python 2.7.13, pytest-3.0.6, py-1.4.32, pluggy-0.4.0
metadata: {'Python': '2.7.13', 'Platform': 'Windows-7-6.1.7601-SP1', 'Packages': {'py': '1.4.32', 'pytest': '3.0.6', 'pluggy': '0.4.0'}, 'JAVA_HOME': 'C:\\Program Files (x86)\\Java\\jd
k1.7.0_01', 'Plugins': {'html': '1.14.2', 'metadata': '1.3.0'}}
rootdir: C:\Users\yatyang\PycharmProjects\pytest_example, inifile:
plugins: metadata-1.3.0, html-1.14.2
collected 2 items
slowTest_logging.py DEBUG:test_1:after 1 sec
DEBUG:test_1:after 2 sec
DEBUG:test_1:after 3 sec
.DEBUG:test_2:after 1 sec
DEBUG:test_2:after 2 sec
DEBUG:test_2:after 3 sec
F
================================== FAILURES ===================================
___________________________________ test_2 ____________________________________
def test_2():
log = logging.getLogger('test_2')
time.sleep(1)
log.debug('after 1 sec')
time.sleep(1)
log.debug('after 2 sec')
time.sleep(1)
log.debug('after 3 sec')
> assert 0, 'failing for demo purposes'
E AssertionError: failing for demo purposes
E assert 0
slowTest_logging.py:25: AssertionError
===================== 1 failed, 1 passed in 6.20 seconds ======================
5.總結(jié)
在寫自動(dòng)化測(cè)試用例時(shí)愉粤,添加有用的log信息是非常有必要的。比如在初期的調(diào)試過程拿撩,能夠一旦運(yùn)行有問題衣厘,就可以獲取到精確的調(diào)試信息影暴。后期在穩(wěn)定的運(yùn)行中型宙,其他測(cè)試人員來運(yùn)行也可以很容易上手魂拦,所以大家一定要重視測(cè)試用例的調(diào)試信息。
通過本文荷愕,應(yīng)該知道如何用pytest,logging和--capture=no實(shí)現(xiàn)運(yùn)行測(cè)試用例的實(shí)時(shí)輸出所有的log信息茂契。