Custom fixtures location

Quality means doing it right when no one is looking. – Henry Ford

If you prefer, just like me, to keep your integration tests in module instead of dev/tests/integration/testsuite or you think that you might want to do that, then – for sure – at some stage, you will encounter problem with loading your custom fixtures from module’s test directory.

Fixtures are php scripts that help us to setup environment in repetitive way. For example, if we add

to the test’s docblock then for each test run script in customer.php will be executed. This particular script creates a new customer entity and saves in database. We can easily use all of existing fixtures in our own tests and this is actually my preferable way to populate database with required data for testing purposes.

However, sometimes you might wish to create your own fixture and this is where problem will show up if you do not keep your tests in dev/tests/integration/testsuite – Magento won’t be able to find your file.

Let’s assume that you have the following module directories structure:

– Vendor
— Module
— Test
—- Unit
—- Integration
—– someTest.php
—– _files
—— custom_fixture.php

And you want to use custom_fixture in someTest.php. You can’t do that simply by adding @magentoDataFixture Vendor/Module/_files/custom_fixture.php

Instead you have to create static public method as a custom data provider in your test class.

Then you will be able to use annotation in the following manner:

And that’s all. By doing this you will be able to keep your tests wherever you want without loosing ability to create your own fixtures.

Best luck!