How do Magento2 Integration and Functional Test relate to composer vendor folder?
-
11-10-2020 - |
سؤال
I want to write a full test suite for my new module. Unit tests are no problem as they can be tested in isolation, and therefore can be in a tests folder within the package.
However Integration and Functional tests act on the whole application, the Magento core has these at root level, so to my question.
So if we are to have the tests controlled by a composer package how do we get them into a root level folder?
I have found nothing in the documentation on what best practice is. Are third party module developers only writing Unit Tests?
I'm not aiming to distribute my module so I could put my Unit tests in the package and put Integration and Functional Tests at root level and source control them with the project. Is this the best plan - how are others doing it?
المحلول
I've described it in another question for integration tests: Creating Integration Tests for Magento 2 Modules
I've successfully placed my integration tests into a separate directory:
src/My/Module/test/integration
. It could be any other directory as well, likeapp/code/My/Module/Test
.Add them as new test suite to the Magento integration tests: Copy
dev/tests/integration/phpunit.xml.dist
todev/tests/integration/phpunit.xml
and add the following in the<testsuites>
node:<testsuite name="My_Module"> <directory suffix="Test.php">../../../src/My/Module/test</directory> </testsuite>
Then run the tests like this from the
dev/tests/integration
directory:../../../vendor/bin/phpunit --testsuite "My_Module"
With the
--testsuite
parameter you can select one test suite by name, so that not all integration tests are run at once
And for fixtures:
To use own fixtures, a little workaround was necessary, because in
Magento\TestFramework\Annotation
the fixture base directory is defined globally. But fortunately Magento allows method names as fixtures as well, so the following works:/** * @magentoDataFixture loadFixture */ public function testSomething() { } public static function loadFixture() { include __DIR__ . '_files/something_fixture.php'; }
You can use wild cards in the test suite definition, so for tests for any project specific modules in app/code
, it can also look like this:
<directory suffix="Test.php">../../../app/code/*/*/Test</directory>
For functional tests it works in the same way.