

# OPS05-BP02 测试并验证更改
<a name="ops_dev_integ_test_val_chg"></a>

 部署的每一项更改都必须经过测试，避免在生产中出现错误。此最佳实践的重点是测试从版本控制到构件构建的更改。除应用程序代码更改外，测试还应该包括基础设施、配置、安全控制和操作程序。测试有多种形式，从单元测试到软件组件分析（SCA）等等。在软件集成和交付过程中，尽早进行测试可进一步确保构件质量。

 组织必须为所有的软件构件制定测试标准。自动化测试可以减少工作量，并避免人工测试的错误。但有些情况下，可能必须进行手动测试。开发人员必须能够访问自动化测试结果，以便创建反馈环路，提高软件质量。

 **期望结果：**软件更改在交付前经过测试。开发人员可以访问测试结果和验证结果。组织制定了适用于所有软件更改的测试标准。

 **常见反模式：**
+  在没有进行任何测试的情况下部署一项新软件更改。它无法在生产环境中运行，从而导致中断。
+  使用 AWS CloudFormation 部署新安全组，而没有在预生产环境中进行测试。这些安全组导致客户无法访问应用程序。
+  修改了一个方法，但没有进行单元测试。该软件在部署到生产环境中后无法运行。

 **建立此最佳实践的好处：**降低了软件部署更改的失败率。软件质量得到改进。开发人员提高了对其代码可行性的认识。可以放心地推出安全策略，支持组织实现合规性。可以提前测试自动扩缩策略更新等基础设施更改，从而满足流量需求。

 **在未建立这种最佳实践的情况下暴露的风险等级：**高 

## 实施指导
<a name="implementation-guidance"></a>

 作为持续集成实践的一部分，对从应用程序代码到基础设施的所有更改都进行测试。将公布测试结果，以便开发人员快速提供反馈。组织制定了测试标准，所有更改都必须通过该标准。

 利用 Amazon Q 开发者版的生成式人工智能的强大功能，提高开发人员的生产力和代码质量。Amazon Q 开发者版包括生成代码建议（基于大型语言模型）、制作单元测试（包括边界条件），以及通过检测和修复安全漏洞来增强代码安全性。

 **客户示例** 

 作为持续集成管道的一部分，AnyCompany Retail 对所有软件构件进行几种类型的测试。他们实行测试驱动型开发，因此所有软件都要进行单元测试。构件构建完毕后，他们会立即运行端到端测试。第一轮测试完成后，他们会运行静态应用程序安全扫描，寻找已知漏洞。在每个测试关口通过时，开发人员都会收到消息。所有测试均完成后，软件构件就会存储在构件库中。

### 实施步骤
<a name="implementation-steps"></a>

1.  与组织中的利益相关方合作，为软件构件制定测试标准。所有构件均应通过哪些标准测试？ 测试范围内是否必须包含合规性或治理要求？ 是否需要进行代码质量测试？ 测试完成后，需要通知谁？ 

   1.  [AWS 部署管道参考架构](https://pipelines.devops.aws.dev/)包含一个权威的测试类型列表，可作为集成管道的一部分对软件构件执行这些测试。

1.  根据软件测试标准，利用必要的测试来检测应用程序。每组测试应在 10 分钟内完成。测试应该作为集成管道的一部分运行。

   1.  使用 [Amazon Q 开发者版](https://docs.aws.amazon.com/amazonq/latest/qdeveloper-ug/what-is.html)，这是一款生成式人工智能工具，可以帮助创建单元测试案例（包括边界条件）、使用代码和注释生成函数以及实现已知算法。

   1.  使用 [Amazon CodeGuru Reviewer](https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/welcome.html) 来测试应用程序代码是否存在缺陷。

   1.  使用 [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 对软件构件执行测试。

   1.  [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 可以将软件测试编排到管道中。

## 资源
<a name="resources"></a>

 **相关最佳实践：**
+  [OPS05-BP01 使用版本控制](https://docs.aws.amazon.com/wellarchitected/latest/operational-excellence-pillar/ops_dev_integ_version_control.html) 
+  [OPS05-BP06 共享设计标准](https://docs.aws.amazon.com/wellarchitected/latest/operational-excellence-pillar/ops_dev_integ_share_design_stds.html) 
+  [OPS05-BP07 实施提高代码质量的实践](https://docs.aws.amazon.com/wellarchitected/latest/operational-excellence-pillar/ops_dev_integ_code_quality.html) 
+  [OPS05-BP10 完全自动化集成和部署](https://docs.aws.amazon.com/wellarchitected/latest/operational-excellence-pillar/ops_dev_integ_auto_integ_deploy.html) 

 **相关文档：**
+  [采用测试驱动型开发方法](https://docs.aws.amazon.com/prescriptive-guidance/latest/best-practices-cdk-typescript-iac/development-best-practices.html) 
+  [Accelerate your Software Development Lifecycle with Amazon Q](https://aws.amazon.com/blogs/devops/accelerate-your-software-development-lifecycle-with-amazon-q/) 
+  [Amazon Q Developer, now generally available, includes previews of new capabilities to reimagine developer experience](https://aws.amazon.com/blogs/aws/amazon-q-developer-now-generally-available-includes-new-capabilities-to-reimagine-developer-experience/) 
+  [The Ultimate Cheat Sheet for Using Amazon Q Developer in Your IDE](https://community.aws/content/2eYoqeFRqaVnk900emsknDfzhfW/the-ultimate-cheat-sheet-for-using-amazon-q-developer-in-your-ide) 
+  [Shift-Left Workload, leveraging AI for Test Creation](https://community.aws/content/2gBZtC94gPzaCQRnt4P0rIYWuBx/shift-left-workload-leveraging-ai-for-test-creation) 
+  [Amazon Q 开发人员中心](https://aws.amazon.com/developer/generative-ai/amazon-q/) 
+  [10 ways to build applications faster with Amazon CodeWhisperer](https://aws.amazon.com/blogs/devops/10-ways-to-build-applications-faster-with-amazon-codewhisperer/) 
+  [Looking beyond code coverage with Amazon CodeWhisperer](https://aws.amazon.com/blogs/devops/looking-beyond-code-coverage-with-amazon-codewhisperer/) 
+  [Best Practices for Prompt Engineering with Amazon CodeWhisperer](https://aws.amazon.com/blogs/devops/best-practices-for-prompt-engineering-with-amazon-codewhisperer/) 
+  [Automated AWS CloudFormation Testing Pipeline with TaskCat and CodePipeline](https://aws.amazon.com/blogs/devops/automated-cloudformation-testing-pipeline-with-taskcat-and-codepipeline/) 
+  [Building end-to-end AWS DevSecOps CI/CD pipeline with open source SCA, SAST, and DAST tools](https://aws.amazon.com/blogs/devops/building-end-to-end-aws-devsecops-ci-cd-pipeline-with-open-source-sca-sast-and-dast-tools/) 
+  [Getting started with testing serverless applications](https://aws.amazon.com/blogs/compute/getting-started-with-testing-serverless-applications/) 
+  [My CI/CD pipeline is my release captain](https://aws.amazon.com/builders-library/cicd-pipeline/) 
+  《[在 AWS 上练习持续集成和持续交付白皮书](https://docs.aws.amazon.com/whitepapers/latest/practicing-continuous-integration-continuous-delivery/welcome.html)》 

 **相关视频：**
+  [Implement an API with Amazon Q Developer Agent for Software Development](https://www.youtube.com/watch?v=U4XEvJUvff4) 
+  [Installing, Configuring, & Using Amazon Q Developer with JetBrains IDEs (How-to)](https://www.youtube.com/watch?v=-iQfIhTA4J0) 
+  [Mastering the art of Amazon CodeWhisperer - YouTube playlist](https://www.youtube.com/playlist?list=PLDqi6CuDzubxzL-yIqgQb9UbbceYdKhpK) 
+  [AWS re:Invent 2020: Testable infrastructure: Integration testing on AWS](https://www.youtube.com/watch?v=KJC380Juo2w) 
+  [AWS Summit ANZ 2021 - Driving a test-first strategy with CDK and test driven development](https://www.youtube.com/watch?v=1R7G_wcyd3s) 
+  [Testing Your Infrastructure as Code with AWS CDK](https://www.youtube.com/watch?v=fWtuwGSoSOU) 

 **相关资源：**
+  [AWS Deployment Pipeline Reference Architecture - Application](https://pipelines.devops.aws.dev/application-pipeline/index.html) 
+  [AWS Kubernetes DevSecOps 管道](https://github.com/aws-samples/devsecops-cicd-containers) 
+  [Run unit tests for a Node.js application from GitHub by using AWS CodeBuild](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/run-unit-tests-for-a-node-js-application-from-github-by-using-aws-codebuild.html) 
+  [Use Serverspec for test-driven development of infrastructure code](https://docs.aws.amazon.com/prescriptive-guidance/latest/patterns/use-serverspec-for-test-driven-development-of-infrastructure-code.html) 

 **相关服务：**
+  [Amazon Q 开发者版](https://aws.amazon.com/q/developer/) 
+  [Amazon CodeGuru Reviewer](https://docs.aws.amazon.com/codeguru/latest/reviewer-ug/welcome.html) 
+  [AWS CodeBuild](https://docs.aws.amazon.com/codebuild/latest/userguide/welcome.html) 
+  [AWS CodePipeline](https://docs.aws.amazon.com/codepipeline/latest/userguide/welcome.html) 