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?
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:
cp phpunit.xml.dist phpunit.xml
- Edit new file with your editor and create custom testsuite in element. You can read more about configuring phpunit with a file in documentation
- 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.
Example of configuration for a less static tests
Let us assume that we have a module Module/Test with related *.less files
copy dev/tests/static/phpunit.xml.dist => dev/tests/static/phpunit.xml Then modify it to looks like:
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.
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
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.