Tôi muốn đặt một số câu lệnh ghi nhật ký trong hàm kiểm tra để kiểm tra một số biến trạng thái.Ghi nhật ký trong các thử nghiệm py.test
Tôi có đoạn mã sau:
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 ')
tôi nhận được kết quả như sau:
[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
Chú ý rằng chỉ những thông điệp logging từ khối '__name__ == __main__'
được chuyển đến giao diện điều khiển.
Có cách nào để buộc pytest phát ra ghi nhật ký để điều khiển từ phương pháp thử không?
Bạn có thể xem [câu trả lời này] (http://stackoverflow.com/a/11877951/148680), được đăng bởi tác giả của py.test. Ông đề nghị một plugin pytest cung cấp một mức độ cao của tính linh hoạt. – chb