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

EDIT: Contrary to what is said in the video, one can unit test the third party module whitout neccessarily override the unit-testing script e.g:

      - name: 'launch magento2 unit test'
        uses: MAD-I-T/magento-actions@master
        env:
          COMPOSER_AUTH: ${{secrets.COMPOSER_AUTH}}
        with:
          process: 'unit-test'
          unit_test_config: 'dev/tests/unit/phpunit.xml.dist'
          unit_test_subset_path: '../Madit/Sips2'
          elasticsearch: 1

In which case you just have to specify the path of your code to test here the code is one level above the magento root project so the unit testing is done from the code in the parent dir ../Madit/Sips2

1 Like