Skip to main content

用于 influxdb 和 pytest 集成的插件。

项目描述

Influxdb Pytest 插件

Pytest 插件设计用于向“InfluxDB”报告测试结果。

目录

  • 关于本文档

  • 用法

  • 添加自定义字段作为附加数据(测试级别和套件级别)

  • 发送附件

关于本文档

欢迎使用 Influxdb Pytest 插件文档!

本样式指南为使用 Influxdb Pytest 插件的任何人提供了一组编辑指南。

用法

安装

pip install influxdb-pytest-plugin

发射

要使用 influxdb-pytest-plugin 运行测试,应提供“–influxdb-pytest”标志。

pytest -sv –influxdb-pytest

在测试的根目录中准备配置文件pytest.ini和/或在运行命令旁边调用下面提到的强制配置变量:

下一个必填字段应在pytest.ini不带“-”前缀的命令行中提及或运行:

  • --influxdb_host- influxdb 的主机

  • --influxdb_name- influxdb 表的名称

以下是可选字段:

  • --influxdb_port- influxdb 的端口

  • --influxdb_user- influxdb 用户的用户名

  • --influxdb_password- influxdb 用户的密码

  • --influxdb_project- 项目名称

  • --influxdb_version- 项目的自定义版本

  • --influxdb_run_id- 运行 id(可以作为 CI 变量传递)

示例pytest.ini

[pytest]
influxdb_host = <DB_HOST>
influxdb_port = <DB_PORT>
influxdb_name = <DB_NAME>
influxdb_user = <DB_USER>
influxdb_password = <DB_PASSWORD>
influxdb_project = <PROJECT_NAME>
influxdb_version = <PROJECT_NAME>
influxdb_run_id = <RUN_ID>

添加自定义字段作为附加数据

为测试结果添加自定义字段 要添加自定义字段作为测试结果的附加数据,应在 conftest.py 中添加如下代码。

示例 1:

@pytest.fixture(scope='function', autouse=True)
def test_suite(request):
    from influxdb_pytest_plugin import TestResultDTO
    test_result_dto = TestResultDTO()
    test_name = request.node.nodeid
    TestResultDTO.set_tag_values(test_result_dto, test_name, {'tag1': 'tag_value1', 'tag2': 'tag_value2'})
    TestResultDTO.set_field_values(test_result_dto, test_name, {'field1': 'field_value1', 'field2': 'field_value2'})

为套件结果 添加自定义字段为了添加自定义字段作为套件结果的附加数据,pytest_terminal_summarypytest 插件如下 conftest.py。

@pytest.hookimpl(hookwrapper=True)
def pytest_terminal_summary(config, terminalreporter):
    from influxdb_pytest_plugin import SuiteResultDTO
    suite_result_dto = SuiteResultDTO()
    SuiteResultDTO.set_tag_values(suite_result_dto, {'tag1': 'tag_value1'})
    SuiteResultDTO.set_field_values(suite_result_dto, {'field1': 'field_value1'})
    yield

示例 2:通过--influxdb_valuesconfig 添加自定义字段,只需填写以下模板并设置为--influxdb_valuesconfig:

{
  "fields": {
    "test_result": {
    },
    "suite_result": {
    }
  },
  "tags": {
    "test_result": {
    },
    "suite_result": {
    }
  }
}

将截图作为附件发送

要将屏幕截图发送到 influxdb,screenshot_url应在函数范围内使用夹具,如下所示:

示例 1:

@pytest.fixture(scope="function")
def chrome_driver_init(request, screenshot_url, pytestconfig):
chrome_driver = webdriver.Chrome()
request.cls.driver = chrome_driver
yield
if request.node.rep_call.failed and pytestconfig.getoption('--influxdb-pytest'):
    screenshot_link = 'URL_EXAMPLE'
    chrome_driver.save_screenshot(screenshot_link)
    screenshot_url(screenshot_link)
chrome_driver.close()

示例 2:

@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    outcome = yield
    rep = outcome.get_result()
    if rep.when == 'call':
        try:
            screenshot_path = web_client.current.save_screenshot("Screenshot link")
            item.user_properties = ("screenshot_url", screenshot_path)
        # web_driver.save_screenshot and other magic to add screenshot to your report
        except Exception as e:
            print('Exception while screen-shot creation: {}'.format(e))

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

influxdb_pytest_plugin-0.2.57.tar.gz (9.1 kB 查看哈希)

已上传 source