PHPUnit not printing result in runner context


I’m implementing unit testing and CI in a project and I’m getting different behaviors in my local env and in the gitlab runner (I am using with the shared runners).

My local env is running in docker containers and I’m using the same images in gitlab CI.

When I run PHPUnit locally, it works and I get the normal output.
On the CI side, It doesn’t log anything. I have no clue why this is the case.
I might miss something about how gitlab CI works as it’s the first time I implement CI.

The only way I got it working in the runner is by running the PHPUnit command two time in a row. It logs the second time.

the docker images I use are webdevops/php-apache-dev:7.4 and mariadb:10.6

here is the releavent part of my gitlab-ci.yml file:

  stage: test
    GIT_CLONE_PATH: $CI_BUILDS_DIR/usr/project
    DOCUMENT_ROOT: $CI_BUILDS_DIR/usr/project/www
    - apt-get update && apt-get install -y mariadb-client
    - git clone /gabox
    - ln -s /gabox "$CI_BUILDS_DIR/gabox"
    - echo auto_prepend_file="" >> /opt/docker/etc/php/php.ini
    - mysql --user="$MARIADB_USER" --password="$MARIADB_PASSWORD" --host=mariadb "$MARIADB_DATABASE" < "$CI_BUILDS_DIR/gabox/sql/start.sql"
    - mysql --user="$MARIADB_USER" --password="$MARIADB_PASSWORD" --host=mariadb "$MARIADB_DATABASE" < "$CI_BUILDS_DIR/gabox/sql/fixtures.sql"
    - cd "$CI_BUILDS_DIR/gabox/phpunit/vendor/bin/"
    - ./phpunit --configuration "$CI_BUILDS_DIR/usr/coach-alex/tests/phpunit.xml" --bootstrap "$CI_BUILDS_DIR/gabox/tests/autoload.php" "$CI_BUILDS_DIR/usr/coach-alex/tests/" --filter 'ExampleTest'
    - ./phpunit --configuration "$CI_BUILDS_DIR/usr/coach-alex/tests/phpunit.xml" --bootstrap "$CI_BUILDS_DIR/gabox/tests/autoload.php" "$CI_BUILDS_DIR/usr/coach-alex/tests/"
    - if: $CI_COMMIT_BRANCH == "develop"

It works like that, but logs only the second time I execute PHPUnit.

Any idea on what causes that behavior ?

Thanks !

1 Like

Hi @Ilias

I would change your phpunit line to either:

    - ./$CI_BUILDS_DIR/gabox/phpunit/vendor/bin/phpunit  ... --coverage-text --colors=never --log-junit $CI_PROJECT_DIR/phpunit-report.xml
        when: always
            junit: $CI_PROJECT_DIR/phpunit-report.xml
        expire_in: 2 weeks

or alternatively just:

    - ./$CI_BUILDS_DIR/gabox/phpunit/vendor/bin/phpunit  ... --coverage-text --colors=never 

The version with the artifact will not print out the result of the unit tests, but when you look at your pipeline in GitLab you will see a tab called Tests with a number next to it, which tells you how many tests were run. This will show you the results of any tests, lints etc. run in the pipeline, that dump output in JUnit format.

You might also want to read the docs on test coverage settings and pipeline badges