52

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.

2
  • Have you tried looking at what the command does? I mean, in its code, perhaps it takes another argument (or list of). Commented Oct 15, 2015 at 13:21
  • 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
    Commented Oct 15, 2015 at 13:23

3 Answers 3

53

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.

6
  • So generally speaking the whole automated testing is still a mess even though they officially released 2.0.0?
    – peedee
    Commented Dec 4, 2015 at 3:01
  • @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. Commented Dec 14, 2015 at 9:16
  • @RakeshJesadiya, looks like your bootstrap file do not include app/functions.php where function __() defined
    – KAndy
    Commented Dec 14, 2015 at 17:29
  • 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"." ... Commented Feb 11, 2016 at 13:04
  • 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
    Commented Feb 21, 2016 at 16:06
15

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.

7
  • hello marius, how to do unit test using cli for catalog module?which command we have to run?base path is magento2/bin? Commented Dec 12, 2015 at 12:43
  • 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
    Commented Dec 12, 2015 at 12:59
  • 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
    Commented Feb 21, 2016 at 16:08
  • how to run unit test for custom module using : php vendor/phpunit/phpunit/phpunit -c dev/tests/unit/phpunit.xml.disvendor/magento/module-catalog/
    – Sushivam
    Commented Jun 21, 2016 at 11:26
  • @SachinS. Just as I described. temporarily edit the phpunit.xml file and put the path to the module you want to test.
    – Marius
    Commented Jun 21, 2016 at 11:27
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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