seretos/behat-logger-extension

将behat执行记录到json结果中


README

本包为behat提供扩展,用于将测试结果记录到json文件中。此包还提供命令来验证和合并这些json文件。

安装

信息:如果您想将behat日志扩展用于您的项目,请阅读“PHP集成”部分!安装部分仅安装作为独立应用程序的cli命令

对于用户,现在有一个可用的docker镜像。对于开发人员,请参阅PHP集成

从1.x迁移到2.x

自2.0版主要版本以来,测试的同步标识已更改。在版本1中,场景标题用于同步。在这个版本中,将使用由behat标签提供的id。每个测试都需要一个唯一的标识符标签。例如 "@testrail-case-1,@testrail-case-2, e.t.c。" 将您的依赖更新到最新的1.x版本,并在您的.testrail.yml中添加以下属性

api:
    ...
    identifier_tag_field: yourNewIdentifierField

执行testrail:push:cases命令将所有id添加到testrail中的案例。现在您可以更新到2.x(但一些.testrail.yml属性已更改!!!)

docker镜像

docker run -v /path/to/project/:/behat/ seretos/behat-logger-cli behat-logger-cli list

将docker容器添加到您的docker-compose.yml中

version: '3.7'

services:
  features-push:
    image: seretos/behat-logger-cli
    command: push
    volumes:
      - ./:/behat/
    environment:
      TESTRAIL_SERVER: http://testrail:80
      TESTRAIL_USER: yourTestrailLogin
      TESTRAIL_PASSWORD: yourTestrailPassword
      TESTRAIL_PROJECT: yourTestrailProject
      TESTRAIL_SUITE: yourTestrailSuite

  features-validate:
    image: seretos/behat-logger-cli
    command: validate
    volumes:
      - ./:/behat/

PHP集成

按以下方式将包添加到您的项目中

composer require seretos/behat-logger-extension --dev
vendor/bin/behat-logger-cli --help

在behat.yml中激活日志记录器

default:
    formatters:
        logger: ~
    extensions:
        seretos\BehatLoggerExtension\BehatLoggerExtension:
            output_path: '%paths.base%/build/behat'

可选:如果您使用的是symfony应用程序,可以将此扩展(seretos\BehatLoggerExtension\BehatLoggerExtensionBundle)添加到您的Symfony内核中,并将命令集成到您的cli中

命令行使用

将不同的结果json文件合并到一个文件和一个套件中

behat-logger-cli combine:logs [suite-name] --output=/output/path/ --regex=results/firefox*

如果不同的json包含一个测试结果,且该测试结果具有相同的测试环境,此命令将抛出异常

将所有找到的特征文件转换为不带结果的单一json文件

behat-logger-cli feature:to:log [suite-name] --output=/output/path/ --regex=features/

检查日志文件中的所有场景都有一个唯一的标题

此命令已弃用,请将来使用validate:scenario:id!

behat-logger-cli validate:scenario:title [log-file.json]

检查日志文件中的所有场景都有一个唯一的id标签

behat-logger-cli validate:scenario:id [log-file.json] --identifier_tag_regex="/^testrail-case-([0-9]*)$/"

检查所有测试都在给定的环境中执行

# check that all browserless tests are executed
behat-logger-cli validate:execution actual.json expected.json --tags=~javascript --environments=unknown
# check that all browser tests are executed in firefox and chrome
behat-logger-cli validate:execution actual.json expected.json --tags=javascript --environments=firefox --environments=chrome

将json结果发送到testrail并创建部分和案例

behat-logger-cli testrail:push:cases testRailSuiteName actual.json

将json结果发送到testrail并创建环境配置

behat-logger-cli testrail:push:configs actual.json

将json结果发送到testrail并创建结果

behat-logger-cli testrail:push:results testRailSuiteName actual.json testResultName --milestone=v2.8.0

命令testrail:push:cases和testrail:push:results需要当前工作目录中的.testrail.yml文件,并包含以下信息

api:
  server: https://yourTestrail.testrail.io/
  user: yourUser@mail.de
  password: yourPassword
  project: youtProject
  template: Test Case (Steps)
  type: Automated
  title_field: custom_preconds
  group_field: custom_automation_type
  identifier_field: custom_identifier
  identifier_regex: /^testrail-case-([0-9]*)$/

# set field an priorities on specific tags
fields:
  /^.*$/:
    custom_automation_type: Behat

priorities:
  /^priority_low$/: Low

日志格式

首先,json文件包含behat套件。如果日志写入器无法检测套件名称,则使用名为“default”的套件

{
  "suites": [
    {
      "name": "default",
      "features": {
        ...
      }
    }
  ]
}

套件包含一系列特性

"features": {
  "features\/featurefile.feature": {
    "title": "feature title",
    "filename": "features\/featurefile.feature",
    "description": null,
    "language": "en",
    "scenarios": {
      ...
    }
  },
  ...
}

特性包含具有步骤和结果的场景

"scenarios": {
  "scenariotitle": {
    "title": "scenariotitle",
    "tags": ["behattag1","behattag2"],
    "steps": [
      {
        "line": 0,
        "text": "the user 'test' exists",
        "keyword": "Given",
        "arguments": []
      },
      {
        "line": 1,
        "text": "i logged in as 'test'",
        "keyword": "And",
        "arguments": []
      }
    ],
    "results": [
      ...
    ]
  },
  ...
}

最后但并非最不重要的是,包含特性结果。环境属性是浏览器名称。在guette中,环境名称设置为“未知”

"results": [
"firefox": {
  "environment": "firefox",
  "duration": "1.00",
  "message": null,
  "stepResults": [
    {
      "line": 0,
      "passed": true,
      "screenshot": null,
      "message": null
    },
    {
      "line": 1,
      "passed": true,
      "screenshot": null,
      "message": null
    }
  ]
  }
]