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

结果如下:

原文地址:https://www.cnblogs.com/landhu/p/9910460.html

时间: 2024-08-29 01:08:09

python pytest测试框架介绍五---日志实时输出的相关文章

python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这里介绍pytest生成的报告怎么带有截图,这在web自动化测试非常有用. 需求是测试用例错误就截图,方法如下: 我们要新建一个关于截图的插件文件conftest.py,注意,文件名不能变,因为pytest-html会自动找这个自己写的插件,内容如下: from selenium import web

python pytest测试框架介绍三

之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_function import pytest @pytest.fixture(scope='module') def resource_a_setup(request): print('\nresources_a_setup()') def resource_a_teardown(): print('

pytest测试框架介绍(3)

12.fixture带参数传递 场景:测试离不开数据,为了数据灵活,一般数据都是通过参数传的 解决:fixture通过固定参数request传递: 步骤:在fixture中增加@pytest.fixture(params=[1,2,3,'linda'])在方法参数写request 如下图,运行结果: 参数传入的可以是列表是元祖 如下图,eval将字符串str当成有效的表达式来求值,并返回结果: 当我们测试登录或者搜索这种同样的场景需要不同的数据时,就可以使用这种参数组合的方法: 运行结果如下:

python nose测试框架全面介绍七--日志相关

引: 之前使用nose框架时,一直使用--logging-config的log文件来生成日志,具体的log配置可见之前python nose测试框架全面介绍四. 但使用一段时间后,发出一个问题,生成的报告只有错误提示,没有日志,查看nose的官网,nose默认支持将日志显示的,如下: 脚本如下: #coding:utf-8 ''' Created on 2016年6月22日 @author: huzq ''' import logging from test_case import new fr

python nose测试框架全面介绍十---用例的跳过

又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的SkipTest典型应用  应用一: ''' @auth:hu ''' from nose.plugins.skip import SkipTest @attr(mode=1) def test_learn_1(): raise SkipTest 但这种SkipTest在实际的日志中没有显示Skip关

python nose测试框架全面介绍六--框架函数别名

之前python nose测试框架全面介绍二中介绍了nose框架的基本构成,但在实际应该中我们也会到setup_function等一系列的名字,查看管网后,我们罗列下nose框架中函数的别名 1.package中写在__init__.py中的函数 setup_package 用setup, setUp, or setUpPackage也可以 teardown_package 用teardown, tearDown, or tearDownPackage也可以 2.函数形式组成的用例 setup_

python nose测试框架全面介绍一

一.简介      nose 是python自带框架unttest的扩展,使测试更简单高效:nose是一个开源的项目,可以在官网上下载源码 1.快速安装 有以下几中安装方式: easy_install nose pip install nose 对于python怎么安装easy_install或pip工具,这里不介绍,网上很多教程. 如果你没有安装easy_install或pip工具,你可以去官网上下载安装包,然后解压,并cd进放解压的目录,然后输入以下代码: python setup.py i

(二) 关于配置travis-ci持续集成python pytest测试的相关记录

接上篇 上篇只是非常官方的描述了一下travis-ci是包括了些什么部分会如何工作但是并没有深入介绍也没有写demo. 这里先贴上一个我已经测试好了的python_travis-ci的环境 https://github.com/piperck/flask_pytest_demo#flask_pytest_demo 只要clone这个仓库,并且发pr上来就可以发现,ci就会开始集成,测试和集成内容都由配置脚本配置完成,在这个demo里现在.我只是配置了几个最简单的测试脚本,并且把他们都跑通了. 从

Python之Web框架介绍

所有的语言Web框架本质其实就是起一个socket服务端,监听一个端口,然后运行起来 Web框架包含两部分,一部分是socket,另外一部分是业务的逻辑处理,根据请求的不同做不同的处理 Python的Web框架分成了两类, 即包含socket也包含业务逻辑处理的(tornado) 不包含socket(框架本身通过第三方模块实现socket)只包含业务逻辑处理(django,Flask) WSGI的全称是Web Server Gateway Interface,翻译过来就是Web服务器网关接口.具