

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 pytest 設定測試報告
<a name="test-report-pytest"></a>

下列程序示範如何使用 pytest 測試架構 AWS CodeBuild 在 中設定測試報告。 [https://docs.pytest.org/](https://docs.pytest.org/)

此程序需要下列先決條件：
+ 您有現有的 CodeBuild 專案。
+ 您的專案是設定為使用 pytest 測試框架的 Python 專案。

將以下項目新增至 `build` 或 `buildspec.yml` 檔案的 `post_build` 階段。此程式碼會自動探索目前目錄中的測試，並將測試報告匯出至 *<test report directory>*/*<report filename>* 指定的檔案。報告使用 `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
```