Code coverage report is not generating - phpunit

When run command phpunit --bootstrap="TestHelper.php" --coverage-clover coverage.xml --whitelist="../../../" --debug --log-junit result.xml ./
It generates result.xml but console get stuck after this line.
Generating code coverage report in Clover XML format ...
Note : I already checked previous quetion on stackoverflow.
My phpunit.xml :
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./TestHelper.php"
backupGlobals="false"
backupStaticAttributes="false"
verbose="true"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true">
<testsuite name="Phalcon - Testsuite">
<directory>./</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory suffix=".php">../app/repositories/Account</directory>
<directory suffix=".php">../app/repositories/Call</directory>
<directory suffix=".php">../app/repositories/Credit</directory>
<directory suffix=".php">../app/repositories/User</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-clover" target="./coverage.xml"/>
<log type="junit" target="./results.xml" logIncompleteSkipped="false"/>
</logging>
</phpunit>

Related

PHPUnit Code Coverage not working and always saying incorrect whitelist config

Ich checked on several sources on the network and also here on stack overflow but couldn't solve it so far.
Heres my config:
<?xml version="1.0" encoding="UTF-8"?>
<!-- https://phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="../../src/test.bootstrap.php"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnError="false"
stopOnFailure="false"
stopOnIncomplete="false"
stopOnSkipped="false"
stopOnRisky="false"
verbose="true"
>
<!--printerFile="vendor/whatthejeff/emoji-phpunit-resultprinter/src/Emoji/PHPUnit/ResultPrinter.php"-->
<!--printerClass="Emoji\PHPUnit\ResultPrinter"-->
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="src/" />
<env name="BOOTSTRAP_CLEAR_CACHE_ENV" value="testing"/>
</php>
<loggin>
<log type="coverage-html" target="build/coverage"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-crap4j" target="build/logs/crap4j.xml"/>
<log type="junit" target="build/logs/junit.xml" logIncompleteSkipped="false"/>
</loggin>
<testsuites>
<testsuite name="Project Test Suite">
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/Bundle/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Tests</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
Due to the Build Server limitations I need to call phpunit with the following command:
./build/bin/phpunit.phar -c build/config/phpunit.xml --coverage-clover build/logs/clover.xml --coverage-crap4j build/logs/crap4j.xml --coverage-html build/coverage --coverage-xml build/logs/coverage.xml --log-junit build/logs/junit.xml --testdox-html build/testdox/testdox.html --testdox-xml build/logs/testdox.xml src/ -v
Everything works except code coverage heres the Result Output:
PHPUnit 6.1.4 by Sebastian Bergmann and contributors.
Runtime: PHP 7.1.5-1+0~20170522123046.25+jessie~1.gbpb8686b with Xdebug 2.6.0-dev
Configuration: /home/testcase/build-directories/build/config/phpunit.xml
Error: Incorrect whitelist config, no code coverage will be generated.
.................. 18 / 18 (100%)
Time: 945 ms, Memory: 6.00MB
OK (18 tests, 68 assertions)
I'm a little bit irritated as unit tests itself are working.
The Reds Message can be ignored (Hope so) as the Redis server is not yet fully configured.
I'm using oh-unit 6.1.4
Please give me a clue where the error is in my configuration
Try to change the whitelist deirectory into you phpunit with this:
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./src/*/*Bundle/Resources</directory>
<directory>./src/*/*Bundle/Tests</directory>
<directory>./src/*/Bundle/*Bundle/Resources</directory>
<directory>./src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
The directory given in the filter/whitelist/directory XML node is relative to the directory that contains the phpunit configuration file.
I think you should use ../../src.
I have the same issue, so I have update the whitelist directory path in phpunit.xml
<whitelist>
<directory>./web/core/includes</directory>
<directory>./web/core/lib</directory>
<!-- Extensions can have their own test directories, so exclude those. -->
<directory>./web/core/modules</directory>
<exclude>
<directory>./web/core/modules/*/src/Tests</directory>
<directory>./web/core/modules/*/tests</directory>
</exclude>
<directory>./web/modules</directory>
<exclude>
<directory>./web/modules/*/src/Tests</directory>
<directory>./web/modules/*/tests</directory>
<directory>./web/modules/*/*/src/Tests</directory>
<directory>./web/modules/*/*/tests</directory>
</exclude>
<directory>./web/sites</directory>
</whitelist>

How to run multiple PHPUnit test suites from command line?

If you have multiple test suites configured in phpunit.xml how do you run more than one test suite but not all of them from the command line?
phpunit.xml
<?xml version="1.0" encoding="utf-8"?>
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="true"
syntaxCheck="true"
bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">tests/unit</directory>
</testsuite>
<testsuite name="Integration">
<directory suffix="Test.php">tests/integration</directory>
</testsuite>
<testsuite name="Acceptance">
<directory suffix="Test.php">tests/acceptance</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-html" target="build/coverage"/>
<log type="testdox-html" target="build/requirements.html"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
example
phpunit --testsuite Unit|Integration but not Acceptance
Since PHPUnit 6.0 you can specify a comma delimited list of suites:
--testsuite suite1,suite2
Original answer for versions < PHPUnit 6.0
It doesn't work as you expect it to i.e. (where <pattern> is not an actual pattern):
--testsuite <pattern> Filter which testsuite to run.
You can choose a test suite to run but you can't use a pattern to filter which ones to run.
A better description would be --testsuite <name> Which testsuite to run.
Issue report https://github.com/sebastianbergmann/phpunit/issues/2273, which was fixed in version 6.0.

"No whitelist configured...". But thats not true. <whitelist> defined in Symfony app\phpunit.xml.dist. (This error occur only in Netbeans, not in cmd)

(I'm beginner in PHPUnit)
In Netbeans I try to code-coverage PHPUnit in Symfony2.8 project, but it throws error:
"C:\wamp\www\treningPHPUnitSymfony2.8\bin\phpunit.bat" "--colors" "--log-junit" "C:\Users\chiny\AppData\Local\Temp\nb-phpunit-log.xml" "--coverage-clover" "C:\Users\chiny\AppData\Local\Temp\nb-phpunit-coverage.xml" "C:\Program Files\NetBeans 8.1\php\phpunit\NetBeansSuite.php" "--" "--run=C:\wamp\www\treningPHPUnitSymfony2.8\src\TreningBundle\Tests\Utils\CalculatorTest.php"
PHPUnit 5.3.4 by Sebastian Bergmann and contributors.
Error: No whitelist configured, no code coverage will be generated
................I 17 / 17 (100%)
Time: 531 ms, Memory: 4.00MB
OK, but incomplete, skipped, or risky tests!
Tests: 17, Assertions: 16, Incomplete: 1.
Done.
But i have Symfony default app\phpunit.xml.dist with <whitelist> defined :
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.8/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<!--
<server name="KERNEL_DIR" value="/path/to/your/app/" />
-->
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
<directory>../src/*Bundle/Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*Bundle/Resources</directory>
<directory>../src/*Bundle/Tests</directory>
<directory>../src/*/*Bundle/Resources</directory>
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*Bundle/Resources</directory>
<directory>../src/*/Bundle/*Bundle/Tests</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
I got:
Symfony2.8, PHPUnit5.3.4, Netbeans 8.1
edit
But in windows console command
phpunit -c app/ src/TreningBundle/ --coverage-html=cov/ works great, generate coverage.
Unfortunately the "No whitelist configured, no code coverage will be generated" message is also printed when the whitelist configuration is invalid, see https://github.com/sebastianbergmann/phpunit/issues/2049 for details.

PHPUnit: No test executed with a seemingly good config file

I get the message no tests executed when i try to do
phpunit
or
phpunit -c phpunit.xml
on the other hand, if i do
phpunit -c phpunit.xml ./tests
It works. But this is a problem considering that some tool that I'm using does not handle this well.
Directory structur
code
tests/
phpunit.xml
autoloader.php
And here is the config file
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="true"
bootstrap="./tests/bootstrap.php"
>
<testsuites>
<testsuite name="StdTestSuite">
<directory>
tests/
</directory>
</testsuite>
</testsuites>
</phpunit>
The problem was the stupidest thing ever.
You can't have whitespace inside the tag.
So what you need to have in you phpunit.xml file is this
...
<testsuite name="StdTestSuite">
<directory>tests/</directory>
</testsuite>
...
Then running
phpunit
Should work

why phpUnit test code coverage not working in Symfony2 when including file?

PHP Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Test\WebTestCase' not found in /home/manivasagam/projects/ProofCentral/src/ProofCentral/ServiceBundle/Tests/Services/AttachmentServiceTest.php on line 15
PHP Stack trace:
PHP 1. {main}() /usr/bin/phpunit:0
PHP 2. PHPUnit_TextUI_Command::main() /usr/bin/phpunit:46
PHP 3. PHPUnit_TextUI_Command->run() /usr/share/php/PHPUnit/TextUI/Command.php:129
PHP 4. PHPUnit_Runner_BaseTestRunner->getTest() /usr/share/php/PHPUnit/TextUI/Command.php:150
PHP 5. PHPUnit_Runner_BaseTestRunner->loadSuiteClass() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:104
PHP 6. PHPUnit_Runner_StandardTestSuiteLoader->load() /usr/share/php/PHPUnit/Runner/BaseTestRunner.php:168
PHP 7. PHPUnit_Util_Fileloader::checkAndLoad() /usr/share/php/PHPUnit/Runner/StandardTestSuiteLoader.php:77
PHP 8. PHPUnit_Util_Fileloader::load() /usr/share/php/PHPUnit/Util/Fileloader.php:76
PHP 9. include_once() /usr/share/php/PHPUnit/Util/Fileloader.php:92
The problem here is that you dont load bootstrap file
I put the configuration in the app/phpunit.xml
With following xml in it:
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
<phpunit
backupGlobals="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap = "bootstrap.php.cache" >
<testsuites>
<testsuite name="Project Test Suite">
<directory>../src/*/*Bundle/Tests</directory>
<directory>../src/*/Bundle/*/Tests</directory>
</testsuite>
</testsuites>
<!--
<php>
<server name="KERNEL_DIR" value="/path/to/your/app/" />
</php>
-->
<filter>
<whitelist>
<directory>../src</directory>
<exclude>
<directory>../src/*/Bundle/*/Resources</directory>
<directory>../src/*/Bundle/*/Controller</directory>
<directory>../src/*/Bundle/*/Tests</directory>
<directory>../src/*/Bundle/*/DataFixtures</directory>
<directory>../src/*/Bundle/*/DependencyInjection</directory>
<directory>../src/*/Bundle/*/Form</directory>
<directory>../src/*/Bundle/*/Event*</directory>
<directory>../src/*/Bundle/*/Firewall</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
And then run the phpunit
phpunit --coverage-text app src/ProofCentral/ServiceBundle/Tests/Services/CalculatorTest.php --bootstrap path/to/your/app/bootstrap.php.cache

Resources