7

I am running php bin/magento dev:tests:run static to check my code quality but it takes a really long time. Is there a way I can run them for one module and/or one theme?

3
+50

Yes, there is a way to run custom testsuite according to your needs. Static tests are run with phpunit and the configuration file is in dev/tests/static/phpunit.xml.dist

So, from inside of this path do:

  1. cp phpunit.xml.dist phpunit.xml
  2. Edit new file with your editor and create custom testsuite in element. You can read more about configuring phpunit with a file in documentation
  3. When you create your testsuite enter command php ../../../vendor/bin/phpunit --testsuite="YOUR TESTSUITE NAME"

That is all :)

There is a more options like running those tests from your IDE, customizing testsuite by excluding or whitelisting files, etc. But the most basic approach is described above.

And a small addition, each of presets use some kind of bootstrap, eg. static/testsuite/Magento/Test/Js/LiveCodeTest.php for Javascript Static Code Analysis; it use blacklist/whitelist *.txt You will have to use and suite those files in your suites. So, in the end, it requires some work but is doable.

Example of configuration for a less static tests

Let us assume that we have a module Module/Test with related *.less files

  1. copy dev/tests/static/phpunit.xml.dist => dev/tests/static/phpunit.xml Then modify it to looks like:

    enter image description here

Most important thing is that our testsuite has attribute name="Test Sample" and target php file containing tests which also serves as a bootstrap file. You can either write your own or copy and modify existing one along with changing path. It means that all configuration can be kept in module's directory but for sake of this tutorial I will go as simple as possible. As a reference please check vendor/magento/magento2-base, there in dev/tests/static you will find similar file.

  1. open static/testsuite/Magento/Test/Less/_files/whitelist/common.txt and add glob pattern or patterns for the location of the files you wish to test, eg. app/code/Test/Module/view/web

  2. now open terminal and go to dev/test/static and from there run php ../../../vendor/bin/phpunit --testsuite="Test Sample"

That is all for less testsuite. For each of tests package you can do the same. Details might vary but general approach is as I described.

And last word, From my point of view it is better to use live linters provided either by ide or plugins for it, or in case of CI, to use tools that a particular CI tool provide.

  • First of all, thanks for this. I'm glad it can be done. But second... can you provide a small example on how it can be done? It will help me and future readers. How should my phpunit.xml look like in case I want to run all the static tests for a module called Test_Sample located in app/code/Test/Sample/. – Marius Nov 3 '17 at 12:03
  • Ok, i updated my answer. – Bartosz Herba Nov 3 '17 at 14:23
  • Sounds like what I need. I will test and come back (on Monday probably) and award you the 50 fake internet points. Thanks. – Marius Nov 3 '17 at 14:59

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.