Manual:PHP unit testing/Code coverage/cs: Difference between revisions
Updating to match new version of source page |
Created page with "Při spuštění PHPUnit na podmnožině souborů nebo podadresářů jsou testy mnohem rychlejší." |
||
| Line 28: | Line 28: | ||
<span id="Coverage_scope"></span> |
<span id="Coverage_scope"></span> |
||
== Rozsah pokrytí == |
== Rozsah pokrytí == |
||
Při spuštění PHPUnit na podmnožině souborů nebo podadresářů jsou testy mnohem rychlejší. |
|||
<span lang="en" dir="ltr" class="mw-content-ltr">When running PHPUnit on a subset of files or sub directory, the tests are much quicker.</span> |
|||
<span lang="en" dir="ltr" class="mw-content-ltr">However, building the coverage report may still take a long time.</span> |
<span lang="en" dir="ltr" class="mw-content-ltr">However, building the coverage report may still take a long time.</span> |
||
Revision as of 10:31, 6 June 2026
Obsah
- Provádění testů
- Vygenerování pokrytí kódu
- Writing testable PHP code
- Psaní testů
- Souvislá integrace
- Pochopení selhání sestavení
- Dodatek
(jak pomoci, zdroje..)
Nástroje
Tato příručka vysvětluje, jak generovat pokrytí kódu na vaší lokální pracovní stanici. To může být užitečné pro pochopení fungování krytí a pro sledování pokroku vaší práce před odesláním změny Gerritu.
Ujistěte se, že máte:
- Je instalován PHPUnit.
- Nainstalovaný ovladač pro pokrytí kódu (doporučuje se pcov, ale lze použít i jiné ovladače, například Xdebug).
Spusťte následující skript:
# $ vendor/bin/phpunit --coverage-html docs/coverage [files]
# For example:
$ vendor/bin/phpunit --coverage-html docs/coverage tests/phpunit/unit/languages/LanguageTest.php
Zobrazení zpráv o pokrytí
Zprávy o pokrytí jádra MediaWiki, rozšíření a všech našich samostatných knihoven jsou pravidelně publikovány na platformě doc.wikimedia.org. (See the tutorial on how to add your extension to the list).
CoverMe nabízí alternativní pohled, třídí funkce podle toho, jak často jsou volány v produkčních projektech Wikimedie, a pomáhá identifikovat funkce, které by z dodatečného testování měly největší prospěch.
Rozsah pokrytí
Při spuštění PHPUnit na podmnožině souborů nebo podadresářů jsou testy mnohem rychlejší. However, building the coverage report may still take a long time.
By changing filter whitelist in phpunit.xml to match the scope of your test run, you can generate coverage reports much faster.
Copy phpunit.xml.dist to phpunit.xml in the root of your repository, then update the whitelist:
For example:
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
<coverage includeUncoveredFiles="false">
<include>
- <directory suffix=".php">includes</directory>
- <directory suffix=".php">languages</directory>
- <directory suffix=".php">maintenance</directory>
- <directory suffix=".php">extensions</directory>
- <directory suffix=".php">skins</directory>
+ <directory suffix=".php">includes/libs</directory>
</include>
<exclude>
<directory suffix=".php">languages/messages</directory>
<directory suffix=".php">maintenance/benchmarks</directory>
<directory suffix=".php">extensions/*/tests</directory>
<directory suffix=".php">skins/*/tests</directory>
</exclude>
</coverage>
In case the relevant file(s) are in a large directly (e.g. /includes), you can also specify individual files:
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
<include>
- <directory suffix=".php">includes/libs</directory>
+ <file>includes/includes/WebRequest.php</file>
</include>
For extensions, you can use the composer script included with MediaWiki core to do this automatically:
$ composer phpunit:coverage-edit -- extensions/YourExtension
PhpStorm
In PhpStorm, you can use the "Run {test} with Coverage" option, and you will see the coverage report directly in the editor.
As defined above, make sure to edit the coverage include list in phpunit.xml first, doing so results in the coverage report being generated very quickly.
MediaWiki-Vagrant
$ vagrant ssh
$ cd /vagrant/mediawiki/tests/phpunit/
$ sudo -u www-data php phpunit.php --wiki wiki --coverage-html ../../docs/coverage includes/MessageTest.php
You can view the coverage report at http://dev.wiki.local.wmftest.net:8080/w/docs/coverage/.
Use a shortcut:
$ alias mwpun='sudo -u www-data php phpunit.php --wiki wiki'
$ mwpun --coverage-html ../../docs/coverage includes/MessageTest.php
You may get a permissions error about www-data being unable to write to this directory. In that case, try the following:
$ alias mwpun='sudo -u www-data php phpunit.php --wiki wiki'
$ sudo rm -rf /vagrant/mediawiki/docs/coverage /tmp/coverage
$ mwpun --coverage-html /tmp/coverage includes/MessageTest.php
$ sudo mv /tmp/coverage /vagrant/mediawiki/docs/coverage
mediawiki-docker-dev
you@host:mediawiki-docker-dev$ ./bash
root@container:/var/www/mediawiki# cd tests/phpunit/
root@container:phpunit# php phpunit.php --coverage-html ../../docs/coverage includes/MessageTest.php
View the coverage report at http://default.web.mw.localhost:8080/mediawiki/docs/coverage/.
See also
External links
- MediaWiki code coverage at doc.wikimedia.org
- Whitelisting Files for Code Coverage (PHPUnit Manual)
