AWS Blogs 03月31日 17:08
Accelerating CI with AWS CodeBuild: Parallel test execution now available
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

AWS CodeBuild 现已支持并行测试执行,显著减少构建时间。通过并行运行测试套件,构建时间从35分钟缩短至6分钟,加速了CI流程,提高了开发效率。文章介绍了如何配置并行测试,包括使用buildspec.yml文件中的batch fanout部分,以及codebuild-tests-run命令进行测试分片。此外,还详细说明了测试报告的合并功能,简化了结果分析。

⏱️并行测试执行允许在多个构建计算环境中同时运行测试,通过将测试套件分割成多个子集,每个构建节点独立执行一部分测试。

⚙️通过在buildspec.yml文件中配置batch fanout部分,可以指定所需的并行级别和其他参数。使用codebuild-tests-run实用程序,结合适当的测试命令和分片方法。

💡CodeBuild提供了两种分片策略:等量分布和稳定性。等量分布策略按字母顺序对测试文件进行排序,并将其均匀分配到并行测试环境中;稳定性策略使用一致的哈希算法来固定测试在分片中的分布。

✅CodeBuild支持自动合并测试报告,将测试报告整合为单个测试摘要,简化结果分析。合并后的报告包括汇总的通过/失败状态、测试持续时间和失败详情,减少了手动报告处理的需求。

<section class="blog-post-content lb-rtxt"><table id="amazon-polly-audio-table"><tbody><tr><td id="amazon-polly-audio-tab"><p></p></td></tr></tbody></table><p>I’m excited to announce that <a href="https://aws.amazon.com/codebuild/&quot;&gt;AWS CodeBuild</a> now supports parallel test execution, so you can run your test suites concurrently and reduce build times significantly.</p><p>With <a href="https://github.com/sebsto/aws-codebuild-parallel-testing&quot;&gt;the demo project I wrote for this post</a>, the total test time went down from 35 minutes to 6 minutes, including the time to provision the environments. These two screenshots from the <a href="https://aws.amazon.com/console/&quot;&gt;AWS Management Console</a> show the difference.</p><p><strong>Sequential execution of the test suite</strong></p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/25/2025-03-24_18-19-55.png&quot;&gt;&lt;img class="aligncenter wp-image-94688" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/25/2025-03-24_18-19-55.png&quot; alt="CodeBuild Parallel Test Results" width="800" height="181" /></a></p><p><strong>Parallel execution of the test suite</strong></p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/25/2025-03-24_18-18-26.png&quot;&gt;&lt;img class="aligncenter wp-image-94689" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/25/2025-03-24_18-18-26.png&quot; alt="CodeBuild Parallel Test Results" width="800" height="181" /></a></p><p>Very long test times pose a significant challenge when running <a href="https://docs.aws.amazon.com/glossary/latest/reference/glos-chap.html#continuous_integration&quot;&gt;continuous integration (CI)</a> at scale. As projects grow in complexity and team size, the time required to execute comprehensive test suites can increase dramatically, leading to extended pipeline execution times. This not only delays the delivery of new features and bug fixes, but also hampers developer productivity by forcing them to wait for build results before proceeding with their tasks. I have experienced pipelines that took up to 60 minutes to run, only to fail at the last step, requiring a complete rerun and further delays. These lengthy cycles can erode developer trust in the CI process, contribute to frustration, and ultimately slow down the entire software delivery cycle. Moreover, long-running tests can lead to resource contention, increased costs because of wasted computing power, and reduced overall efficiency of the development process.</p><p>With parallel test execution in CodeBuild, you can now run your tests concurrently across multiple build compute environments. This feature implements a sharding approach where each build node independently executes a subset of your test suite. CodeBuild provides environment variables that identify the current node number and the total number of nodes, which are used to determine which tests each node should run. There is no control build node or coordination between nodes at build time—each node operates independently to execute its assigned portion of your tests.</p><p>To enable test splitting, configure the <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/batch-build-buildspec.html#build-spec.batch.build-fanout&quot;&gt;batch fanout</a> section in your <code>buildspec.xml</code>, specifying the desired parallelism level and other relevant parameters. Additionally, use the <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/parallel-test-tests-run.html&quot;&gt;codebuild-tests-run&lt;/a&gt; utility in your build step, along with the appropriate test commands and the chosen splitting method.</p><p>The tests are split based on the sharding strategy you specify. <code>codebuild-tests-run</code> offers two sharding strategies:</p><ul><li><strong>Equal-distribution.</strong> This strategy sorts test files alphabetically and distributes them in chunks equally across parallel test environments. Changes in the names or quantity of test files might reassign files across shards.</li><li><strong>Stability.</strong> This strategy fixes the distribution of tests across shards by using a consistent hashing algorithm. It maintains existing file-to-shard assignments when new files are added or removed.</li></ul><p>CodeBuild supports automatic merging of test reports when running tests in parallel. With automatic test report merging, CodeBuild consolidates tests reports into a single test summary, simplifying result analysis. The merged report includes aggregated pass/fail statuses, test durations, and failure details, reducing the need for manual report processing. You can view the merged results in the <a href="https://console.aws.amazon.com/codebuild&quot;&gt;CodeBuild console</a>, retrieve them using the <a href="https://aws.amazon.com/cli/&quot;&gt;AWS Command Line Interface (AWS CLI)</a>, or integrate them with other reporting tools to streamline test analysis.</p><p><strong>Let’s look at how it works</strong><br />Let me demonstrate how to implement parallel testing in a project. For this demo, I created <a href="https://github.com/sebsto/aws-codebuild-parallel-testing&quot;&gt;a very basic Python project with hundreds of tests</a>. To speed things up, I asked <a href="https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/command-line.html&quot;&gt;Amazon Q Developer on the command line</a> to create a project and 1,800 test cases. Each test case is in a separate file and takes one second to complete. Running all tests in a sequence requires 30 minutes, excluding the time to provision the environment.</p><p>In this demo, I run the test suite on ten compute environments in parallel and measure how long it takes to run the suite.</p><p>To do so, I added a <code>buildspec.yml</code> file to my project.</p><pre class="lang-yaml">version: 0.2batch: fast-fail: false build-fanout: parallelism: 10 # ten runtime environments ignore-failure: falsephases: install: commands: - echo 'Installing Python dependencies' - dnf install -y python3 python3-pip - pip3 install --upgrade pip - pip3 install pytest build: commands: - echo 'Running Python Tests' - | codebuild-tests-run \ --test-command 'python -m pytest --junitxml=report/testreport.xml' \ --files-search "codebuild-glob-search 'tests/test.py'" \ --sharding-strategy 'equal-distribution' post_build: commands: - echo "Test execution completed"reports: pytest_reports: files: - ".xml" base-directory: "report" file-format: JUNITXML </pre><p>There are three parts to highlight in the YAML file.</p><p>First, there’s a <code>build-fanout</code> section under <code>batch</code>. The <code>parallelism</code> command tells CodeBuild how many test environments to run in parallel. The <code>ignore-failure</code> command indicates if failure in any of the fanout build tasks can be ignored.</p><p>Second, I use the pre-installed <code>codebuild-tests-run</code> command to run my tests.</p><p>This command receives the complete list of test files and decides which of the tests must be run on the current node.</p><ul><li>Use the <code>sharding-strategy</code> argument to choose between equally distributed or stable distribution, as I explained earlier.</li><li>Use the <code>files-search</code> argument to pass all the files that are candidates for a run. We recommend to use the provided <code>codebuild-glob-search</code> command for performance reasons, but any file search tool, such as <a href="https://man7.org/linux/man-pages/man1/find.1.html&quot;&gt;find(1)&lt;/a&gt;, will work.</li><li>I pass the actual test command to run on the shard with the <code>test-command</code> argument.</li></ul><p>Lastly, the <code>reports</code> section instructs CodeBuild to collect and merge the test reports on each node.</p><p>Then, I open the CodeBuild console to create a project and a batch build configuration for this project. There’s nothing new here, so I’ll spare you the details. <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/create-project.html&quot;&gt;The documentation has all the details to get you started</a>.  <strong>Parallel testing works on batch builds. Make sure <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/batch-build.html&quot;&gt;to configure your project to run in batch</a></strong>.</p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/18/2025-03-18_14-53-45.png&quot;&gt;&lt;img class="aligncenter size-full wp-image-94361" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/18/2025-03-18_14-53-45.png&quot; alt="CodeBuild : create a batch build" width="822" height="937" /></a></p><p>Now, I’m ready to trigger an execution of the test suite. I can commit new code on my GitHub repository or trigger the build in the console.</p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/18/2025-03-18_18-17-19.png&quot;&gt;&lt;img class="aligncenter size-full wp-image-94360" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/18/2025-03-18_18-17-19.png&quot; alt="CodeBuild : trigger a new build" width="1547" height="295" /></a></p><p>After a few minutes, I see a status report of the different steps of the build; with a status for each test environment or shard.</p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/21/2025-03-21_11-23-04b-1.png&quot;&gt;&lt;img class="aligncenter wp-image-94494 size-full" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/21/2025-03-21_11-23-04b-1.png&quot; alt="CodeBuild: status" width="1412" height="1209" /></a></p><p>When the test is complete, I select the <strong>Reports</strong> tab to access the merged test reports.</p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/18/2025-03-18_16-05-06.png&quot;&gt;&lt;img class="aligncenter size-full wp-image-94363" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/18/2025-03-18_16-05-06.png&quot; alt="CodeBuild: test reports" width="1558" height="759" /></a></p><p>The <strong>Reports</strong> section aggregates all test data from all shards and keeps the history for all builds. I select my most recent build in the <strong>Report history</strong> section to access the detailed report.</p><p><a href="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/21/2025-03-21_11-23-24.png&quot;&gt;&lt;img class="aligncenter wp-image-94495 size-full" src="https://d2908q01vomqb2.cloudfront.net/da4b9237bacccdf19c0760cab7aec4a8359010b0/2025/03/21/2025-03-21_11-23-24.png&quot; alt="CodeBuild: Test Report" width="1422" height="1191" /></a></p><p>As expected, I can see the aggregated and the individual status for each of my 1,800 test cases. In this demo, they’re all passing, and the report is green.</p><p>The 1,800 tests of the demo project take one second each to complete. When I run this test suite sequentially, it took 35 minutes to complete. When I run the test suite in parallel on ten compute environments, it took 6 minutes to complete, including the time to provision the environments. <strong>The parallel run took 17.9 percent of the time of the sequential run</strong>. Actual numbers will vary with your projects.</p><p><strong>Additional things to know<br /></strong> This new capability is compatible with all testing frameworks. <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/sample-parallel-test.html&quot;&gt;The documentation includes examples</a> for Django, Elixir, Go, Java (Maven), Javascript (Jest), Kotlin, PHPUnit, Pytest, Ruby (Cucumber), and Ruby (RSpec).</p><p>For test frameworks that don’t accept space-separated lists, <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/parallel-test-tests-run.html&quot;&gt;the <code>codebuild-tests-run</code> CLI provides a flexible alternative</a> through the <code>CODEBUILD_CURRENT_SHARD_FILES</code> environment variable. This variable contains a newline-separated list of test file paths for the current build shard. You can use it to adapt to different test framework requirements and format test file names.</p><p>You can further customize how tests are split across environments by writing your own sharding script and using the <code>CODEBUILD_BATCH_BUILD_IDENTIFIER</code> environment variable, which is automatically set in each build. You can use this technique to implement framework-specific parallelization or optimization.</p><p><strong>Pricing and availability<br /></strong> With parallel test execution, you can now complete your test suites in a fraction of the time previously required, accelerating your development cycle and improving your team’s productivity.</p><p>Parallel test execution is available on all <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-compute-types.html&quot;&gt;three compute modes offered by CodeBuild</a>: on-demand, reserved capacity, and <a href="https://aws.amazon.com/lambda/&quot;&gt;AWS Lambda</a> compute.</p><p>This capability is available today in all <a href="https://docs.aws.amazon.com/glossary/latest/reference/glos-chap.html#region&quot;&gt;AWS Regions</a> where CodeBuild is offered, with no additional cost beyond <a href="https://aws.amazon.com/codebuild/pricing/&quot;&gt;the standard CodeBuild pricing</a> for the compute resources used.</p><p>I invite you to try parallel test execution in CodeBuild today. Visit the <a href="https://docs.aws.amazon.com/codebuild/latest/userguide/parallel-test.html&quot;&gt;AWS CodeBuild documentation</a> to learn more and get started with parallelizing your tests.</p><a href="https://linktr.ee/sebsto&quot;&gt;— seb</a><p>PS: Here’s the prompt I used to create the demo application and its test suite: “I’m writing a blog post to announce codebuild parallel testing. Write a very simple python app that has hundreds of tests, each test in a separate test file. Each test takes one second to complete.”</p><hr /><p>How is the News Blog doing? Take this <a href="https://amazonmr.au1.qualtrics.com/jfe/form/SV_eyD5tC5xNGCdCmi&quot;&gt;1 minute survey</a>!</p><p><em>(This <a href="https://amazonmr.au1.qualtrics.com/jfe/form/SV_eyD5tC5xNGCdCmi&quot;&gt;survey&lt;/a&gt; is hosted by an external company. AWS handles your information as described in the <a href="https://aws.amazon.com/privacy/&quot;&gt;AWS Privacy Notice</a>. AWS will own the data gathered via this survey and will not share the information collected with survey respondents.)</em></p></section><aside id="Comments" class="blog-comments"><div data-lb-comp="aws-blog:cosmic-comments" data-env="prod" data-content-id="cf48437c-d36f-473d-9c11-ffc88cd9b664" data-title="Accelerating CI with AWS CodeBuild: Parallel test execution now available" data-url="https://aws.amazon.com/blogs/aws/accelerating-ci-with-aws-codebuild-parallel-test-execution-now-available/&quot;&gt;&lt;p data-failed-message="Comments cannot be loaded… Please refresh and try again.">Loading comments…</p></div></aside>

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

AWS CodeBuild 并行测试 CI/CD
相关文章