Run tests for a specific module in Magento2

43

14

Running the tests for Magento to using php bin/magento dev:tests:run takes a lot of time.
Is there a way to run the tests for a specific module only?

I'm a noob in (unit) testing so have mercy.

Marius

Posted 2015-10-15T12:58:28.490

Reputation: 150 497

Have you tried looking at what the command does? I mean, in its code, perhaps it takes another argument (or list of). – Julien Lachal – 2015-10-15T13:21:15.400

1@JulienLachal. Tried that. It only accepts one argument (type) that can be all, unit, integration, integration-all, static, static-all, integrity, legacy, default. And some general options like -h, -q, ... that are available for all commands. So nothing special about them – Marius – 2015-10-15T13:23:38.280

Answers

43

Magento 2 use phpunit framework for running test. You can run test per type and module with command:

{phpunit_executable} -c {Magentoroot}/dev/test/{type}/phpunit.xml[.dist] path/to/module/test/dir

For example:

  • to run unit test for Catalog, you need run command

    vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magento/Catalog/Test/Unit/

  • to run integration test for Catalog, you need run vendor/phpunit/phpunit/phpunit -c dev/tests/integration/phpunit.xml.dist dev/tests/integration/testsuite/Magento/Catalog

Currently only unit test moved to modules. Also static and integration integrity test is working with all magento code.

KAndy

Posted 2015-10-15T12:58:28.490

Reputation: 13 549

So generally speaking the whole automated testing is still a mess even though they officially released 2.0.0? – peedee – 2015-12-04T03:01:34.910

@KAndy,
I have run catalog module unit test, but cli displays error like this, Fatal error: Call to undefined function Magento\Catalog\Block\Adminhtml\Product\Edit\Action\Attribute\Tab__() in /var/www/html/magento2/vendor/magento/module-catalog/Block/Adminhtml/Product/Edi‌​t/Action/Attribute/Tab/Inventory.php on line 89. In core module error are display.what is the cause of error. plz explain it.
– Rakesh Jesadiya – 2015-12-14T09:16:42.023

@RakeshJesadiya, looks like your bootstrap file do not include app/functions.php where function __() defined – KAndy – 2015-12-14T17:29:47.857

Yeah.. for me it is not running at all on 2.0.2 when i run "vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magento/Catalog/Test/Unit/" in magento root directory it gives me "PHPUnit 4.1.0 by Sebastian Bergmann. Cannot open file "app/code/Magento/Catalog/Test/Unit/.php"." ... – Lachezar Raychev – 2016-02-11T13:04:22.857

1@LachezarRaychev The Magento core codes is not in app/code/Magento path. Its moved to vendor/magento. Please try this command php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist vendor/magento/module-catalog/ – Franc – 2016-02-21T16:06:47.640

13

kandy's answer seams like the way to go, but in the mean time I found a quick and dirty way to do it.

Copy dev/tests/unit/phpunit.xml.dist to dev/tests/unit/phpunit.xml and replace

<directory suffix="Test.php">../../../app/code/*/*/Test/Unit</directory>

with

<directory suffix="Test.php">../../../app/code/[Namespace]/[Module]/Test/Unit</directory>  

and comment out the other directory tags inside the <testsuite> tag.

When you are done, revert your changes or remove phpunit.xml.

Marius

Posted 2015-10-15T12:58:28.490

Reputation: 150 497

hello marius, how to do unit test using cli for catalog module?which command we have to run?base path is magento2/bin? – Rakesh Jesadiya – 2015-12-12T12:43:05.560

It's in the answer from KAndy. vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist app/code/Magento/Catalog/Test/Unit/ – Marius – 2015-12-12T12:59:27.453

1@Rakesh :: please try this updated command. php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.dist vendor/magento/module-catalog/ Magento's core codes is moved to vendor/magento path. – Franc – 2016-02-21T16:08:38.117

how to run unit test for custom module using : php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.disvendor/magento/module-catalog/ – Sachin S – 2016-06-21T11:26:08.937

@SachinS. Just as I described. temporarily edit the phpunit.xml file and put the path to the module you want to test. – Marius – 2016-06-21T11:27:47.420

php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml app/code/Tagalys/Tglssearch i get no tests executed – Sachin S – 2016-06-21T11:35:44.220

My edited phpunit.xml is <directory suffix="Test.php">../../../app/code/Tagalys/Tglssearch/Test/Unit</directory> but what is Test/Unit – Sachin S – 2016-06-21T11:44:02.093

2

just to add that by using Phpstorm IDE you can define Test configuration for your module and run it separately. Running Tests in PhpStorm

Amin Memariani

Posted 2015-10-15T12:58:28.490

Reputation: 352