In Magento 2 documentation "Running Unit Tests in the CLI" is text:
To run all tests, navigate to the Magento base directory and execute the following command:
$ ./vendor/bin/phpunit -c dev/tests/unit/phpunit.xml.dist
My question is: How can I run unit tests or integration tests without navigating to specific Magento directory?
So basically I would like to know the reason, why I have to be in specific directory to run phpunit commands in CLI? (I want to execute the command from any location)
EDITED:
Why I can not use full path to the vendor/bin/phpunit directory?
<path to Magento base directory>/vendor/bin/phpunit -c dev/tests/unit/phpunit.xml
ERROR THAT IT RETURNS: Could not read "dev/tests/unit/phpunit.xml". Why the error appears?
Example: why I can not run /var/www/magento2/vendor/bin/phpunit -c dev/tests/unit/phpunit.xml when I am situated in /var/www/magento2/var/log $ location or anywhere else in the file system?
Reason: I want to create an app that allows me to speed up my development process. (I am not interested IDE-s) and I want to understand the reason why I have to navigate to the Magento base directory.
-
I will add this link to other users here: Chapter 3. The Command-Line Test Runneruser3748173– user37481732017年06月28日 10:41:59 +00:00Commented Jun 28, 2017 at 10:41
1 Answer 1
PHPUnit is installed by Composer, so it's executable is in the /vendor/bin directory. If you want to be able to run that program from anywhere in the command line, you need to add it to your path. Assuming you don't already have another version of phpunit installed globally, there are a couple of ways to do this:
If you run
export PATH=$PATH:/var/www/magento2/vendor/binit will only last for the length of the session.You can permanently change it by adding that line to your
~/.bashrcfile:export PATH=$PATH:/var/www/magento2/vendor/binI think you can also create a symlink to
phpunitto the/bindirectory, but it's not best practice: https://unix.stackexchange.com/questions/116508/adding-to-path-vs-linking-from-binInstall
phpunitglobally another way (PEAR?), and don't use the version that M2 installs via Composer.
So now you can run Magento's phpunit anywhere. But you still need to pass the correct configuration to it.
Each test suite in Magento (unit, functional, etc) has a slightly different configuration, which is why you might pass it in via the -c dev/tests/unit/phpunit.xml.dist parameter, as you did in your example.
But if you rename phpunit.xml.dist to phpunit.xml, and if you run phpunit in the dev/tests/unit/ directory, it should automatically use the phpunit.xml file in that directory. This saves some typing.
What I would recommend is adding vendor/bin to your PATH, so you can run phpunit anywhere, then cd in to the test folder you want to run, rename the config files phpunit.xml, run your tests suites in those directories, for the shortest command.
You will still need to type out the full path to the actual tests you want to run, though.
-
Thank you for your answer, but I want to know why I can not use full path to the vendor/bin/phpunit directory?user3748173– user37481732017年06月26日 17:26:48 +00:00Commented Jun 26, 2017 at 17:26
-
What directory are you running the tests from? The Magento root?thaddeusmt– thaddeusmt2017年06月26日 17:34:39 +00:00Commented Jun 26, 2017 at 17:34
-
Make sure this file exists, too:
ls dev/tests/unit/phpunit.xmlIf you haven't renamedphpunit.xml.disttophpunit.xmlyou will get theCould not readerror, obviously.thaddeusmt– thaddeusmt2017年06月26日 18:01:03 +00:00Commented Jun 26, 2017 at 18:01 -
The file that @thaddeusmt pointed out phpunit.xml exists and when following the documentation all works. (The documentation does not answer my question).user3748173– user37481732017年06月26日 20:13:06 +00:00Commented Jun 26, 2017 at 20:13
-
1You should be able to run the commands anywhere - but you'll need to use the full path to the
phpunit.xmlas well:/var/www/magento2/vendor/bin/phpunit -c /var/www/magento2/dev/tests/unit/phpunit.xmlthaddeusmt– thaddeusmt2017年06月27日 21:39:59 +00:00Commented Jun 27, 2017 at 21:39
Explore related questions
See similar questions with these tags.