Unit testing in magento 2

Check your magento unit tests at every push using magento-actions:

name: m2-actions-test

on: [push]

jobs:
  magento2-build:
    runs-on: ubuntu-latest
    container: ubuntu
    name: 'm2 tests & build'
    services:
      mysql:
        image: docker://mysql:8.0
        env:
          MYSQL_ROOT_PASSWORD: magento
          MYSQL_DATABASE: magento
        ports:
          - 3306:3306
        options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3
      elasticsearch:
        image: docker://elasticsearch:7.1.0
        ports:
          - 9200:9200
        options: -e="discovery.type=single-node" --health-cmd="curl http://localhost:9200/_cluster/health" --health-interval=10s --health-timeout=5s --health-retries=10
    steps:
    - uses: actions/checkout@v1
      with:
        submodules: recursive
 
    - name: 'launch magento2 unit test'
      if: always()
      uses: MAD-I-T/magento-actions@v3.10
      env:
        COMPOSER_AUTH: ${{secrets.COMPOSER_AUTH}}
      with:
        process: 'unit-test'
        elasticsearch: 1

To run unit tests in a specific location, one can use the unit_test_subset_path

- name: 'This step will execute specific unit tests in the path dir'
  uses: MAD-I-T/magento-actions@v3.10
  env:
    COMPOSER_AUTH: ${{secrets.COMPOSER_AUTH}}
  with:
    process: 'unit-test'
    elasticsearch: 1
    unit_test_subset_path: 'vendor/magento/module-email/Test/Unit'

To run magento unit tests with custom config specify the path to the new config file through unit_test_config argument.

For example copy the default config dev/tests/unit/phpunit.xml.dist to dev/tests/unit/phpunit.xml and adapt the config to your liking. Then commit the later and call the magento-actions as follows:

    - name: 'launch magento2 unit test'
      if: always()
      uses: MAD-I-T/magento-actions@master
      env:
        COMPOSER_AUTH: ${{secrets.COMPOSER_AUTH}}
      with:
        process: 'unit-test'
        elasticsearch: 1
        unit_test_config: 'dev/tests/unit/phpunit.xml'
        unit_test_subset_path: 'vendor/magento/module-email/Test/Unit'

Custom unit testing process

1 Like