【pytest學習】pytest+logging日志打印


一、pytest.ini文件配置

首先pytest是從pytest.ini中讀取log_cli配置的,默認是關閉的。首先需要在執行文件根目錄配置pytest.ini文件

[pytest]
log_cli = 1
log_cli_level = DEBUG
log_cli_date_format = %Y-%m-%d-%H-%M-%S
log_cli_format = %(asctime)s - %(filename)s - %(module)s - %(funcName)s - %(lineno)d - %(levelname)s - %(message)s
log_file = test.log # 日志文件記錄
log_file_level = DEBUG
log_file_date_format = %Y-%m-%d-%H-%M-%S
log_file_format = %(asctime)s - %(filename)s - %(module)s - %(funcName)s - %(lineno)d - %(levelname)s - %(message)s

使用命令行執行時加上【pytest pytest_lean2.py -o log_cli=true -o log_cli_level=INFO】也可以達到目的

二、logging代碼處理
'''
log = logging.getLogger(__name__)
def test_product(self):
# 創建產品
  log.info('創建產品')
  log.info(productInfo)
'''
三、執行效果

 

 

 三、pip國內源替換

[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host = pypi.tuna.tsinghua.edu.cn
其他國內源
阿里雲 http://mirrors.aliyun.com/pypi/simple/
中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/
中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/

四、pytest報錯 make sure your test modules/packages have valid Python names.

ImportError while importing test module 'D:\Python\PycharmProjects\PYDEMO\TestCase\main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
TestCase\test_suite.py:6: in <module>
from TestCase import test_http_request
E ModuleNotFoundError: No module named 'TestCase'
————————————————

方法 1 在要執行pytest 的根目錄新建 conftest.py文件;文件內容

import os

import sys

sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) #conftest所在目錄的上一級目錄加入到Python path中

sys.path.insert(0, os.path.abspath(os.path.dirname(__file__))) #conftest所在目錄加入到Python path中


方法2 pytet 在執行測試 test case 目錄下每個目錄都包含 __init__.py文件 ;在項目的跟目錄執行

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM