pytest基础

pytest

python 命名规则:Google 开源项目风格指南
在这里插入图片描述
pytest 命名规则:文件名、类、方法、test_开头(注意:测试类中不要添加_init构造函数)

用例名称、用例步骤、用例断言
在这里插入图片描述
assert 表达式
assert 表达式,描述

类型规则
setup_module/teardown_module全局模块级
setup_class/teardown_class类级,只在类前后运行一次
setup_function/teardown_function函数级,在类外面
setup_method/teardown_method方法级,在类中的每个方法执行的前后
setup/teardown在类中,运行在调用方法的前后

参数化设计方法就是将模型中的定量信息变量化,使之成为任意调整的参数。对于变量化参数赋予不同的数值,就得到不同大小和形状的零件模型。

import pytest

@pytest.mark.parametrize("name,age",[("Tom",5),("Lily",6),("Alice",3)],ids=["kid1","kid2","kid3"])
def test_demo(name,age):
    print(f"姓名是{name},年龄是{age}")
    assert True

笛卡尔积

import pytest

@pytest.mark.parametrize("name",["Tom","Lily","Alice"])
@pytest.mark.parametrize("age",[2,3,4,5,6])
def test_demo(name,age):
    print(f"姓名是{name},年龄是{age}")
    assert True

给用例打标签
@pytest.mark.标签名
执行时加-m
pytest test_demo.py -m=webtest
pytest test_demo.py -m apptest
pytest test_demo.py -m “not ios”
pytest.ini 里面加
markers = 标签名
标签名
标签名

  • @pytest.mark.skip(reason=“跳过的原因”) 始终跳过该用例
  • @pytest.mark.skipif (condition判断条件,reason=“跳过的原因”)遇到特定情况跳过该条测试用例
  • 在代码中添加pytest.skip(reason)跳过后续代码的执行(代码中进行判断,未符合条件的情况就添加跳过代码)
  • @pytest.mark.xfail(reason=“失败的原因”) 遇到特定情况产生一个“期望失败”的输出
  • 在代码中添加pytest.xfail(reason=“失败的原因”)跳过后续代码的执行
    在这里插入图片描述
    –lf last failed只重新运行上次执行失败的测试用例,如果上次没有失败,所有用例都执行
    –ff fail first 先运行上次执行失败的测试用例,然后再运行其余的测试用例
    pytest --help
    pytest -x 用例一旦失败就立即停止执行(回归测试之前,先进行冒烟测试,即只测核心功能)(先保证核心没问题之后再进行场景、特殊功能的测试)
    pytest --maxfail=num 用例失败达到num停止执行
    pytest -m 标记用例
    pyetst -k 执行包括某个关键字的测试用例pyetst -k “search” 双引号
    pytest -v 打印详细日志
    pytest -s 打印输出日志(pytest -vs 一般一块使用)
    pytest --collect-only 收集测试用例不运行,pytest自动导入功能

python代码执行pytest(持续集成时会用到)

  • main函数
  • 在命令行 python -m pytest ,pytest后面可以跟pytest的各种用法
    在这里插入图片描述
    在这里插入图片描述
    pytest.raise()
    可以捕获特定的异常;获取捕获的异常细节(异常类型、信息);发生异常,后面的代码将不会继续执行。
def test_demo():
    with pytest.raises((ValueError,ZeroDivisionError)) as exc_info:
        raise ValueError("error")
    assert exc_info.type is ValueError
    assert exc_info.value.args[0] == "error"

测试步骤的数据驱动
测试数据的数据驱动
配置的数据驱动

-
 test: 127.0.0.1
@pytest.mark.parametrize("env",yaml.safe_load(open("./env.yml")))
def test_demo(env):
    if "test" in env:
        print("测试环境")
        print(f"测试环境的ip是{env['test']}")
    if "dev" in env:
        print("开发环境")

openpyxl官网

import openpyxl

book = openpyxl.load_workbook("./demo.xlsx")
sheet = book.active
cell_a1 = sheet['A1'].value
cell_b2 = sheet.cell(column=2,row=2).value
cells = sheet["A2":"B21"]
datas=[[value.value for value in rows] for rows in cells]

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
fixture conftest.py scope yield
在这里插入图片描述
在这里插入图片描述

import pytest

@pytest.fixture()
def login():
    print("login")
    token = "423sg466ftyr574575tyr4yyt"
    yield token
    print("out")

def test_demo(login):
    print("test_demo")
    print(f"token:{login}")
    assert True

def test_demo2():
    print("test_demo2")
    assert True

conftest.py 注意拼写!
在这里插入图片描述
在这里插入图片描述
pytest.ini(项目的根目录下)
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
conftest.py

@pytest.fixture(scope="session", autouse=True)
def manage_logs(request):
    """Set log file name same as test name"""
    now = time.strftime("%Y-%m-%d_%H-%M-%S")
    log_name = 'log/' + now + '.logs'
    request.config.pluginmanager.get_plugin("logging-plugin").set_log_path(log_name)

pytest.ini

[pytest]
log_cli = true
log_cli_level = info
addopts = --capture=no
log_cli_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
log_cli_date_format = %Y-%m-%d %H:%M:%S
;log_file = ./log/test.log
log_file_level = info
log_file_format = %(asctime)s [%(levelname)s] %(message)s (%(filename)s:%(lineno)s)
log_file_date_format = %Y-%m-%d %H:%M:%S

allure

官方介绍

  • Java (1.8)
  • allure(2.13.5),需要配置环境变量 3386
    下载地址:https://repo1.maven.org/maven2/io/qameta/allure/allure-commandline/
    官方学习地址
    pytest xx.py --alluredir ./result
    allure serve ./result
    pytest xx.py --alluredir ./result --clean-alluredir
    在这里插入图片描述
    用例代码中添加
with allure.step("步骤1:打开应用"):
	pass
allure.attach('A text attacment in module scope fixture', 'blah blah blah',allure.attachment_type.TEXT)
allure.attach.file('./data/totally_open_source_kitten.png', attachment_type=allure.attachment_type.PNG)
allure.attach('<head></head><body> a page </body>', 'Attach with HTML type', allure.attachment_type.HTML)

BLOCKER阻塞缺陷,功能未实现无法下一步
CRITICAL严重缺陷,功能点缺失
NORMAL一般缺陷,格式错误或边界
MINOR次要缺陷,界面错误与UI的需求不符
TRIVIAL轻微缺陷,必须项无提示或提示不规范
@allure.severity(allure.severity_level.NORMAL)
运行级别为normal,critical的测试用例
pytest -vs 文件名 --allure-severities normal,critical --alluredir=./result

生成报告:

  • 在线报告allure serve ./result
  • 生成最终的报告allure generate -c -o ./allure-report ./result,然后打开报告allure open ./allure-report
  • 多个测试结果,在线报告allure serve ./result1 ./result2 ./result3
  • 多个测试结果,生成最终的报告allure generate -c -o ./allure-report ./result1 ./result2 ./result3,然后打开报告allure open ./allure-report
    在这里插入图片描述
    在这里插入图片描述

pytest 插件

外部插件:需要 pip install 安装
本地插件: conftest.py存放
内置插件:代码内部的_pytest 目录加载 hookspec.py

pytest 插件 pytest-ordering 控制用例顺序 @pytets.mark.run(order=1)
pytest 插件 pytest-xdist 分布式并发执行测试用例

  • 场景1:1个用例需要执行1分钟,有1000条用例,那就需要执行1000分钟,如果10个并发一起只需要100分钟。
  • 场景2:当数据同时进行修改操作时,有可能会出现问题,需要模拟该场景,即需要进行多用户并发。
  • pytest -n auto

hook 函数 钩子函数:是一个函数,在系统消息触发时被系统调用,自动触发机制(自动执行)。
pytest有非常多的勾子函数,使用时直接编写函数体, hook 函数的名称是确定的。
在这里插入图片描述
pytest大体简化的执行顺序

  • 开始执行
  • 收集测试用例
  • 执行setup
  • 执行测试用例
  • 执行teardown
  • 获取测试结果
  • 结束执行

pytest实际的执行顺序
在这里插入图片描述
root
└── pytest_cmdline_main
├── pytest_plugin_registered
├── pytest_configure
│ └── pytest_plugin_registered
├── pytest_sessionstart
│ ├── pytest_plugin_registered
│ └── pytest_report_header
├── pytest_collection
│ ├── pytest_collectstart
│ ├── pytest_make_collect_report
│ │ ├── pytest_collect_file
│ │ │ └── pytest_pycollect_makemodule
│ │ └── pytest_pycollect_makeitem
│ │ └── pytest_generate_tests
│ │ └── pytest_make_parametrize_id
│ ├── pytest_collectreport
│ ├── pytest_itemcollected
│ ├── pytest_collection_modifyitems
│ └── pytest_collection_finish
│ └── pytest_report_collectionfinish
├── pytest_runtestloop
│ └── pytest_runtest_protocol
│ ├── pytest_runtest_logstart
│ ├── pytest_runtest_setup
│ │ └── pytest_fixture_setup
│ ├── pytest_runtest_makereport
│ ├── pytest_runtest_logreport
│ │ └── pytest_report_teststatus
│ ├── pytest_runtest_call
│ │ └── pytest_pyfunc_call
│ ├── pytest_runtest_teardown
│ │ └── pytest_fixture_post_finalizer
│ └── pytest_runtest_logfinish
├── pytest_sessionfinish
│ └── pytest_terminal_summary
└── pytest_unconfigure

…\Python\Lib\site-packages_pytest\hookspec.py

"""Hook specifications for pytest plugins which are invoked by pytest itself
and by builtin plugins."""
from pathlib import Path
from typing import Any
from typing import Dict
from typing import List
from typing import Mapping
from typing import Optional
from typing import Sequence
from typing import Tuple
from typing import TYPE_CHECKING
from typing import Union

from pluggy import HookspecMarker

from _pytest.deprecated import WARNING_CMDLINE_PREPARSE_HOOK

if TYPE_CHECKING:
    import pdb
    import warnings
    from typing_extensions import Literal

    from _pytest._code.code import ExceptionRepr
    from _pytest.code import ExceptionInfo
    from _pytest.config import Config
    from _pytest.config import ExitCode
    from _pytest.config import PytestPluginManager
    from _pytest.config import _PluggyPlugin
    from _pytest.config.argparsing import Parser
    from _pytest.fixtures import FixtureDef
    from _pytest.fixtures import SubRequest
    from _pytest.main import Session
    from _pytest.nodes import Collector
    from _pytest.nodes import Item
    from _pytest.outcomes import Exit
    from _pytest.python import Class
    from _pytest.python import Function
    from _pytest.python import Metafunc
    from _pytest.python import Module
    from _pytest.reports import CollectReport
    from _pytest.reports import TestReport
    from _pytest.runner import CallInfo
    from _pytest.terminal import TerminalReporter
    from _pytest.compat import LEGACY_PATH


hookspec = HookspecMarker("pytest")

# -------------------------------------------------------------------------
# Initialization hooks called for every plugin
# -------------------------------------------------------------------------

@hookspec(historic=True)
def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
    """Called at plugin registration time to allow adding new hooks via a call to
    ``pluginmanager.add_hookspecs(module_or_class, prefix)``.

    :param pytest.PytestPluginManager pluginmanager: The pytest plugin manager.

    .. note::
        This hook is incompatible with ``hookwrapper=True``.
    """

@hookspec(historic=True)
def pytest_plugin_registered(
    plugin: "_PluggyPlugin", manager: "PytestPluginManager"
) -> None:
    """A new pytest plugin got registered.

    :param plugin: The plugin module or instance.
    :param pytest.PytestPluginManager manager: pytest plugin manager.

    .. note::
        This hook is incompatible with ``hookwrapper=True``.
    """

@hookspec(historic=True)
def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") -> None:
    """Register argparse-style options and ini-style config values,
    called once at the beginning of a test run.添加命令行参数,运行时会先读取命令行参数

    .. note::

        This function should be implemented only in plugins or ``conftest.py``
        files situated at the tests root directory due to how pytest
        :ref:`discovers plugins during startup <pluginorder>`.

    :param pytest.Parser parser:
        To add command line options, call
        :py:func:`parser.addoption(...) <pytest.Parser.addoption>`.
        To add ini-file values call :py:func:`parser.addini(...)
        <pytest.Parser.addini>`.

    :param pytest.PytestPluginManager pluginmanager:
        The pytest plugin manager, which can be used to install :py:func:`hookspec`'s
        or :py:func:`hookimpl`'s and allow one plugin to call another plugin's hooks
        to change how command line options are added.

    Options can later be accessed through the
    :py:class:`config <pytest.Config>` object, respectively:

    - :py:func:`config.getoption(name) <pytest.Config.getoption>` to
      retrieve the value of a command line option.

    - :py:func:`config.getini(name) <pytest.Config.getini>` to retrieve
      a value read from an ini-style file.

    The config object is passed around on many internal objects via the ``.config``
    attribute or can be retrieved as the ``pytestconfig`` fixture.

    .. note::
        This hook is incompatible with ``hookwrapper=True``.
    """

@hookspec(historic=True)
def pytest_configure(config: "Config") -> None:
    """Allow plugins and conftest files to perform initial configuration.

    This hook is called for every plugin and initial conftest file
    after command line options have been parsed.

    After that, the hook is called for other conftest files as they are
    imported.

    .. note::
        This hook is incompatible with ``hookwrapper=True``.

    :param pytest.Config config: The pytest config object.
    """

# -------------------------------------------------------------------------
# Bootstrapping hooks called for plugins registered early enough:
# internal and 3rd party plugins.
# -------------------------------------------------------------------------

@hookspec(firstresult=True)
def pytest_cmdline_parse(
    pluginmanager: "PytestPluginManager", args: List[str]
) -> Optional["Config"]:
    """Return an initialized config object, parsing the specified args.

    Stops at first non-None result, see :ref:`firstresult`.

    .. note::
        This hook will only be called for plugin classes passed to the
        ``plugins`` arg when using `pytest.main`_ to perform an in-process
        test run.

    :param pytest.PytestPluginManager pluginmanager: The pytest plugin manager.
    :param List[str] args: List of arguments passed on the command line.
    """

@hookspec(warn_on_impl=WARNING_CMDLINE_PREPARSE_HOOK)
def pytest_cmdline_preparse(config: "Config", args: List[str]) -> None:
    """(**Deprecated**) modify command line arguments before option parsing.

    This hook is considered deprecated and will be removed in a future pytest version. Consider
    using :hook:`pytest_load_initial_conftests` instead.

    .. note::
        This hook will not be called for ``conftest.py`` files, only for setuptools plugins.

    :param pytest.Config config: The pytest config object.
    :param List[str] args: Arguments passed on the command line.
    """

@hookspec(firstresult=True)
def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]:
    """Called for performing the main command line action. The default
    implementation will invoke the configure hooks and runtest_mainloop.

    Stops at first non-None result, see :ref:`firstresult`.

    :param pytest.Config config: The pytest config object.
    """

def pytest_load_initial_conftests(
    early_config: "Config", parser: "Parser", args: List[str]
) -> None:
    """Called to implement the loading of initial conftest files ahead
    of command line option parsing.

    .. note::
        This hook will not be called for ``conftest.py`` files, only for setuptools plugins.

    :param pytest.Config early_config: The pytest config object.
    :param List[str] args: Arguments passed on the command line.
    :param pytest.Parser parser: To add command line options.
    """

# -------------------------------------------------------------------------
# collection hooks
# -------------------------------------------------------------------------

@hookspec(firstresult=True)
def pytest_collection(session: "Session") -> Optional[object]:
    """Perform the collection phase for the given session.

    Stops at first non-None result, see :ref:`firstresult`.
    The return value is not used, but only stops further processing.

    The default collection phase is this (see individual hooks for full details):

    1. Starting from ``session`` as the initial collector:

      1. ``pytest_collectstart(collector)``
      2. ``report = pytest_make_collect_report(collector)``
      3. ``pytest_exception_interact(collector, call, report)`` if an interactive exception occurred
      4. For each collected node:

        1. If an item, ``pytest_itemcollected(item)``
        2. If a collector, recurse into it.

      5. ``pytest_collectreport(report)``

    2. ``pytest_collection_modifyitems(session, config, items)``

      1. ``pytest_deselected(items)`` for any deselected items (may be called multiple times)

    3. ``pytest_collection_finish(session)``
    4. Set ``session.items`` to the list of collected items
    5. Set ``session.testscollected`` to the number of collected items

    You can implement this hook to only perform some action before collection,
    for example the terminal plugin uses it to start displaying the collection
    counter (and returns `None`).

    :param pytest.Session session: The pytest session object.
    """

def pytest_collection_modifyitems(
    session: "Session", config: "Config", items: List["Item"]
) -> None:
    """Called after collection has been performed. May filter or re-order
    the items in-place.收集测试用例,收集之后,可以改编码、改执行顺序

    :param pytest.Session session: The pytest session object.
    :param pytest.Config config: The pytest config object.
    :param List[pytest.Item] items: List of item objects.
    """

def pytest_collection_finish(session: "Session") -> None:
    """Called after collection has been performed and modified.收集之后的操作

    :param pytest.Session session: The pytest session object.
    """

@hookspec(firstresult=True)
def pytest_ignore_collect(
    collection_path: Path, path: "LEGACY_PATH", config: "Config"
) -> Optional[bool]:
    """Return True to prevent considering this path for collection.

    This hook is consulted for all files and directories prior to calling
    more specific hooks.

    Stops at first non-None result, see :ref:`firstresult`.

    :param pathlib.Path collection_path : The path to analyze.
    :param LEGACY_PATH path: The path to analyze (deprecated).
    :param pytest.Config config: The pytest config object.

    .. versionchanged:: 7.0.0
        The ``collection_path`` parameter was added as a :class:`pathlib.Path`
        equivalent of the ``path`` parameter. The ``path`` parameter
        has been deprecated.
    """

def pytest_collect_file(
    file_path: Path, path: "LEGACY_PATH", parent: "Collector"
) -> "Optional[Collector]":
    """Create a Collector for the given path, or None if not relevant.

    The new node needs to have the specified ``parent`` as a parent.

    :param pathlib.Path file_path: The path to analyze.
    :param LEGACY_PATH path: The path to collect (deprecated).

    .. versionchanged:: 7.0.0
        The ``file_path`` parameter was added as a :class:`pathlib.Path`
        equivalent of the ``path`` parameter. The ``path`` parameter
        has been deprecated.
    """

# logging hooks for collection

def pytest_collectstart(collector: "Collector") -> None:
    """Collector starts collecting."""

def pytest_itemcollected(item: "Item") -> None:
    """We just collected a test item."""

def pytest_collectreport(report: "CollectReport") -> None:
    """Collector finished collecting."""

def pytest_deselected(items: Sequence["Item"]) -> None:
    """Called for deselected test items, e.g. by keyword.

    May be called multiple times.
    """
@hookspec(firstresult=True)
def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectReport]":
    """Perform :func:`collector.collect() <pytest.Collector.collect>` and return
    a :class:`~pytest.CollectReport`.

    Stops at first non-None result, see :ref:`firstresult`.
    """

# -------------------------------------------------------------------------
# Python test function related hooks
# -------------------------------------------------------------------------

@hookspec(firstresult=True)
def pytest_pycollect_makemodule(
    module_path: Path, path: "LEGACY_PATH", parent
) -> Optional["Module"]:
    """Return a Module collector or None for the given path.

    This hook will be called for each matching test module path.
    The pytest_collect_file hook needs to be used if you want to
    create test modules for files that do not match as a test module.

    Stops at first non-None result, see :ref:`firstresult`.

    :param pathlib.Path module_path: The path of the module to collect.
    :param LEGACY_PATH path: The path of the module to collect (deprecated).

    .. versionchanged:: 7.0.0
        The ``module_path`` parameter was added as a :class:`pathlib.Path`
        equivalent of the ``path`` parameter.

        The ``path`` parameter has been deprecated in favor of ``fspath``.
    """

@hookspec(firstresult=True)
def pytest_pycollect_makeitem(
    collector: Union["Module", "Class"], name: str, obj: object
) -> Union[None, "Item", "Collector", List[Union["Item", "Collector"]]]:
    """Return a custom item/collector for a Python object in a module, or None.

    Stops at first non-None result, see :ref:`firstresult`.
    """

@hookspec(firstresult=True)
def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]:
    """Call underlying test function.

    Stops at first non-None result, see :ref:`firstresult`.
    """

def pytest_generate_tests(metafunc: "Metafunc") -> None:
    """Generate (multiple) parametrized calls to a test function."""

@hookspec(firstresult=True)
def pytest_make_parametrize_id(
    config: "Config", val: object, argname: str
) -> Optional[str]:
    """Return a user-friendly string representation of the given ``val``
    that will be used by @pytest.mark.parametrize calls, or None if the hook
    doesn't know about ``val``.

    The parameter name is available as ``argname``, if required.

    Stops at first non-None result, see :ref:`firstresult`.

    :param pytest.Config config: The pytest config object.
    :param val: The parametrized value.
    :param str argname: The automatic parameter name produced by pytest.
    """

# -------------------------------------------------------------------------
# runtest related hooks
# -------------------------------------------------------------------------

@hookspec(firstresult=True)
def pytest_runtestloop(session: "Session") -> Optional[object]:
    """Perform the main runtest loop (after collection finished).

    The default hook implementation performs the runtest protocol for all items
    collected in the session (``session.items``), unless the collection failed
    or the ``collectonly`` pytest option is set.

    If at any point :py:func:`pytest.exit` is called, the loop is
    terminated immediately.

    If at any point ``session.shouldfail`` or ``session.shouldstop`` are set, the
    loop is terminated after the runtest protocol for the current item is finished.

    :param pytest.Session session: The pytest session object.

    Stops at first non-None result, see :ref:`firstresult`.
    The return value is not used, but only stops further processing.
    """

@hookspec(firstresult=True)
def pytest_runtest_protocol(
    item: "Item", nextitem: "Optional[Item]"
) -> Optional[object]:
    """Perform the runtest protocol for a single test item.

    The default runtest protocol is this (see individual hooks for full details):

    - ``pytest_runtest_logstart(nodeid, location)``

    - Setup phase:
        - ``call = pytest_runtest_setup(item)`` (wrapped in ``CallInfo(when="setup")``)
        - ``report = pytest_runtest_makereport(item, call)``
        - ``pytest_runtest_logreport(report)``
        - ``pytest_exception_interact(call, report)`` if an interactive exception occurred

    - Call phase, if the the setup passed and the ``setuponly`` pytest option is not set:
        - ``call = pytest_runtest_call(item)`` (wrapped in ``CallInfo(when="call")``)
        - ``report = pytest_runtest_makereport(item, call)``
        - ``pytest_runtest_logreport(report)``
        - ``pytest_exception_interact(call, report)`` if an interactive exception occurred

    - Teardown phase:
        - ``call = pytest_runtest_teardown(item, nextitem)`` (wrapped in ``CallInfo(when="teardown")``)
        - ``report = pytest_runtest_makereport(item, call)``
        - ``pytest_runtest_logreport(report)``
        - ``pytest_exception_interact(call, report)`` if an interactive exception occurred

    - ``pytest_runtest_logfinish(nodeid, location)``

    :param item: Test item for which the runtest protocol is performed.
    :param nextitem: The scheduled-to-be-next test item (or None if this is the end my friend).

    Stops at first non-None result, see :ref:`firstresult`.
    The return value is not used, but only stops further processing.
    """

def pytest_runtest_logstart(
    nodeid: str, location: Tuple[str, Optional[int], str]
) -> None:
    """Called at the start of running the runtest protocol for a single item.

    See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.

    :param str nodeid: Full node ID of the item.
    :param location: A tuple of ``(filename, lineno, testname)``.
    """

def pytest_runtest_logfinish(
    nodeid: str, location: Tuple[str, Optional[int], str]
) -> None:
    """Called at the end of running the runtest protocol for a single item.

    See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.

    :param str nodeid: Full node ID of the item.
    :param location: A tuple of ``(filename, lineno, testname)``.
    """

def pytest_runtest_setup(item: "Item") -> None:
    """Called to perform the setup phase for a test item.在调用pytest_runtest_call之前调用

    The default implementation runs ``setup()`` on ``item`` and all of its
    parents (which haven't been setup yet). This includes obtaining the
    values of fixtures required by the item (which haven't been obtained
    yet).
    """

def pytest_runtest_call(item: "Item") -> None:
    """Called to run the test for test item (the call phase).调用执行测试的用例!

    The default implementation calls ``item.runtest()``.
    """

def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None:
    """Called to perform the teardown phase for a test item.

    The default implementation runs the finalizers and calls ``teardown()``
    on ``item`` and all of its parents (which need to be torn down). This
    includes running the teardown phase of fixtures required by the item (if
    they go out of scope).

    :param nextitem:
        The scheduled-to-be-next test item (None if no further test item is
        scheduled). This argument is used to perform exact teardowns, i.e.
        calling just enough finalizers so that nextitem only needs to call
        setup functions.
    """

@hookspec(firstresult=True)
def pytest_runtest_makereport(
    item: "Item", call: "CallInfo[None]"
) -> Optional["TestReport"]:
    """Called to create a :class:`~pytest.TestReport` for each of
    the setup, call and teardown runtest phases of a test item.运行测试用例,返回setup,call,teardown的执行结果

    See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.

    :param call: The :class:`~pytest.CallInfo` for the phase.

    Stops at first non-None result, see :ref:`firstresult`.
    """

def pytest_runtest_logreport(report: "TestReport") -> None:
    """Process the :class:`~pytest.TestReport` produced for each
    of the setup, call and teardown runtest phases of an item.

    See :hook:`pytest_runtest_protocol` for a description of the runtest protocol.
    """

@hookspec(firstresult=True)
def pytest_report_to_serializable(
    config: "Config",
    report: Union["CollectReport", "TestReport"],
) -> Optional[Dict[str, Any]]:
    """Serialize the given report object into a data structure suitable for
    sending over the wire, e.g. converted to JSON."""

@hookspec(firstresult=True)
def pytest_report_from_serializable(
    config: "Config",
    data: Dict[str, Any],
) -> Optional[Union["CollectReport", "TestReport"]]:
    """Restore a report object previously serialized with
    :hook:`pytest_report_to_serializable`."""

# -------------------------------------------------------------------------
# Fixture related hooks
# -------------------------------------------------------------------------

@hookspec(firstresult=True)
def pytest_fixture_setup(
    fixturedef: "FixtureDef[Any]", request: "SubRequest"
) -> Optional[object]:
    """Perform fixture setup execution.

    :returns: The return value of the call to the fixture function.

    Stops at first non-None result, see :ref:`firstresult`.

    .. note::
        If the fixture function returns None, other implementations of
        this hook function will continue to be called, according to the
        behavior of the :ref:`firstresult` option.
    """


def pytest_fixture_post_finalizer(
    fixturedef: "FixtureDef[Any]", request: "SubRequest"
) -> None:
    """Called after fixture teardown, but before the cache is cleared, so
    the fixture result ``fixturedef.cached_result`` is still available (not
    ``None``)."""

# -------------------------------------------------------------------------
# test session related hooks
# -------------------------------------------------------------------------

def pytest_sessionstart(session: "Session") -> None:
    """Called after the ``Session`` object has been created and before performing collection
    and entering the run test loop.

    :param pytest.Session session: The pytest session object.
    """

def pytest_sessionfinish(
    session: "Session",
    exitstatus: Union[int, "ExitCode"],
) -> None:
    """Called after whole test run finished, right before returning the exit status to the system.

    :param pytest.Session session: The pytest session object.
    :param int exitstatus: The status which pytest will return to the system.
    """

def pytest_unconfigure(config: "Config") -> None:
    """Called before test process is exited.

    :param pytest.Config config: The pytest config object.
    """

# -------------------------------------------------------------------------
# hooks for customizing the assert methods
# -------------------------------------------------------------------------

def pytest_assertrepr_compare(
    config: "Config", op: str, left: object, right: object
) -> Optional[List[str]]:
    """Return explanation for comparisons in failing assert expressions.

    Return None for no custom explanation, otherwise return a list
    of strings. The strings will be joined by newlines but any newlines
    *in* a string will be escaped. Note that all but the first line will
    be indented slightly, the intention is for the first line to be a summary.

    :param pytest.Config config: The pytest config object.
    """

def pytest_assertion_pass(item: "Item", lineno: int, orig: str, expl: str) -> None:
    """Called whenever an assertion passes.

    .. versionadded:: 5.0

    Use this hook to do some processing after a passing assertion.
    The original assertion information is available in the `orig` string
    and the pytest introspected assertion information is available in the
    `expl` string.

    This hook must be explicitly enabled by the ``enable_assertion_pass_hook``
    ini-file option:

    .. code-block:: ini

        [pytest]
        enable_assertion_pass_hook=true

    You need to **clean the .pyc** files in your project directory and interpreter libraries
    when enabling this option, as assertions will require to be re-written.

    :param pytest.Item item: pytest item object of current test.
    :param int lineno: Line number of the assert statement.
    :param str orig: String with the original assertion.
    :param str expl: String with the assert explanation.
    """
# -------------------------------------------------------------------------
# Hooks for influencing reporting (invoked from _pytest_terminal).
# -------------------------------------------------------------------------

def pytest_report_header(
    config: "Config", start_path: Path, startdir: "LEGACY_PATH"
) -> Union[str, List[str]]:
    """Return a string or list of strings to be displayed as header info for terminal reporting.

    :param pytest.Config config: The pytest config object.
    :param Path start_path: The starting dir.
    :param LEGACY_PATH startdir: The starting dir (deprecated).

    .. note::

        Lines returned by a plugin are displayed before those of plugins which
        ran before it.
        If you want to have your line(s) displayed first, use
        :ref:`trylast=True <plugin-hookorder>`.

    .. note::

        This function should be implemented only in plugins or ``conftest.py``
        files situated at the tests root directory due to how pytest
        :ref:`discovers plugins during startup <pluginorder>`.

    .. versionchanged:: 7.0.0
        The ``start_path`` parameter was added as a :class:`pathlib.Path`
        equivalent of the ``startdir`` parameter. The ``startdir`` parameter
        has been deprecated.
    """

def pytest_report_collectionfinish(
    config: "Config",
    start_path: Path,
    startdir: "LEGACY_PATH",
    items: Sequence["Item"],
) -> Union[str, List[str]]:
    """Return a string or list of strings to be displayed after collection
    has finished successfully.

    These strings will be displayed after the standard "collected X items" message.

    .. versionadded:: 3.2

    :param pytest.Config config: The pytest config object.
    :param Path start_path: The starting dir.
    :param LEGACY_PATH startdir: The starting dir (deprecated).
    :param items: List of pytest items that are going to be executed; this list should not be modified.

    .. note::

        Lines returned by a plugin are displayed before those of plugins which
        ran before it.
        If you want to have your line(s) displayed first, use
        :ref:`trylast=True <plugin-hookorder>`.

    .. versionchanged:: 7.0.0
        The ``start_path`` parameter was added as a :class:`pathlib.Path`
        equivalent of the ``startdir`` parameter. The ``startdir`` parameter
        has been deprecated.
    """

@hookspec(firstresult=True)
def pytest_report_teststatus(
    report: Union["CollectReport", "TestReport"], config: "Config"
) -> Tuple[str, str, Union[str, Mapping[str, bool]]]:
    """Return result-category, shortletter and verbose word for status
    reporting.

    The result-category is a category in which to count the result, for
    example "passed", "skipped", "error" or the empty string.

    The shortletter is shown as testing progresses, for example ".", "s",
    "E" or the empty string.

    The verbose word is shown as testing progresses in verbose mode, for
    example "PASSED", "SKIPPED", "ERROR" or the empty string.

    pytest may style these implicitly according to the report outcome.
    To provide explicit styling, return a tuple for the verbose word,
    for example ``"rerun", "R", ("RERUN", {"yellow": True})``.

    :param report: The report object whose status is to be returned.
    :param config: The pytest config object.

    Stops at first non-None result, see :ref:`firstresult`.
    """

def pytest_terminal_summary(
    terminalreporter: "TerminalReporter",
    exitstatus: "ExitCode",
    config: "Config",
) -> None:
    """Add a section to terminal summary reporting.

    :param _pytest.terminal.TerminalReporter terminalreporter: The internal terminal reporter object.
    :param int exitstatus: The exit status that will be reported back to the OS.
    :param pytest.Config config: The pytest config object.

    .. versionadded:: 4.2
        The ``config`` parameter.
    """

@hookspec(historic=True)
def pytest_warning_recorded(
    warning_message: "warnings.WarningMessage",
    when: "Literal['config', 'collect', 'runtest']",
    nodeid: str,
    location: Optional[Tuple[str, int, str]],
) -> None:
    """Process a warning captured by the internal pytest warnings plugin.

    :param warnings.WarningMessage warning_message:
        The captured warning. This is the same object produced by :py:func:`warnings.catch_warnings`, and contains
        the same attributes as the parameters of :py:func:`warnings.showwarning`.

    :param str when:
        Indicates when the warning was captured. Possible values:

        * ``"config"``: during pytest configuration/initialization stage.
        * ``"collect"``: during test collection.
        * ``"runtest"``: during test execution.

    :param str nodeid:
        Full id of the item.

    :param tuple|None location:
        When available, holds information about the execution context of the captured
        warning (filename, linenumber, function). ``function`` evaluates to <module>
        when the execution context is at the module level.

    .. versionadded:: 6.0
    """
# -------------------------------------------------------------------------
# Hooks for influencing skipping
# -------------------------------------------------------------------------

def pytest_markeval_namespace(config: "Config") -> Dict[str, Any]:
    """Called when constructing the globals dictionary used for
    evaluating string conditions in xfail/skipif markers.

    This is useful when the condition for a marker requires
    objects that are expensive or impossible to obtain during
    collection time, which is required by normal boolean
    conditions.

    .. versionadded:: 6.2

    :param pytest.Config config: The pytest config object.
    :returns: A dictionary of additional globals to add.
    """
# -------------------------------------------------------------------------
# error handling and internal debugging hooks
# -------------------------------------------------------------------------

def pytest_internalerror(
    excrepr: "ExceptionRepr",
    excinfo: "ExceptionInfo[BaseException]",
) -> Optional[bool]:
    """Called for internal errors.

    Return True to suppress the fallback handling of printing an
    INTERNALERROR message directly to sys.stderr.
    """

def pytest_keyboard_interrupt(
    excinfo: "ExceptionInfo[Union[KeyboardInterrupt, Exit]]",
) -> None:
    """Called for keyboard interrupt."""

def pytest_exception_interact(
    node: Union["Item", "Collector"],
    call: "CallInfo[Any]",
    report: Union["CollectReport", "TestReport"],
) -> None:
    """Called when an exception was raised which can potentially be
    interactively handled.

    May be called during collection (see :hook:`pytest_make_collect_report`),
    in which case ``report`` is a :class:`CollectReport`.

    May be called during runtest of an item (see :hook:`pytest_runtest_protocol`),
    in which case ``report`` is a :class:`TestReport`.

    This hook is not called if the exception that was raised is an internal
    exception like ``skip.Exception``.
    """

def pytest_enter_pdb(config: "Config", pdb: "pdb.Pdb") -> None:
    """Called upon pdb.set_trace().

    Can be used by plugins to take special action just before the python
    debugger enters interactive mode.

    :param pytest.Config config: The pytest config object.
    :param pdb.Pdb pdb: The Pdb instance.
    """

def pytest_leave_pdb(config: "Config", pdb: "pdb.Pdb") -> None:
    """Called when leaving pdb (e.g. with continue after pdb.set_trace()).

    Can be used by plugins to take special action just after the python
    debugger leaves interactive mode.

    :param pytest.Config config: The pytest config object.
    :param pdb.Pdb pdb: The Pdb instance.
    """

修改默认编码

pytest_collection_modifyitems 为收集上来的测试用例实现定制化的功能,可以自定义用例执行的顺序,解决编码问题(中文的用例名称),自动添加标签。。。
conftest.py

def pytest_collection_modifyitems(
    session: "Session", config: "Config", items: List["Item"]
) -> None:
    for item in items:
        item.name = item.name.encode('utf-8').decode('unicode-escape')
        item._nodeid = item.nodeid.encode('utf-8').decode('unicode-escape')

添加命令行参数

def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") -> None:
    mygroup = parser.getgroup("lly")
    mygroup.addoption(
        "--env",
        default="test",
        dest="env",
        help="set your run env"
    )

@pytest.fixture(scope="session")
def cmdoption(request):
    myenv = request.config.getoption("--env",default="test")
    if myenv == "test":
        datapath = "datas/test/data.yml"
    elif myenv == "dev":
        datapath = "datas/dev/data.yml"
    with open(datapath) as f:
        datas = yaml.safe_load(f)
    return myenv,datas

# 执行pytest --env dev

插件打包发布

www.pypi.org官网说明
打包项目构成:源码包、setup.py、测试包

from setuptools import setup,find_packages
setup(
    name='pytest_xxx',
    url='https://github.com/xx/pytest-xxx',
    version='1.0',
    auther='balahalalala',
    auther_email='xxx@qq.com',
    description='set your ...',
    long_description='...',
    classifiers=[# 分类索引 pip 对所属包的分类
        'Framework :: Pytest',
        'Programming Language :: Python :: 3.8',
        'Topic :: Software Development :: Testing',
    ],
    license='proprietary',
    packages=find_packages(),
    keywords=['pytest','py.test','pytest_encode'],
    install_requires=['pytest'],#需要安装的依赖
    entry_points={'pytest11': ['pytest_xx = pytest_xx.main']},#入口模块或入口函数
    zip_safe=False
)

依赖包安装

  • pip install setuptools 包管理工具
  • pip install wheel 生成*.whl 格式的安装包,本质上也是一个压缩包

打包命令

  • python setup.py sdist bdist_wheel
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

balahalalala

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值