Code testing
In a Cloud Docker for Commerce development environment, you can use the integrated test suite in each Cloud Suite for Commerce package for application code testing.
Test resources
The test suites, which use the Codeception testing framework for PHP, provide acceptance tests to validate code intended for contribution to Commerce Cloud package repositories.
- Acceptance tests are in the package
src/Test/Functional/Acceptance
folder. - Test configuration is the
codeception.dist.yml
file in the package folder.
Use the following table to access the files and available tests for each Cloud package. The links use the develop
branch, so be sure to select the appropriate tag for your project:
Package | Test configuration link | Acceptance test link |
---|---|---|
ECE-tools | ||
Cloud components | ||
Cloud Docker | ||
Cloud patches |
For application testing, use the Magento Application Testing (MFTF) framework to run functional tests. See Application testing.
Prepare the Docker environment for testing
To set up and configure the test environment:
Clone the GitHub repository for the package to test.
Copied to your clipboardgit clone git@github.com:magento/<cloud-suite-package-repository>.gitFor example, clone the ECE-Tools package:
Copied to your clipboardgit clone git@github.com:magento/ece-tools.gitStop all services that use the following ports:
80
—varnish443
—web, tls3306
—Apache, mysql
Add the following hostname to your
/etc/hosts
file.Copied to your clipboard127.0.0.1 magento2.dockerAlternatively, you can run the following command to add it to the file:
Copied to your clipboardecho "127.0.0.1 magento2.docker" | sudo tee -a /etc/hostsAdd required project dependencies if you are testing the following packages.
For the
magento/magento-cloud-patches
package:Copied to your clipboardcomposer require "magento/magento-cloud-docker:^1.0.0" --no-updateFor the
magento/magento-cloud-components
package:Copied to your clipboardcomposer require "magento/magento-cloud-docker:^1.0.0" --no-updatecomposer require "magento/framework:*" --no-updatecomposer require "magento/module-store:*" --no-updatecomposer require "magento/module-url-rewrite:*" --no-update
Switch to the preferred PHP version for running tests.
Update the project dependencies.
Copied to your clipboardcomposer update
Add credentials
Add credentials to your Docker environment using any of the following methods:
- Use environment variables
- Load credentials from the environment configuration file
- Add variables directly to the test configuration file
To add credentials using environment variables:
Create environment variables for your GitHub authentication keys.
Copied to your clipboardexport REPO_USERNAME=your_public_keyCopied to your clipboardexport REPO_PASSWORD=your_private_keySome packages require a GitHub authentication during installation. Create an environment variable with your GitHub token that can be used to install these packages.
Copied to your clipboard`export GITHUB_TOKEN=your_github_token
To load credentials from the environment configuration file:
Run the following commands to write credentials to the
./.env
file:Copied to your clipboardecho "REPO_USERNAME=your_public_key" >> ./.envCopied to your clipboardecho "REPO_PASSWORD=your_private_key" >> ./.envEdit the
codeception.dist.yml
file.Copied to your clipboardparams:- tests/functional/configuration.dist.yml- env- .envAdd a dependency for the vlucas/phpdotenv package required to load the environment variables.
Copied to your clipboardcomposer require "vlucas/phpdotenv": "^3.0"
To add credentials directly to the test configuration file:
Open the
codeception.dist.yml
file in an editor.Replace the
%REPO_USERNAME%
,%REPO_PASSWORD%
, and%GITHUB_TOKEN%
placeholder values with your credentials:Copied to your clipboardmodules:config:Magento\CloudDocker\Test\Functional\Codeception\TestInfrastructure:...composer_magento_username: "%REPO_USERNAME%"composer_magento_password: "%REPO_PASSWORD%"composer_github_token: "%GITHUB_TOKEN%"...
Run tests
Before you run tests, you must prepare your Docker environment and update the test configuration file. See Prepare the test environment. By default, functional tests produce a short output. You can receive more detailed output by editing the codeception.dist.yml
test configuration file to set the printOutput:
property to true
.
Copied to your clipboardmodules:config:Magento\CloudDocker\Test\Functional\Codeception\TestInfrastructure:...printOutput: true...Magento\CloudDocker\Test\Functional\Codeception\Docker:...printOutput: true...
You can locate the test configuration file in the root directory for each package. See Test resources.
Run a specific test
Use the following command format to run a specific functional test:
Copied to your clipboard./vendor/bin/codecept run Acceptance <TestName>Cest
For example, the following test for magento/ece-tools
code verifies that the post-deploy task runs successfully.
Copied to your clipboard./vendor/bin/codecept run Acceptance PostDeployCest
Sample response:
Copied to your clipboardCodeception PHP Testing Framework v2.5.6Powered by PHPUnit 6.5.14 by Sebastian Bergmann and contributors.Running with seed:Acceptance Tests (1) --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------PostDeployCest: Test post deploy | {"ADMIN_EMAIL":"admin@example.com"}[Magento\MagentoCloud\Test\Functional\Robo\Tasks\GenerateDockerCompose] Running ./bin/ece-docker build:compose--mode=functional --php=7.2......✔ PostDeployCest: Test post deploy | {"ADMIN_EMAIL":"admin@example.com"} (210.41s)
You can see the available tests for each package in the package source files. See Test resources.
Run all tests
Use the following commands to run all available tests for each PHP version:
PHP 7.1
Copied to your clipboard./vendor/bin/codecept run -g php71 --stepsPHP 7.2
Copied to your clipboard./vendor/bin/codecept run -g php72 --stepsPHP 7.3
Copied to your clipboard./vendor/bin/codecept run -g php73 --stepsPHP 7.4
Copied to your clipboard./vendor/bin/codecept run -g php74 --stepsIf you are testing
magento/ece-tools
code, use the following command:Copied to your clipboard./vendor/bin/codecept run -x php71 -x php72 -x php73 -x php74 --steps
For help with Codeception command options, use the .vendor/bin/codecept run --help
command, or see the Codeception CLI documentation for the installed version.