6

A very good thing about Magento 2 is that we put together all files inside the module itself. Layout XML, Templates, Less/Sass, Blocks, Models, Helpers, API, SQL scripts and everything. It's important because when we enable, disable or even delete the module everything lays on a single directory.

However, when I add a @magentoDataFixture for an integration test, it says it should be placed on dev/tests/integration/testsuite directory, and not on the module itself. The more directories, the more difficult to maintain. It looks like M1 with app and skin directories where files became very far away from each other, in terms of tree directory.

Example:

/**
 * @magentoDataFixture MyNamespace/MyModule/_files/create_some_data_here.php
 */
class CustomIntegrationTestTest extends AbstractController
{

Magento will look up on the directory: dev/tests/integration/testsuite/MyNamespace/MyModule/_files/create_some_data_here.php

and not somewhere like this: app/code/MyNamespace/MyModule/Test/....../_files/create_some_data_here.php

Can @magentoDataFixture files be inside the module itself and not on default directory dev/tests/integration/testsuite?

2
1

Yes it can. You can pass a relative path starting from dev/tests/integration/testsuite and Magento will pickup the fixture.

If you have the fixture app/code/MyNamespace/MyModule/Test/_files/create_some_data_here.php you can use the following @magentoDataFixture annotation:

/**
 * @magentoDataFixture ../../../../app/code/MyNamespace/MyModule/Test/_files/create_some_data_here.php
 */
public function testSomething() {}
0

In case if test class has multiple assertions then it would be great to use it as a method because annotations use a relative path.

/**
 * @magentoDataFixture loadInitialFixtures
 */
public function testSomething() {}

/**
 * @magentoDataFixture loadInitialFixtures
 */
public function testSomethingOther() {}

/**
 * @magentoDataFixture loadInitialFixtures
 */
public function testSomethingSomeOther() {}


/**
* Loads some data creation scripts because annotations use a relative path
*/
public static function loadInitialFixtures(){
    require __DIR__ . '/../../_files/create_some_data_here.php';
}

/**
* Rollback some data creation scripts because annotations use a relative path
*/
public static function loadInitialFixturesRollback() {
    require __DIR__ . '/../../_files/create_some_data_here_rollback.php';
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.