python pytest测试框架介绍五---日志实时输出
同样的,在使用pytest进行自动化测试时,需要将实时日志打印出来,而不是跑完后才在报告中出结果。
不过,好在pytest在3.3版本开始,就支持这一功能了,而不用再像nose一样,再去装第三方插件。
网上也有相关实时的日志输入说明,但我尝试后,不是我想要的,比如:pytest输出Log
看看我们下面这样一段代码,以unittest模式写的:
#coding:utf-8
'''
Created on 2017年8月31日
@author: huzq
''' from __future__ import print_function
import pytest
from unittest import TestCase
from selenium import webdriver
import logging,sys log = logging.getLogger(__name__) class TestClass(TestCase): @classmethod
def setUpClass(cls):
log.info('setup_class()')
cls.driver = webdriver.Firefox()
cls.driver.get("http://www.baidu.com")
log.info("xxxxxxxxxxxxxxx") @classmethod
def teardown_class(cls): log.info('teardown_class()') def setUp(self):
log.info('\nsetup_method()')
self.addCleanup(self.screen_shot) def screen_shot(self):
log.info("yyyyyyyyyyyyyy")
log.info("sereen_shot") def qqq(self):
log.info("xxxxxxxxxxxqqqq")
assert 4==5 #def teardown_method(self, method):
def tearDown(self):
log.info("ffjiafuiodafdfj___teardown") @pytest.mark.slow
def test_7(self):
import time
time.sleep(10)
log.info('- test_7()') @pytest.mark.qq
def test_4(self):
import pdb;pdb.set_trace()
self.result=self.addCleanup(self.qqq)
log.info('- test_4()') def test_5(self):
log.info('- test_4()')
assert 4==5
如果没有加日志实时输出会是怎么样的,如下:

可以看出,日志在过程中没有实时输出,在实际跑项目录,这个有点不太好看。
解决:
看看pytest是怎么解决的呢。
首先pytest是从pytest.ini中读取log_cli配置的,默认是关闭的。如上图中显示,我们的pytest.ini文件是空的
再看看pytest -h文件:
关于log的help有以下:
--no-print-logs disable printing caught logs on failed tests.
--log-level=LOG_LEVEL
logging level used by the logging module
--log-format=LOG_FORMAT
log format as used by the logging module.
--log-date-format=LOG_DATE_FORMAT
log date format as used by the logging module.
--log-cli-level=LOG_CLI_LEVEL
cli logging level.
--log-cli-format=LOG_CLI_FORMAT
log format as used by the logging module.
--log-cli-date-format=LOG_CLI_DATE_FORMAT
log date format as used by the logging module.
--log-file=LOG_FILE path to a file when logging will be written to.
--log-file-level=LOG_FILE_LEVEL
log file logging level.
--log-file-format=LOG_FILE_FORMAT
log format as used by the logging module.
--log-file-date-format=LOG_FILE_DATE_FORMAT
log date format as used by the logging module.
然后你还会发现一行:
[pytest] ini-options in the first pytest.ini|tox.ini|setup.cfg file found:
所以,有两种方法解决
1) 在当前文件夹下写pytest.ini或tox.ini或setup.cfg文件,然后将日志相关写在里面,如下:
[pytest]
log_cli = 1
log_cli_level = INFO
log_cli_format = %(asctime)s [%(levelname)8s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format=%Y-%m-%d %H:%M:%S
这时就可以正常打印日志出来。
2) 直接用pytest -o方式重写,这个功能在pytest 3.4之后才实现,如下
pytest pytest_lean2.py -o log_cli=true -o log_cli_level=INFO
结果如下:

update更新下:
实际在项目过程中,实时日志需要时间及文件名还有行号,可在后面加这样的参数:
-vv -o log_cli=true -o log_cli_level=INFO --log-date-format="%Y-%m-%d %H:%M:%S" --log-format="%(filename)s:%(lineno)s %(asctime)s %(levelname)s %(message)s"
结果就会像下面这样

python pytest测试框架介绍五---日志实时输出的更多相关文章
- python pytest测试框架介绍二
在介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了 一.pytest的用例发现规则 pytest可以在不同的函数.包中发现用例,发现的规则如下 文件名以test_开 ...
- python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制
一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...
- python pytest测试框架介绍三
之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...
- python pytest测试框架介绍一
一.安装 pytest不是python默认的package,需要自动手工安装. pytest支持python 2.6--3.5之间的版本,同时可以在unix及windows上安装 安装方式: pip ...
- Pytest测试框架(五):pytest + allure生成测试报告
Allure 是一款轻量级.支持多语言的开源自动化测试报告生成框架,由Java语言开发,可以集成到 Jenkins. pytest 测试框架支持Allure 报告生成. pytest也可以生成juni ...
- 『德不孤』Pytest框架 — 1、Pytest测试框架介绍
目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...
- 【pytest系列】- pytest测试框架介绍与运行
如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言 目前有两种纯测试的测 ...
- python nose测试框架全面介绍七--日志相关
引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的 ...
- python nose测试框架全面介绍十---用例的跳过
又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...
随机推荐
- snmpwalk命令
使用该命令需提前安装好net-snmp*rpm相关包 语法: snmpwalk -v 1或2(代表SNMP版本) -c SNMP读密码 IP地址 OID(对象标示符) (1) -v: 指定snmp的版 ...
- python算法练习
6. 约瑟夫环问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到k的那个人被杀掉:他的下一个人又从1开始报数,数到k的那个人又被杀掉:依此规律重复下去 ...
- python之编程风格
第一:语句和语法 # 表示注释掉的内容 \ 续行 print("yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy\ yyyyyyyyyyyyyyyyyyyyy ...
- 5、python的变量和常量
今天看看python的变量和常量,这是python中最基本的两个概念. 首先先说一下解释器执行Python的过程: python3 C:\test.py 1. 启动python解释器(内存中) ...
- nginx的http_sub_module模块使用之替换字符串
Nginx可以实现很多功能,提供了许多插件,其中一个比较冷门的http_sub_module,是用来替换指定字符串的,它的原理是Nginx解析到文件后,执行这个插件进行拦截后返回. 昨天碰到一个场景, ...
- CentOS 7最小安装之后应该尽快做好的几件事情
1 导言 CentOS的最小系统仅包含内核和必要的工具,派不上多大用处,以后还得安装很多附加软件.为了方便以后的工作,还需要对系统做一些调整和补充. 本文涉及的工作均应以root身份执行 ...
- 2.1 mac下多版本jdk的安装和管理
之前已经安装过jdk8了,安装路径:/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk 现在安装jdk10,下载后,双击dmg文件一直到安装完成,安装 ...
- 单片机成长之路(51基础篇) - 006 在Linux下搭建51单片机的开发烧写环境
在Linux下没有像keli那样好用的IDE来开发51单片机,开发环境只能自己搭建了. 第一步:安装交叉编译工具 a) 安装SDCC sudo apt-get install sdcc b)测试SDC ...
- Unity应用架构设计(1)—— MVVM 模式的设计和实施(Part 2)
MVVM回顾 经过上一篇文章的介绍,相信你对MVVM的设计思想有所了解.MVVM的核心思想就是解耦,View与ViewModel应该感受不到彼此的存在. View只关心怎样渲染,而ViewModel只 ...
- Mathematica绘制曲面交线方法(方法二)
MeshFunction方式 Show[Graphics3D[{Opacity[0.5], Ball[{0, 0, 0}, 2]}], ParametricPlot3D[{4 + (3 + Cos[v ...