

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

# 在批次建置中啟用平行測試執行
<a name="parallel-test-enable"></a>

若要平行執行測試，請更新批次組建 buildspec 檔案，以包含 build-fanout 欄位和平行組建數目，以在 `parallelism` 欄位中分割測試套件，如下所示。`parallelism` 欄位指定要設定多少獨立執行器來執行測試套件。

若要在多個平行執行環境中執行測試，請將 `parallelism` 欄位設定為大於零的值。在以下範例中， `parallelism` 設定為 5，表示 CodeBuild 會啟動五個相同的組建，並平行執行測試套件的一部分。

您可以使用 [codebuild-tests-run](parallel-test-tests-run.md) CLI 命令來分割和執行測試。您的測試檔案會分割，而您的部分測試會在每次建置中執行。這可減少執行完整測試套件所需的整體時間。在下列範例中，測試將分割成五個，並根據測試的名稱計算分割點。

```
version: 0.2

batch:
  fast-fail: false 
  build-fanout:
    parallelism: 5
    ignore-failure: false
    
phases:
  install:
    commands:
      - npm install jest-junit --save-dev
  pre_build:
    commands:
      - echo 'prebuild'
  build:
    commands:
      - |
        codebuild-tests-run \
         --test-command 'npx jest --runInBand --coverage' \
         --files-search "codebuild-glob-search '**/_tests_/**/*.test.js'" \
         --sharding-strategy 'equal-distribution'

  post_build:
    commands:
      - codebuild-glob-search '**/*.xml'  
      - echo "Running post-build steps..."
      - echo "Build completed on `date`"

reports:
  test-reports:
    files:
      - '**/junit.xml'               
    base-directory: .
    discard-paths: yes           
    file-format: JUNITXML
```

如果報告已設定為建置集組建，則每個組建都會分別產生測試報告，這些報告可以在 AWS CodeBuild 主控台中對應組建**的報告**索引標籤下檢視。

如需如何批次執行平行測試的詳細資訊，請參閱 [各種測試架構範例的平行測試執行](sample-parallel-test.md)。