

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用 pytest 设置测试报告
<a name="test-report-pytest"></a>

以下过程演示如何在 AWS CodeBuild 中使用 [pytest 测试框架](https://docs.pytest.org/)来设置测试报告。

该过程需要以下先决条件：
+ 您有一个现有的 CodeBuild 项目。
+ 您的项目是一个 Python 项目，此项目设置为使用 pytest 测试框架。

将以下条目添加到 `build` 文件的 `post_build` 或 `buildspec.yml` 阶段。此代码会自动发现当前目录中的测试，并将测试报告导出到由 *<测试报告目录>*/*<报告文件名>* 指定的文件中。报告使用 `JunitXml` 格式。

```
      - python -m pytest --junitxml=<test report directory>/<report filename>
```

在您的 `buildspec.yml` 文件中，添加/更新以下部分。

```
version: 0.2

phases:
  install:
    runtime-versions:
      python: 3.7
    commands:
      - pip3 install pytest
  build:
    commands:
      - python -m pytest --junitxml=<test report directory>/<report filename>

reports:
  pytest_reports:
    files:
      - <report filename>
    base-directory: <test report directory>
    file-format: JUNITXML
```