[拉勾直推营] pytest插件开发加餐

参考链接

pytest hook: 定制pytest插件必备之pytest hook的执行顺序

pypi 打包: https://packaging.python.org/tutorials/packaging-projects/

课程大纲

  • 插件的加载方式
  • 什么是hook
  • Pytest 有哪些hook函数
  • 如何改写hook函数
  • 实战打包,发布

代码参考

"""
__author__ = 'hogwarts_xixi'
__time__ = '2021/3/1 8:17 下午'
"""
from typing import List

import pytest
import yaml


def pytest_collection_modifyitems(session, config, items:List):
    for item in items:
        # item.name 用例的名字
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        # item.nodeid 用例的路径
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')

        if 'login' in item.nodeid:
            item.add_marker(pytest.mark.login)

    items.reverse()

# 添加一个命令行参数
def pytest_addoption(parser):
    mygroup = parser.getgroup("hogwarts")     #group 将下面所有的 option都展示在这个group下。
    mygroup.addoption("--env",    #注册一个命令行选项
      default='test',      # 参数的默认值
      dest='env',            #  存储的变量
      help='set your run env'    # 帮助提示 参数的描述信息
    )

@pytest.fixture(scope='session')
def cmdoption(request):
    env = request.config.getoption("--env", default='test')
    if env == 'test':
        print("test 环境")
        datapath = "datas/test/datas.yml"
    elif env == 'dev':
        print("开发 环境")
        datapath = "datas/dev/datas.yml"

    with open(datapath) as f:
        datas = yaml.safe_load(f)
    return env,datas

打包

必须要有代码和setup.py 文件

setup.py 是一个构建工具

打包需要两个工具

  • wheel, setuptools

  • setup.py 文件

"""
__author__ = 'hogwarts_xixi'
__time__ = '2021/3/1 8:57 下午'
"""
from setuptools import setup
setup(
    name='pytest_encode',
    url='https://github.com/xxx/pytest-encode',
    version='1.0',
    author="xixi",
    author_email='418974188@qq.com',
    description='set your encoding and logger',
    long_description='Show Chinese for your mark.parametrize(). Define logger variable for getting your log',
    classifiers=[# 分类索引 ,pip 对所属包的分类
        'Framework :: Pytest',
        'Programming Language :: Python',
        'Topic :: Software Development :: Testing',
        'Programming Language :: Python :: 3.8',
    ],
    license='proprietary',
    packages=['pytest_encode'],
    keywords=[
        'pytest', 'py.test', 'pytest_encode',
    ],

    # 需要安装的依赖
    install_requires=[
        'pytest'
    ],
    # 入口模块 或者入口函数
    entry_points={
        'pytest11': [
            'pytest-encode = pytest_encode',
        ]
    },
    zip_safe=False
)

  • 打包命令:
python setup.py sdist bdist_wheel

image
上面的是源码包,下面的是whl包,可以通过pip install 进行安装

发布:

代码参考

https://github.com/ceshiren/HogwartsLG4

课后作业

  • 自己编写一个插件
    • 创建一个日志的实例,定义好日志的格式,在测试用例中添加一些日志输出,输出日志到文件中
    • 设置编码格式
    • 打包
1 个赞

老师,这个没有ppt吗 :pleading_face:

老师,现在这个打包的结构变了,现在的意思是说我还要在example_pkg上面再创建一个src的包?还是目录也行?


这个setup构建脚本中的package_dir中“空的程序包名称表示‘根程序包’, 即项目中包含该程序包的所有Python源文件的src 目录,因此在这种情况下,该目录被指定为根程序包”这句话怎么理解?
packages是怎么理解的?是可以不写的意思吗?

作业
https://github.com/wegiod/hogwarts_lg40053.git

  • packages:如果你要导入你的模拟,需要在这里列出来,像官网说的,可以直接使用find_packages 帮你自动发现这些包和子包
  • src: 就是你的源代码,如何创建,如何命名无所谓的,按你个人习惯就好。