前言

前面一篇已经学会了使用hook函数改变pytest运行的结果,代码写在conftest.py文件,实际上就是本地的插件了。

当有一天你公司的小伙伴觉得你写的还不错,或者更多的小伙伴想要你这个功能,于是你就想着放到github上,写成一个插件,方便小伙伴使用pip去安装。

插件开发

先新建一个工程,工程名称就是插件名称,一般以pytest-开头命名,目录结构如下

  • setup.py 在安装python的相关模块和库时,我们一般使用pip install 模块名或者python setup.py install,前者是在线安装,会安装该包的相关依赖包;

    后者是下载源码包然后在本地安装,不会安装该包的相关依赖包

  • README.rst README 文档,告诉用户如何使用你的插件,具体能实现什么功能

  • pytest_change_report.py 也就是之前conftest.py里面写的hook函数实现的功能

  • tests 用于测试本插件的相关功能,属于自测的内容

  • tests/conftest.py 开启需要的插件pytester

  • tests/test_change_report.py 测试插件的代码

setup.py

在安装python的相关模块和库时,我们一般使用pip install 模块名或者python setup.py install,前者是在线安装,会安装该包的相关依赖包;

后者是下载源码包然后在本地安装,不会安装该包的相关依赖包.

setup.py 描述安装包相关的信息

from setuptools import setup

"""The setup script.
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
""" setup(
name='pytest-change-report',
url='https://github.com/yoyoketang/pytest-change-report',
version='1.0',
author="yoyo",
author_email='283340479@qq.com',
description='turn . into √,turn F into x',
long_description='print result on terminal turn . into √,turn F into x using hook',
classifiers=[
'Framework :: Pytest',
'Programming Language :: Python',
'Topic :: Software Development :: Testing',
'Programming Language :: Python :: 3.6',
],
license='proprietary',
py_modules=['pytest_change_report'],
keywords=[
'pytest', 'py.test', 'pytest-change-report',
], install_requires=[
'pytest'
],
entry_points={
'pytest11': [
'change-report = pytest_change_report',
]
}
)

pytest_change_report.py

插件的核心功能,也就是之前在conftest.py用hook函数实现的功能。

我们现在实现的功能:

1.把测试的结果.改成√,F改成x

2.命令行加个--change参数开关,默认不开启,当加上参数`--change on·的时候才生效

import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/ def pytest_addoption(parser):
parser.addoption(
"--change",
action="store",
default="off",
help="'Default 'off' for change, option: on or off"
) def pytest_report_teststatus(report, config):
'''turn . into √,turn F into x, turn E into 0'''
if config.getoption("--change") == "on":
if report.when == 'call' and report.failed:
return (report.outcome, 'x', 'failed')
if report.when == 'call' and report.passed:
return (report.outcome, '√', 'passed')

测试插件

当插件功能实现完成后,需要在tests目录测试自己写的插件

tests/conftest.py 文件开启pytester,专门用于测试插件的

'''pytester is needed for testing plgugins.'''

pytest_plugins = ['pytester']

tests/test_change_report.py 文件写测试用例

import pytest
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/ def test_raw_report(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest() # check that 1 test passed, 1 test failed.
result.assert_outcomes(passed=1, failed=1)
result.stdout.fnmatch_lines(["*.F*", ]) def test_change_on_report(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest("--change", "on") # check that 1 test passed, 1 test failed.
result.stdout.fnmatch_lines(['*√x*', ]) def test_change_off_report(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest("--change", "off") # check that 1 test passed, 1 test failed.
result.stdout.fnmatch_lines(['*.F*', ]) def test_change_default_report(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest("--change") # check stderr pytest: error: argument --change: expected one argument
result.stderr.fnmatch_lines(['*argument --change: expected one argument*', ]) def test_verbose_report(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest("-v") # check that 1 test passed, 1 test failed.
result.stdout.fnmatch_lines(['*::test_01 PASSED*', '*::test_02 FAILED*']) def test_change_verbose_report(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b def test_02():
a = "hello"
b = "hello world"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest("--change", "on", "-v") # check that 1 test passed, 1 test failed.
result.stdout.fnmatch_lines(['*::test_01 passed*', '*::test_02 failed*']) def test_help(testdir):
"""Make sure that our plugin works."""
# create a temporary pytest test file
testdir.makepyfile(
"""
def test_01():
a = "hello"
b = "hello"
assert a == b
"""
) # run all tests with pytest
result = testdir.runpytest("--help") # check --help
result.stdout.fnmatch_lines(["*--change=*", ])

本地安装插件

cd到插件的项目目录,使用pip安装

pip install .

>pip install .
Processing d:\soft\pytest-change-report
Using legacy setup.py install for pytest-change-report, since package 'wheel' is not installed.
Installing collected packages: pytest-change-report
Attempting uninstall: pytest-change-report
Found existing installation: pytest-change-report 1.0
Uninstalling pytest-change-report-1.0:
Successfully uninstalled pytest-change-report-1.0
Running setup.py install for pytest-change-report ... done
Successfully installed pytest-change-report-1.0

安装完成后,在cmd输入 pip show pytest-change-report就可以看到前面setup.py里面的描述内容

>pip show pytest-change-report
Name: pytest-change-report
Version: 1.0
Summary: turn . into √,turn F into x
Home-page: https://github.com/yoyoketang/pytest-change-report
Author: yoyo
Author-email: 283340479@qq.com
License: proprietary
Location: e:\python36\lib\site-packages\pytest_change_report-1.0-py3.6.egg
Requires: pytest
Required-by:

安装完成后,输入pytest测试tests/test_change_report.py

>pytest
============================= test session starts ============================= collected 7 items tests\test_change_report.py ....... [100%] ========================== 7 passed in 0.89 seconds ===========================

测试文件里面的用例全部通过

README.rst

最后需要写个 README.rst 使用教程文档,这样你写的插件就能被其它小伙伴学习和使用了。

==============
pytest-change-report: pytest plugin
============== **This pytest plugin turn . into √,turn F into x** Usage
===== 从github源码安装 pip install git+https://github.com/yoyoketang/pytest-change-report.git 命令行运行示例 pytest --change on demo
==== 先写pytest用例test_demo.py def test_01():
a = "hello"
b = "hello"
assert a == b def test_02(login):
a = "hello"
b = "hello world"
assert a == b 命令行执行pytest, 默认不会改变之前的报告内容 >pytest test_demo.py
============================= test session starts =============================
collected 2 items test_demo.py .F [100%] ================================== FAILURES ===================================
___________________________________ test_02 ___________________________________ def test_02():
a = "hello"
b = "hello world"
> assert a == b
E AssertionError: assert 'hello' == 'hello world'
E - hello
E + hello world test_demo.py:10: AssertionError
===================== 1 failed, 1 passed in 0.11 seconds ====================== 加上 --change on 参数后运行 >pytest test_demo.py --change on
============================= test session starts =============================
collected 2 items test_demo.py √x [100%] ================================== FAILURES ===================================
___________________________________ test_02 ___________________________________ def test_02():
a = "hello"
b = "hello world"
> assert a == b
E AssertionError: assert 'hello' == 'hello world'
E - hello
E + hello world test_demo.py:10: AssertionError
===================== 1 failed, 1 passed in 0.08 seconds ====================== pytest.ini
========== 可以添加到pytest.ini配置文件,这样默认就会带上--change参数 [pytest]
--change = on # 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/

提交github

最后把插件代码提交到github仓库,方便下载https://github.com/yoyoketang/pytest-change-report

pytest文档55-plugins插件开发的更多相关文章

  1. pytest文档7-pytest-html生成html报告

    前言 pytest-HTML是一个插件,pytest用于生成测试结果的HTML报告.兼容Python 2.7,3.6 pytest-html 1.github上源码地址[https://github. ...

  2. pytest文档3-pycharm运行pytest

    前言 上一篇pytest文档2-用例运行规则已经介绍了如何在cmd执行pytest用例,平常我们写代码在pycharm比较多 写完用例之后,需要调试看看,是不是能正常运行,如果每次跑去cmd执行,太麻 ...

  3. pytest文档19-doctest测试框架

    前言 doctest从字面意思上看,那就是文档测试.doctest是python里面自带的一个模块,它实际上是单元测试的一种. 官方解释:doctest 模块会搜索那些看起来像交互式会话的 Pytho ...

  4. pytest文档46-关于https请求警告问题(InsecureRequestWarning: Unverified HTTPS request is being made)

    前言 使用 pytest 执行 https 请求用例的时候,控制台会出现警告:InsecureRequestWarning: Unverified HTTPS request is being mad ...

  5. pytest文档43-元数据使用(pytest-metadata)

    前言 什么是元数据?元数据是关于数据的描述,存储着关于数据的信息,为人们更方便地检索信息提供了帮助. pytest 框架里面的元数据可以使用 pytest-metadata 插件实现.文档地址http ...

  6. pytest文档1-环境准备与入门

    前言 首先说下为什么要学pytest,在此之前相信大家已经掌握了python里面的unittest单元测试框架,那再学一个框架肯定是需要学习时间成本的. 刚开始我的内心是拒绝的,我想我用unittes ...

  7. pytest文档56-插件打包上传到 pypi 库

    前言 pytest 的插件完成之后,可以上传到 github,方便其他小伙伴通过 pip 源码安装.如果我们想通过 pip install packages 这种方式安装的话,需上传到 pypi 仓库 ...

  8. pytest文档51-内置fixture之cache使用

    前言 pytest 运行完用例之后会生成一个 .pytest_cache 的缓存文件夹,用于记录用例的ids和上一次失败的用例. 方便我们在运行用例的时候加上--lf 和 --ff 参数,快速运行上一 ...

  9. pytest文档21-pytest-html报告优化(nodeid中文显示[\u6350\u52a9\u6211\u4eec]问题解决)

    前言 pytest-html报告中当用到参数化时候,获取用例的nodeid里面有中文时候,会显示[\u6350\u52a9\u6211\u4eec]这种编码(再次声明,这个不叫乱码,这是unicode ...

随机推荐

  1. Linux下mysql安装记录

    1.MySQL下载路径:https://dev.mysql.com/downloads/ Linux下的安装步骤:http://www.runoob.com/linux/mysql-install-s ...

  2. 腾讯云 云开发 部署 Blazor网站

    Blazor 应用程序除了在 Github Pages/Gitee Pages等静态资源部署以外,现在你有了一个新的选择,那就是使用云开发静态网站功能来部署啦! 系统依赖 在进行后续的内容前,请先确保 ...

  3. matplotlib | Python强大的作图工具,让你从此驾驭图表

    今天是数据处理专题的第9篇文章,在之前的8篇文章当中我们已经介绍完了pandas这个库的一些基本用法,我们先把一些冷门的高级用法放一放,先来给大家介绍一下另外一个很有用的数据分析库--matplotl ...

  4. [LeetCode]67. 二进制求和(字符串)(数学)

    题目 给你两个二进制字符串,返回它们的和(用二进制表示). 输入为 非空 字符串且只包含数字 1 和 0. 题解 两个字符串从低位开始加,前面位不够补0.维护进位,最后加上最后一个进位,最后反转结果字 ...

  5. 1000000 / 60S 的 RocketMQ 不停机,扩容,平滑升级!

    一.背景 1.各业务系统持续迭代过程中,JDK.SpringBoot.RocketMQ Client 等框架也进行了升级,高版本的 RocketMQ Client 发送的消息到低版本中,在控制台中午无 ...

  6. 堆的源码与应用:堆排序、优先队列、TopK问题

    1.堆 堆(Heap))是一种重要的数据结构,是实现优先队列(Priority Queues)首选的数据结构.由于堆有很多种变体,包括二项式堆.斐波那契堆等,但是这里只考虑最常见的就是二叉堆(以下简称 ...

  7. Echarts山东省地图两级钻取、返回及济南合并莱芜地图

    Echarts3.0+jQuery3.3.1 山东省地图中济南市需要注意下,莱芜市已经和济南市合并,地图数据来源于地图选择器,获取山东省地图信息及各地市地图信息(JSON格式) //山东地图(第一级地 ...

  8. 【测试基础第六篇】bug定义及生命周期

    bug定义 狭义:软件程序的漏洞或缺陷 广义:测试工程师或用户所发现和提出的软件可改进的细节(增强型.建议性)或需求文档存在差异的功能实现 职责:发现bug,提给开发,让其修改 bug类型--了解 代 ...

  9. 手撸Mysql原生语句--多表

    在开始之前,我们需要建立表,做建表和数据的准备的工作. 1.建表 create table department( id int, name varchar(20) ); create table e ...

  10. Spring ioc 源码分析之--beanFactory.registerResolvableDependency()方法

    背景: 在spring ioc refresh()方法中,看到了如下逻辑: 要理解该逻辑的原理,先看场景:有个接口InterFaceA,它有2个实现类A1和A2 那么,如果一个类C 依赖了InterF ...