aws codebuild 使用和配置codebuild测试报告

参考资料

在codebuild构建过程中获取有关在构建期间运行的测试的详细信息。

codebuild测试报告

通过在buildspec.yaml中配置报告组,运行构建项目时系统将运行测试用例并创建测试报告。不需要在运行测试之前创建报告组。

  • 如果指定报表组名称, CodeBuild 当您运行报告时创建报告组
  • 如果要使用已存在的报告组,需要在buildspec 文件中指定其 arn

测试报告

报告buildspec.yaml配置

通过测试报告可以发现并解决构建过程中存在的问题。报告创建后30天过期,也可以无限期存储在s3桶(额外权限配置)。

测试报告支持以下格式

  • Cucumber JSON (.json)
  • JUnit XML (.xml)
  • NUnit XML (.xml)
  • NUnit3 XML (.xml)
  • TestNG XML (.xml)
  • Visual Studio TRX (.trx)

测试报告相关权限

https://docs.aws.amazon.com/zh_cn/codebuild/latest/userguide/test-permissions.html#test-permissions-required

对于新创建的项目默认托管策略中已经配置权限。对于自定义的构建项目,codebuild角色需要以下权限

  • CreateReportGroup

  • CreateReport

  • UpdateReport

  • BatchPutTestCases

  • BatchPutCodeCoverages(代码覆盖率报告)

代码覆盖率报告

包括line和branch覆盖

  • line coverage = (total lines covered)/(total number of lines)
  • branch coverage = (total branches covered)/(total number of branches)

代码覆盖率报告支持以下格式

  • JaCoCo XML
  • SimpleCov JSON
  • Clover XML
  • Cobertura XML

测试项目

aws sample仓库提供了sample项目,对于不同场景下的构建环境都提供了demo

手动创建测试组

$ cat > CreateReportGroupInput.json << EOF
{
  "name": "test-report",
  "type": "TEST",
  "exportConfig": {
    "exportConfigType": "S3",
    "s3Destination": {
      "bucket": "codebuild-bjs-output-bucket",
      "path": "report",
      "packaging": "NONE"
    }
  }
}
EOF
$ aws codebuild create-report-group --cli-input-json file://CreateReportGroupInput.json
reportGroup:
  arn: arn:aws-cn:codebuild:cn-north-1:xxxxxx:report-group/test-report
  exportConfig:
    exportConfigType: S3
    s3Destination:
      bucket: codebuild-bjs-output-bucket
      bucketOwner: 'xxxxxxxxx'
      encryptionKey: arn:aws-cn:kms:cn-north-1:xxxxxxxxx:alias/aws/s3
      packaging: NONE
      path: report
  name: test-report
  status: ACTIVE
  type: TEST

示例buildspec.yaml

version: 0.2
phases:
  install:
    runtime-versions:
      # java: openjdk8
      # python: 3.7
  build:
    commands:
    - echo Running tests 
    - <enter commands to run your tests>
  reports:
    # <report-name-or-arn>:
    arn:aws-cn:codebuild:cn-north-1:xxxxxxx:report-group/test-report:
      files:
      - '<test-result-files>'
      base-directory: '<optional-base-directory>'
      discard-paths: false

nodejs项目测试

https://github.com/istanbuljs/nyc

https://mochajs.bootcss.com/

测试框架 Mocha 实例教程

sample项目是一个计算的四则运算计算器

$ npm test
> simple-calculator-service@1.0.0 test
> ./node_modules/nyc/bin/nyc.js --reporter=lcov ./node_modules/mocha/bin/mocha

  Calculator Tests
    Addition Tests
      ✓ returns 1 + 1 = 2
      ✓ returns 1 + -1 = 0
    Subtraction Tests
      ✓ returns 2 - 1 = 1
      ✓ returns 1 - -1 = 2
    Multiply Tests
      ✓ returns 2 * 2 = 4
      ✓ returns 0 * 4 = 4

  Calculator service
    ✓ it should connect to the redis cache
    ✓ it should add two numbers

  8 passing (35ms)

查看测试报告

$ ./node_modules/mocha/bin/mocha --reporters # report支持的格式
    dot - dot matrix
    doc - html documentation
    spec - hierarchical spec list
    json - single json object
    progress - progress bar
    list - spec-style listing
    tap - test-anything-protocol
    landing - unicode landing strip
    xunit - xunit reporter
    min - minimal reporter (great with --watch)
    json-stream - newline delimited json events
    markdown - markdown documentation (github flavour)
    nyan - nyan cat!
$ ./node_modules/mocha/bin/mocha --reporter=xunit
<testsuite name="Mocha Tests" tests="8" failures="0" errors="0" skipped="0" timestamp="Mon, 16 Jan 2023 17:16:08 GMT" time="0.032">
<testcase classname="Calculator Tests Addition Tests" name="returns 1 + 1 = 2" time="0.001"/>
<testcase classname="Calculator Tests Addition Tests" name="returns 1 + -1 = 0" time="0"/>
<testcase classname="Calculator Tests Subtraction Tests" name="returns 2 - 1 = 1" time="0"/>
<testcase classname="Calculator Tests Subtraction Tests" name="returns 1 - -1 = 2" time="0"/>
<testcase classname="Calculator Tests Multiply Tests" name="returns 2 * 2 = 4" time="0"/>
<testcase classname="Calculator Tests Multiply Tests" name="returns 0 * 4 = 4" time="0.001"/>
<testcase classname="Calculator service" name="it should connect to the redis cache" time="0"/>
<testcase classname="Calculator service" name="it should add two numbers" time="0.024"/>
</testsuite>

修改buildspec.yml

version: 0.2

phases:
  install:
    commands:
      - npm install
  build:
    commands:
      - ./node_modules/mocha/bin/mocha --reporter=xunit > report.xml
artifacts:
  files:
    - '**/*'
reports:
  mocha-reports:
    files:
      - "report.xml"
    file-format: "JUNITXML"

创建codebuild项目并启动构建,在控制台查看测试结果

在这里插入图片描述

感觉codebuild的测试报告功能比较鸡肋,支持的格式太少了

posted @ 2023-01-17 01:23  zhaojie10  阅读(11)  评论(0)    收藏  举报  来源