__init__.py 2.25 KB
Newer Older
zzy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
# -*-coding:utf-8 -*- #
# ---------------------------------------------------------------------------
# ProjectName:   test62
# FileName:      __init__.py
# Author:       lao_zhao
# Datetime:     2024/9/4 14:01
# Description:
# 
# ---------------------------------------------------------------------------
import logging
import os
import time


"""
    pytest输出报告的插件:pytest-html. pip install pytest-html.
        命令:pytest 用例层 -vv --html=报告存名称 --self-contained-html
    报告输出是可能有用例会断言失败,断言失败的用例可以进行失败重跑。失败重跑的插件为:pytest-rerunfailures.  pip install pytest-rerunfailures
        pytest 用例层 -vv --html=报告存名称 --self-contained-html --reruns 失败重跑的次数 --reruns-delay 每次失败重跑的间隔时间

"""
"""
    allure生成报告:
        pip install allure-pytest
        
        1: 生成测试结果的json文件。 pytest 用例层 --alluredir=存放json文件的目录 --clean-alluredir
        2: 根据第一步的json文件生成报告
            allure serve 第一步生成json文件的存放目录
            allure generate 第一步生成json文件的存放目录 -o 静态报告保存的路径
            
        3: 打开allure生成的静态报告:allure open 静态报告保存的路径

"""


def log(name="张三"):
    # 创建Logger对象
    logger = logging.getLogger(name)
    # 设置日志的级别
    logger.level = logging.INFO
    # 2024-09-04-14-09-10.log
    log_name = time.strftime("%Y-%m-%d-%H-%M-%S", time.localtime())+".log"
    log_dir = os.path.join(os.path.join(os.path.dirname(__file__), "report"), "log")
    log_path = os.path.join(log_dir, log_name)
    # 设置写入日志的文件
    handle = logging.FileHandler(log_path, mode="a", encoding="utf-8")
    # 设置日志的显示格式
    formatter = logging.Formatter('%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s -- %(name)s')
    # 设置日志文件中的日志格式
    handle.setFormatter(formatter)
    # 给Logger对象添加文件
    logger.addHandler(handle)
    # 将Logger对象返回
    return logger


# 先执行函数,获取函数的返回值,以后之间使用函数的返回值写入日志
log = log()