

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

# 測試架構
<a name="test-framework-reporting"></a>

本節中的主題示範如何在 中 AWS CodeBuild 針對各種測試架構設定測試報告。

**Topics**
+ [使用 Jasmine 設定測試報告](test-report-jasmine.md)
+ [使用 Jest 設定測試報告](test-report-jest.md)
+ [使用 pytest 設定測試報告](test-report-pytest.md)
+ [使用 RSpec 設定測試報告](test-report-rspec.md)

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

下列程序示範如何使用 JasmineBDD 測試架構 AWS CodeBuild 在 中設定測試報告。 [JasmineBDD ](http://jasmine.github.io/) 

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

將 [https://www.npmjs.com/package/jasmine-reporters](https://www.npmjs.com/package/jasmine-reporters) 套件新增至您專案的 `package.json` 檔案的 `devDependencies` 區段。此套件具備 JavaScript 報告程式類別的集合，可與 Jasmine 搭配使用。

```
npm install --save-dev jasmine-reporters
```

如果尚未存在，請將 `test` 指令碼新增至專案的 `package.json` 檔案中。`test` 指令碼可確保**npm test**在執行時呼叫 Jasmine。

```
{
  "scripts": {
    "test": "npx jasmine"
  }
}
```

CodeBuild 支援下列 Jasmine 測試報告程式：

**JUnitXmlReporter**  
用於產生 `JunitXml` 格式的報告。

**NUnitXmlReporter**  
用於產生 `NunitXml` 格式的報告。

預設情況下，使用 Jasmine 的 Node.js 專案會有 `spec` 子目錄，其包含 Jasmine 組態和測試指令碼。

若要將 Jasmine 設為產生 `JunitXML` 格式的報告，請將以下程式碼新增至您的測試，以執行個體化 `JUnitXmlReporter` 報告程式。

```
var reporters = require('jasmine-reporters');

var junitReporter = new reporters.JUnitXmlReporter({
  savePath: <test report directory>,
  filePrefix: <report filename>,
  consolidateAll: true
});

jasmine.getEnv().addReporter(junitReporter);
```

若要將 Jasmine 設為產生 `NunitXML` 格式的報告，請將以下程式碼新增至您的測試，以執行個體化 `NUnitXmlReporter` 報告程式。

```
var reporters = require('jasmine-reporters');

var nunitReporter = new reporters.NUnitXmlReporter({
  savePath: <test report directory>,
  filePrefix: <report filename>,
  consolidateAll: true
});

jasmine.getEnv().addReporter(nunitReporter)
```

測試報告會匯出至 *<test report directory>*/*<report filename>* 指定的檔案。

在您的 `buildspec.yml` 檔案中，新增/更新以下區段。

```
version: 0.2

phases:
  pre_build:
    commands:
      - npm install
  build:
    commands:
      - npm build
      - npm test

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

如果您使用的是 `NunitXml` 報告格式，請將 `file-format` 值變更為以下內容。

```
    file-format: NUNITXML
```

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

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

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

將 [https://www.npmjs.com/package/jest-junit](https://www.npmjs.com/package/jest-junit) 套件新增至您專案的 `package.json` 檔案的 `devDependencies` 區段。CodeBuild 使用此套件來產生 `JunitXml` 格式的報告。

```
npm install --save-dev jest-junit
```

如果尚未存在，請將 `test` 指令碼新增至專案的 `package.json` 檔案中。`test` 指令碼可確保在 **npm test** 執行時呼叫 Jest。

```
{
  "scripts": {
    "test": "jest"
  }
}
```

將以下內容新增至您的 Jest 組態檔，以將 Jest 設為使用 `JunitXml` 報告程式。如果您的專案沒有 Jest 組態檔，請在您專案的根目錄中，建立一個名為 `jest.config.js` 的檔案，並新增以下內容。測試報告會匯出至 *<test report directory>*/*<report filename>* 指定的檔案。

```
module.exports = {
  reporters: [
    'default',
    [ 'jest-junit', {
      outputDirectory: <test report directory>,
      outputName: <report filename>,
    } ]
  ]
};
```

在您的 `buildspec.yml` 檔案中，新增/更新以下區段。

```
version: 0.2

phases:
  pre_build:
    commands:
      - npm install
  build:
    commands:
      - npm build
      - npm test

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

# 使用 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
```

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

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

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

在您的 `buildspec.yml` 檔案中，新增/更新以下內容。此程式碼會在 *<test source directory>* 目錄中執行測試，並將測試報告匯出至 *<test report directory>*/*<report filename>* 指定的檔案。報告使用 `JunitXml` 格式。

```
version: 0.2

phases:
  install:
    runtime-versions:
      ruby: 2.6
  pre_build:
    commands:
      - gem install rspec
      - gem install rspec_junit_formatter
  build:
    commands:
      - rspec <test source directory>/* --format RspecJunitFormatter --out <test report directory>/<report filename>
reports:
    rspec_reports:
        files:
            - <report filename>
        base-directory: <test report directory>
        file-format: JUNITXML
```