Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/_internal-phpunit-report.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test PHPUnit report action

on:
push:
branches:
- main
paths:
- ".github/workflows/_internal-phpunit-report.yaml"
- "phpunit-report/**"
- "!(**/*.md)"
pull_request:
branches:
- main
paths:
- ".github/workflows/_internal-phpunit-report.yaml"
- "phpunit-report/**"
- "!(**/*.md)"

jobs:
phpunit-report:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Run the report script's tests
run: python3 -m unittest discover -s phpunit-report/test -v

- name: Summarize the test fixtures
uses: ./phpunit-report
with:
path: phpunit-report/test/fixtures
title: PHPUnit report of the test fixtures
24 changes: 23 additions & 1 deletion .github/workflows/nx-integration-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -153,4 +153,26 @@ jobs:
working-directory: ./main
run: |
export WARDEN="$(dirname $(pwd))/warden/bin/warden"
nx affected --target=test:integration --head=HEAD --base=remotes/origin/${{ inputs.pr_base }} --parallel=1
# Stream each module's PHPUnit output while it runs. Nx otherwise prints it only
# after the module finished, so a cancelled or timed-out job shows nothing.
nx affected --target=test:integration --head=HEAD --base=remotes/origin/${{ inputs.pr_base }} --parallel=1 --output-style=stream-without-prefixes

- name: Upload Integration Test Reports
id: upload-reports
if: always()
uses: actions/upload-artifact@v4
with:
# Artifact names must be unique per run and cannot contain the ":" of the service versions
name: integration-test-reports-${{ strategy.job-index }}
path: main/phpunit-output/
if-no-files-found: ignore
retention-days: 14

- name: Summarize Integration Test Results
if: always()
uses: mage-os/github-actions/phpunit-report@main
with:
path: main/phpunit-output/teamcity
title: Integration tests
# The summary links the reports when it leaves failures out
reports-url: ${{ steps.upload-reports.outputs.artifact-url }}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@ Opinionated Github Actions and Workflows to make building, testing, and maintain
| [Semver Compare](./semver-compare/README.md) | A Github Action that semantically compares two versions |
| [Supported Version](./supported-version/README.md) | A Github Action that computes the currently supported Github Actions Matrix for Magento 2 |
| [Setup Install](./setup-install/README.md) | A Github Action that runs `bin/magento setup:install` from the supported-version services matrix |
| [PHPUnit Report](./phpunit-report/README.md) | A Github Action that summarizes PHPUnit TeamCity logs in the job summary |
2 changes: 1 addition & 1 deletion nx-integration-tests-setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ runs:
run: |
npm run generate-workspace -- --commands=test:unit,test:integration \
--test:unit='if [ -d {{ MODULE_PATH }}Test/Unit ]; then ${WARDEN} env exec -T php-fpm ./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml {{ MODULE_PATH }}Test/Unit; else echo "{{ MODULE_NAME }} has no unit test; fi' \
--test:integration='${WARDEN} env exec -T --workdir /var/www/html/dev/tests/integration php-fpm ../../../vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite '"'"'Magento Integration Tests Real Suite'"'"' --filter='"'"'/Magento/{{ MODULE_DIRECTORY }}/|Magento\\{{ MODULE_DIRECTORY }}'"'"' --log-junit=../../../phpunit-output/junit/{{ MODULE_DIRECTORY }}.xml'
--test:integration='${WARDEN} env exec -T --workdir /var/www/html/dev/tests/integration php-fpm ../../../vendor/bin/phpunit --configuration phpunit.xml.dist --testsuite '"'"'Magento Integration Tests Real Suite'"'"' --filter='"'"'/Magento/{{ MODULE_DIRECTORY }}/|Magento\\{{ MODULE_DIRECTORY }}'"'"' --log-junit=../../../phpunit-output/junit/{{ MODULE_DIRECTORY }}.xml --log-teamcity=../../../phpunit-output/teamcity/{{ MODULE_DIRECTORY }}.log'

- name: Print Affected
working-directory: ./main
Expand Down
82 changes: 82 additions & 0 deletions phpunit-report/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# PHPUnit Report

A GitHub Action that turns PHPUnit TeamCity logs into a job summary. It lists the failing tests of each suite with their message, and adds error annotations for the first failures.

It reads the TeamCity log (`phpunit --log-teamcity`) instead of JUnit. PHPUnit writes the TeamCity log while the tests run, and the JUnit log only at the end, so only the TeamCity log keeps the failures of a run that PHPUnit aborted. Such a suite is marked as ended early.

The action only reports; it never fails the job. Run it with `if: always()` after the step that runs the tests.

## Inputs

See the [action.yml](./action.yml)

| Input | Description | Required | Default |
|-------------|----------------------------------------------------------------------------------------------------------------------------------|----------|-------------------|
| path | Directory with one TeamCity log per suite, named `<suite>.log`. The file name is the suite's name. | true | |
| title | Heading of the summary | false | 'PHPUnit results' |
| reports-url | Link to the full test reports, such as the `artifact-url` output of `actions/upload-artifact`. Shown when failures are left out. | false | '' |

## Usage

```yml
- name: Run tests
run: vendor/bin/phpunit --log-teamcity phpunit-output/teamcity/Unit.log

- name: Upload test reports
id: reports
if: always()
uses: actions/upload-artifact@v4
with:
name: test-reports
path: phpunit-output/

- name: Summarize test results
if: always()
uses: mage-os/github-actions/phpunit-report@main
with:
path: phpunit-output/teamcity
title: Unit tests
reports-url: ${{ steps.reports.outputs.artifact-url }}
```

The summary starts with a headline, then lists the failed tests of each failing suite, then a table of all suites (collapsed when there are more than 10):

> :x: 2 of 3 suites failed: 103 failed tests, 1 suite ended early.
>
> <details><summary><b>Catalog</b>: 98 failed tests, ended early</summary>
>
> PHPUnit stopped before the suite finished. The job log shows why.
>
> | Test | Message |
> |---|---|
> | `Magento\Catalog\Controller\Category\CategoryUrlRewriteTest::testCategoryUrlOnStoreView` | Unable to apply fixture: Magento/Store/_files/store.php |
>
> </details>
>
> <details><summary><b>Sales</b>: 5 failed tests</summary>
>
> | Test | Message |
> |---|---|
> | `Magento\Sales\Cron\CleanExpiredQuotesTest::testExecute` | Failed asserting that 3 matches expected 2. |
>
> </details>
>
> | Suite | Tests | Failed | Skipped | Result |
> |---|---:|---:|---:|---|
> | Catalog | 2195 | 98 | 107 | :warning: ended early (PHPUnit aborted or the job stopped), see the job log |
> | Customer | 512 | 0 | 12 | :white_check_mark: passed |
> | Sales | 681 | 5 | 39 | :x: failed |

The first failures also get error annotations, which point at the test or fixture file when the stack trace names one.

A suite lists at most 100 failures, and the summary stops listing them at 512 KiB: GitHub drops a step summary larger than 1 MiB. The summary then points to the `reports-url`.

A suite with an empty log is shown as without results, not as passed. PHPUnit leaves the log empty when no test matched, and when it stopped while loading the tests, for example on a syntax error. When its bootstrap fails, PHPUnit writes no log at all, so that suite is missing from the summary. The job log shows what happened in both cases.

## Development

The script needs only Python 3, which GitHub-hosted runners provide. Run its tests with:

```sh
python3 -m unittest discover -s phpunit-report/test
```
31 changes: 31 additions & 0 deletions phpunit-report/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: "PHPUnit Report"
author: "Mage-OS"
description: "Summarize PHPUnit TeamCity logs in the job summary and annotate the first failures, also for runs that PHPUnit aborted."

inputs:
path:
description: "Directory with one PHPUnit TeamCity log (phpunit --log-teamcity) per suite, named <suite>.log"
required: true
title:
description: "Heading of the summary"
required: false
default: "PHPUnit results"
reports-url:
description: "Link to the full test reports, such as the artifact-url output of actions/upload-artifact. The summary points to it when it leaves failures out."
required: false
default: ""

runs:
using: "composite"
steps:
- name: Summarize PHPUnit results
shell: bash
env:
REPORT_PATH: ${{ inputs.path }}
REPORT_TITLE: ${{ inputs.title }}
REPORTS_URL: ${{ inputs.reports-url }}
run: python3 "${GITHUB_ACTION_PATH}/report.py" "${REPORT_PATH}" "${REPORT_TITLE}" "${REPORTS_URL}"

branding:
icon: "check-square"
color: "green"
Loading
Loading