【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:PythonPycharmProjectsPYDEMOTestCasemain.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
TestCase est_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文件 ;在项目的跟目录执行

原文地址:https://www.cnblogs.com/ricebug2/p/14143086.html