接口自动化(1)

项目框架和自动化报告

pytest的pytest.ini配置文件是一个固定配置文件,pytest.ini用于读取整个项目的配置信息,pytest按照此配置文件指定方式运行。

  • 配置文件放在项目的根目录下
  • 文件名固定不可修改
  • 文件中不得出现中文以及注释
  • 文件说明如下:
  • 4.1 addopts 声明运行方式以及生成报告如下
  • 4.2 testpaths 指定用例文件名字
  • 4.3 python_files 指定用例所在文件
  • 4.4 python_classes 指定用例类所在位置
  • 4.5 python_functions 指定用例所在的位置

例子:

[pytest]
addopts = -s -v --html=../report/report.html
testpaths = testcase
python_files = test_*.py
python_classes = Test*
python_functions = test*

logging 库

Python自带的日志库,需要掌握基本的API。

工具类:封装API。

  • 默认情况下,会打印WARNING级别的日志
  • DEBUG:详细信息,调试信息。
  • INFO:确认一切按预期运行。
  • WARNING:表明发生了一些意外,或者不久的将来会发生问题(如‘磁盘满了’)。软件还是在正常工作。—-默认级别
  • ERROR:由于更严重的问题,软件已不能执行一些功能了。
  • CRITICAL:严重错误,表明软件已不能继续运行了。
  • 注意:
  • 日志等级 DEBUG详细<INFO<WARNING<ERROR<CRITICAL简略
  • 只有级别高于或者等于日志级别的日志才会被输出,低于该等级的日志将会被丢弃
对象名称对象API
logging1、五个输出日志小写,设置级别大写:basicconfig(level=大写,format=设置格式)
日志系统
1
bosslogger=getLogger(“name”)
setLevel(ERROR)
—set the lowest level
2
streamhandler(处理流)
2.1setLevel(debug)结果为error以上
—set the lowest level
2.2setFormat
3
fileHandler
r w(覆写) a(append追加模式)
3.1
fp1=FileHandler(‘fileadress’,mode=’a’,encoding=’utf-8′)
3.2
setLevel()
3.3
setFormat()
4
外部被调用的对象是boss,再用boss调用其他
5
boss.addHandler()

logging.debug(“我是代码调试信息11~”)
logging.info(“我是系统正常输出信息11”)
logging.warning(“—-我是警告信息11,比如内存不够~—–“)
logging.debug(“我是代码调试信息22~”)
logging.error(“—–我是一个小bug11!!——“)
logging.info(“我是系统正常输出信息22”)
logging.warning(“—-我是警告信息22,比如内存不够~—–“)
logging.error(“—–我是一个小bug22!!——“)
logging.warning(“—-我是警告信息33,比如内存不够~—–“)
logging.critical(“—-我是bug 严重事故~导致系统崩溃—–“)

日志多终端分发:

import logging


class logutil:
    def __init__(self):#coustructing a class as an object
        self.logger=logging.getLogger('ikun')#convert it an contribute variable
        self.logger.setLevel(logging.INFO)
        sh=logging.StreamHandler()
        sh.setLevel(logging.ERROR)
        sh.setFormatter(logging.Formatter('%(filename)s-%(asctime)s-[hanghao:%(lineno)d] - %(levelname)s: %(message)s'))
        fh=logging.FileHandler('1.log',mode='a',encoding='utf-8')
        fh.setFormatter(logging.Formatter('%(filename)s-%(asctime)s-[hanghao:%(lineno)d] - %(levelname)s: %(message)s'))
        fh.setLevel(logging.WARNING)
        self.logger.addHandler(sh)
        self.logger.addHandler(fh)
    def log(self):
        return self.logger
ll = logutil().log()

logutil()创建对象,.log()调用了方法,赋值给了ll

from logutil import ll

if __name__ == '__main__':
    # 自测
    # 日志系统 默认的级别  是WARNING 级别
    ll.warning("我是代码调试信息11~")

封装为工具类使用:

import pytest

from jwdev.jwdev1 import Count
from utils.jw2 import logger


class TestAdd():
    def test_11(self):
        logger.info("主人,我是第一条用例~")
        # 1设计用例~===》预期结果

        try:
            a = 3
            b = 5
            yuqi = 81
            # 2 调用开发写的代码===》实际结果~
            c1 = Count(a, b)
            shiji = c1.add()
            assert yuqi == shiji
        except: # 断言失败 也会except 分支~
            logger.error("主人,我是第一条用例有bug~~")


    def test_22(self):
        logger.info("主人,我是第二条用例~")

        assert  200==Count(100,100).add()

    def test_33(self):
        logger.info("主人,我是第三条用例~")
        assert  202==Count(101,101).add()

if __name__ == '__main__':
    pytest.main(['-v', '-s', 'jwtest1.py']) # 不要忘了改成你的文件名~

logger对象改为ll。

API:

对象名字对象API
OS__file__ 魔法变量,得到当前文件名字路径
os.path.dirname(__file__)当前目录(文件父目录),套一层是祖父目录
os.sep路径分割符号,会自动的获取当前的系统使用的分隔符号
os.path.join(目录,文件名字)
把文件拼接到目录上
timesleep强制等待
localtime()打印当前时间(英文习惯)
strftime(“%Y_%m_%d %H:%M:%S”,时间)时间指time.localtime()方法

例子:print(time.strftime(“%Y_%m_%d”,time.localtime()))

os方法确定文件目录:

import logging
import os


class logutil:
    def __init__(self):#coustructing a class as an object
        self.logger=logging.getLogger('ikun')#convert it an contribute variable
        self.logger.setLevel(logging.INFO)
        sh=logging.StreamHandler()
        sh.setLevel(logging.ERROR)
        sh.setFormatter(logging.Formatter('%(filename)s-%(asctime)s-[hanghao:%(lineno)d] - %(levelname)s: %(message)s'))
        project_path=os.path.dirname(os.path.dirname(__file__))
        log_path=project_path+os.sep+'log'
        log_path_name=os.path.join(log_path,'jw2.log')
        fh=logging.FileHandler(log_path_name,mode='a',encoding='utf-8')
        fh.setFormatter(logging.Formatter('%(filename)s-%(asctime)s-[hanghao:%(lineno)d] - %(levelname)s: %(message)s'))
        fh.setLevel(logging.WARNING)
        self.logger.addHandler(sh)
        self.logger.addHandler(fh)
    def log(self):
        return self.logger
ll = logutil().log()

自动创建log文件夹:

if not os.path.exists(log_path):
os.makedirs(log_path) # 自动创建 log 文件夹

页: 1 2 3


评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注