You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何查看AWS CodeBuild生成的Python项目单元测试与覆盖率报告?

Great question! I’ve helped several teams replicate that Jenkins-style test reporting and historical tracking when moving to AWS CodeBuild. Here are the most practical, AWS-native (and some third-party) approaches to visualize your pytest results and coverage data:

1. AWS CloudWatch Metrics + Custom Dashboards (Native, No Extra Tools)

This is the simplest way to track historical trends for key metrics like test pass rate, failure count, and coverage percentage—just like Jenkins’ built-in graphs.

How to set it up:

  • Step 1: Extract metrics from your reports
    Write a small Python script (e.g., push_test_metrics.py) to parse your junit.xml and coverage.xml files. Use the standard xml.etree.ElementTree library to pull values like total tests, failed tests, and line coverage percentage.
  • Step 2: Push metrics to CloudWatch
    Use the AWS SDK (boto3) in your script to send these metrics to CloudWatch. Example snippet:
    import boto3
    import xml.etree.ElementTree as ET
    
    # Parse JUnit results
    junit_tree = ET.parse('reports/junit.xml')
    test_suite = junit_tree.getroot()
    total_tests = int(test_suite.attrib['tests'])
    failed_tests = int(test_suite.attrib['failures']) + int(test_suite.attrib['errors'])
    
    # Parse coverage results
    coverage_tree = ET.parse('reports/coverage.xml')
    coverage_rate = float(coverage_tree.getroot().attrib['line-rate']) * 100
    
    # Push to CloudWatch
    cloudwatch = boto3.client('cloudwatch')
    cloudwatch.put_metric_data(
        Namespace='CodeBuild/PythonTests',
        MetricData=[
            {'MetricName': 'TotalTests', 'Value': total_tests},
            {'MetricName': 'FailedTests', 'Value': failed_tests},
            {'MetricName': 'LineCoveragePercent', 'Value': coverage_rate}
        ]
    )
    
  • Step 3: Add the script to your buildspec.yml
    Include the script in your post_build phase, along with uploading raw reports to S3 for reference:
    phases:
      install:
        commands:
          - pip install pytest pytest-cov boto3
      build:
        commands:
          - pytest tests/ --junitxml=reports/junit.xml --cov=src --cov-report=xml:reports/coverage.xml --cov-report=html:reports/coverage-html
      post_build:
        commands:
          - python push_test_metrics.py
          - aws s3 cp reports/ s3://your-report-bucket/${CODEBUILD_PROJECT_NAME}/${CODEBUILD_BUILD_ID}/ --recursive
    
  • Step 4: Build a CloudWatch Dashboard
    Go to the CloudWatch console, create a new dashboard, and add widgets for your metrics:
    • A line chart for LineCoveragePercent to track coverage over time
    • A stacked bar chart for TotalTests vs FailedTests to see pass rate trends
      You can even add links to the raw S3 reports directly in the dashboard for deep dives.
2. S3 Static Hosting + Open-Source Report Viewers (Detailed Report Access)

If you want to view full, interactive test reports (like Jenkins’ HTML test results), you can host the pytest-generated HTML coverage reports and use a JUnit viewer to render XML results as HTML.

How to set it up:

  • Step 1: Upload reports to S3
    As above, use aws s3 cp in your post_build phase to upload the reports/ folder (including coverage-html and junit.xml) to S3. Organize them by build ID or git commit hash for easy history tracking.
  • Step 2: Enable S3 static website hosting
    Turn on static hosting for your S3 bucket, and set the index document to something like index.html.
  • Step 3: Add a JUnit viewer
    Use an open-source tool like junit-viewer to convert your junit.xml to HTML during the build. Install it via npm (add npm install -g junit-viewer to your install phase), then run:
    junit-viewer --results=reports/junit.xml --save=reports/test-results.html
    
    Now your S3 bucket will have both the coverage HTML and a formatted test results page for every build. You can create a simple index page that lists all builds with links to their reports, or use S3’s built-in bucket browsing (enable it in permissions).
3. Third-Party Tools like Codecov (Professional Coverage Tracking)

If you want advanced features like pull request coverage comparisons, detailed code-level coverage history, and team notifications, integrate with a tool like Codecov.

How to set it up:

  • Step 1: Install Codecov in your build
    Add pip install codecov to your install phase.
  • Step 2: Upload coverage reports
    After running pytest, add this command to your build phase:
    codecov --file=reports/coverage.xml
    
  • Step 3: Link to your CodeBuild project
    Connect your CodeBuild project to Codecov via your AWS IAM credentials or a Codecov token. Codecov will automatically track coverage across builds, generate historical graphs, and even show coverage changes in pull requests.
Bonus: Combine Approaches for Full Visibility

For the best of both worlds:

  • Use CloudWatch for high-level trend tracking in your AWS console
  • Use S3 static hosting for accessing detailed, per-build test and coverage reports
  • Use Codecov if you need PR-specific coverage analysis and team collaboration tools

内容的提问来源于stack exchange,提问作者Brian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:59:11