GitHub Test Reporter

repository·main·Indexed 18 days ago

https://github.com/ctrf-io/github-test-reporter

A tool for integrating detailed test results into GitHub Actions workflows. It enables developers to view summaries, failure analyses, flaky test detection, and AI-driven insights within job summaries, Pull Request comments, and status checks. Supports CTRF and JUnit JSON formats, custom Handlebars templates, and community-built reports like cobra-report, failed-detailed, and summary-short.

Tokens
27.8K
Snippets
107
Records
126
Agent score
63%

What's inside github-test-reporter

  1. Important considerations for cobra-report

    main

    When using the cobra-report template, keep the following in mind:

    • JSON Structure: Your test results JSON files must conform to the expected CTRF format so the template can correctly parse summary values and test statuses.
    • Custom Helpers: The template relies on specific helpers (getCtrfEmoji, formatMessage, and anyFailedTests) being available in the environment.
    • Conditional Rendering: The detailed failure table is only rendered if at least one test has a failed status.
  2. Access GitHub properties in custom reports

    main

    When building custom reports, you can access GitHub-specific metadata using the github property within your template. This allows you to inject workflow information, repository details, pull request data, and sender information directly into your report.

    Commonly used properties include:

    • Workflow/Run Info: github.workflow, github.runId, github.runNumber.
    • Repository Info: github.repository.name, github.repository.fullName, github.repository.htmlUrl.
    • Pull Request Info: github.pullRequest.number, github.pullRequest.title, github.pullRequest.state (available during PR events).
    • User Info: github.actor, github.sender.login.

    Note: The availability of certain properties depends on the event that triggered the workflow. For example, github.pullRequest is only populated during pull request events.

    <!-- Example usage in a template -->
    <h1>Report for {{github.repository.fullName}}</h1>
    <p>Workflow: {{github.workflow}} (Run #{{github.runNumber}})</p>
  3. Install github-actions-ctrf locally in a Node.js project

    main

    If you prefer to manage the reporter as a project dependency, install it via npm. Once installed, you can execute it directly from your node_modules or by defining a script in your package.json.

    # Install the package
    npm install github-actions-ctrf
    
    # Run via node_modules path
    ./node_modules/.bin/github-actions-ctrf path-to-your-ctrf-report.json
  4. Create a custom summary using Handlebars templates

    main

    You can define how the GitHub Actions summary or Pull Request comment is presented by using a Handlebars template. This allows you to include custom markdown and leverage data from your CTRF report and GitHub properties for dynamic output.

    To use the custom summary method, you must provide two arguments to the command:

    1. The path to your CTRF report JSON file.
    2. The path to your Handlebars (.hbs) template file.

    In a GitHub Actions workflow, use the custom command as shown below:

    - name: Publish CTRF Custom summary
      run: |
        npx github-actions-ctrf custom path-to-your-ctrf-report.json path-to-your-handlebars-template.hbs
      if: always()
  5. Post CTRF results as Pull Request comments

    main

    You can post test results directly as a comment on a Pull Request.

    Standard PR Comment

    Use the pull-request command. This requires the GITHUB_TOKEN environment variable with write permissions for pull requests.

    - name: Publish CTRF pull request comment
      run: npx github-actions-ctrf pull-request path-to-your-ctrf-report.json
      if: always()
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    Using a Specific View in PR Comments

    You can combine the pull-request command with any view command using the --pull-request flag. For example, to post a flaky-rate view:

    npx github-actions-ctrf flaky-rate ctrf-report.json --pull-request

    Comment Management Options

    • --on-fail-only: Post the comment only if there are failed tests.
    • --update-comment: If true, appends the new report to an existing tagged comment instead of creating a new one.
    • --overwrite-comment: If true, replaces the entire content of an existing tagged comment.
    • --comment-tag <string>: A unique identifier used to find, update, or overwrite specific comments (e.g., using ${{ github.workflow }}-${{ github.job }}).
  6. Debug available GitHub properties in your workflow

    main

    To see all available properties for your specific workflow run, you can print the entire github context to your workflow logs using jq. This is useful for discovering the exact structure of the github.context object and other properties available to your template.

    Add this step to your GitHub Actions workflow file:

    - name: Print GitHub Context
      env:
        CONTEXT: ${{ toJson(github) }}
      run: echo "$CONTEXT" | jq .
  7. Use community-built reports

    main

    You can leverage pre-built reports from the community by setting community-report: true and specifying a community-report-name (e.g., summary-short).

    - name: Publish Test Report
      uses: ctrf-io/github-test-reporter@v1
      with:
        report-path: './ctrf/*.json'
        community-report: true
        community-report-name: summary-short
      if: always()
  8. Enable the GitHub Report

    main

    The GitHub Report provides a test report that follows the GitHub Design System, using GitHub icons and color schemes for a native look and feel. It includes a summary of results and expandable sections for failed, flaky, skipped, and pending tests, along with git and action context.

    - name: Publish Test Report
      uses: ctrf-io/github-test-reporter@v1
      with:
        report-path: './ctrf/*.json'
        github-report: true
      if: always()
  9. Export processed CTRF reports to a file

    main

    If you need to process the enriched CTRF report further, or if your reports are larger than 1MB, use the write-ctrf-to-file input. This exposes the internal processed CTRF report that has been enriched with properties used by the GitHub Test Reporter.

    - name: Write CTRF to File
      uses: ctrf-io/github-test-reporter@v1
      with:
        report-path: './ctrf/*.json'
        write-ctrf-to-file: './ctrf/ctrf-report.json'
      if: always()
  10. Generate a Summary Delta Report

    main

    The Summary Delta Report provides a concise table overview of test statuses (passed, failed, skipped, flaky, etc.) and includes a comparison to a baseline. This allows you to see changes (increases or decreases) in test counts and duration compared to previous results.

    - name: Publish Test Report
      uses: ctrf-io/github-test-reporter@v1
      with:
        report-path: './ctrf/*.json'
        summary-delta-report: true
      if: always()