How do I run phpunit over folders matching a pattern - phpunit

I have a folder structure like this
root_folder
folder1
_unittest
<other files>
folder2
_unittest
<other files>
Now what I want to do is run phpunit from the root on files found in all the subfolders named _unittest
Can anyone suggest how this can be done (maybe using the xml config file?)
Thanks

You can indeed use testsuites directive in phpunit.xml.dist, but this directive allow you to play with wildcards and target easily subdirectories in one testsuite. For your exampple, you can do it like that:
<testsuites>
<testsuite name="only-unit-test">
<directory suffix=".php">./*/_unittest/</directory>
</testsuite>
<testsuite name="all-test">
<directory suffix=".php">./</directory>
</testsuite>
</testsuites>
phpunit will go through all subfolders and check for testcases in _unittest folders. It avoids to have an entry for each folder in your configuration.

PHPunit will, when the xml config is set only run files from a given set of directories
<testsuites>
<testsuite name="test-suite">
<directory suffix=".php">./folder1/_unittest</directory>
<!-- other listing of directories for tests -->
</testsuite>
</testsuites>
If your folder2 is at the same level as root_folder then you may want to rethink the structure, or place the test configuration in a different directory.

Related

CakePHP 2 Code Coverage now says "No files to generate coverage for"

My unit tests on CakePHP still run, but the code coverage has disappeared. All I get is "No files to generate coverage for".
Our application is currently running on CakePHP 2.10.15. I have PHPUnit 5.7 installed. Running PHP 7. I use the web runner for tests & coverage. I have XDebug 2.7.0beta1 running.
Did one of our recent upgrades break some sort of connection between Cake and PHPUnit?
Create a file phpunit.xml in your app folder:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/5.7/phpunit.xsd">
<logging>
<log type="coverage-text" target="tmp.txt" showOnlySummary="true"/>
</logging>
<testsuite name="default">
<directory suffix="Test.php">tests</directory>
</testsuite>
<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<!-- directories that you want in code coverage -->
<directory suffix=".php">app/</directory>
</whitelist>
</filter>
</phpunit>
For me just adding this file like this worked just fine. For more information about this xml: https://phpunit.de/manual/5.7/en/appendixes.configuration.html
Be aware to include only the files and directories you need, otherwise it will became very slow.

How to exclude composer autoloader files from phpunit coverage report (using netbeans/windows)

My 'test project' contains only the Application.php and the ApplicationTest.php but the phpunit collect the coverage information about composer autoloader files also, which is wrong. How can I exclude the autoloader files from the coverage report?
When creating the configuration for code coverage, you'll almost always set the 'whitelist' in the phpunit.xml file to only cover your main source files - this also speeds up the test-run, as it's not having to also run code-coverage of all the library files in the vendor/ directory (because that can take a long time).
<filter>
<whitelist processUncoveredFilesFromWhitelist="true"
addUncoveredFilesFromWhitelist="false">
<!-- only collect code coverage in src/**/*.php files -->
<directory suffix=".php">./src</directory>
<exclude>
<!-- directories/files to not cover (within src/) -->
<directory suffix=".php">./src/*/*Bundle/Resources</directory>
<directory suffix=".php">./src/*Bundle/Resources</directory>
<directory suffix=".php">./src/tests/</directory>
</exclude>
</whitelist>
</filter>
Configure a whitelist and do not add the autoloader to it.

Phpunit tries to test all Symfony console commands

I'm working on a Symfony project. As I need to do unit testing, I downloaded and installed Phpunit 6.2.4 from its website.
However, when I tried to update my database, I got this output
php bin/console doctrine:schema:update --dump-sql
PHPUnit 6.2.4 by Sebastian Bergmann and contributors.
unrecognized option --dump-sql
bin/console doctrine:schema:update
Cannot open file "doctrine:schema:update.php".
I tried other console commands, but the result is the same. Basically, my guess was that somehow Phpunit tries to test every single file, so I edited the phpunit.xml file like this, using a previous one that worked in other project.
<?xml version="1.0" encoding="UTF-8"?>
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/4.1/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="app/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_DIR" value="app/" />
</php>
<testsuites>
<testsuite name="Project Test Suite">
<directory suffix=".php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src</directory>
<exclude>
<directory>src/*Bundle/Resources</directory>
<directory>src/*/*Bundle/Resources</directory>
<directory>src/*/Bundle/*Bundle/Resources</directory>
</exclude>
</whitelist>
<blacklist>
<directory>bin</directory>
<directory>docker</directory>
</blacklist>
</filter>
</phpunit>
As far as I know, blacklisting both bin and docker directories should result in phpunit not running anything inside them, but it still doesn't work.
Then I checked my composer.json for the symfony/phpunit-bridge, removed it and tried again, but the problem continues.
Has anyone ever faced this?
Blacklisting appear to remove directories from the code-coverage generation whitelist - but if src/ does not contain those sub-directories, and so it is redundant - and also a great deal slower adding and discarding huge numbers of files. On one project, I tried to blacklist vendor/, it was taking a minute to even show the initial command banner, before starting to run the tests.
Remove the <blacklist/> section, and the rest should be fine - if you don't mention bin/ or docker/ then PHPunit won't need to read the files, it so won't run them either.

Phpunit starts all the tests or not starts all the tests, ignoring the config

My project contains 2 packages, and I want to run tests in only one of them. Used symfony 3.3 and phpunit 6.3.0
phpunit.xml.dist
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/6.3/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="./src/CoreBundle/Tests/autoloadWithIsolatedDatabase.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="KERNEL_CLASS" value="AppKernel" />
</php>
<testsuites>
<testsuite name="App">
<directory>src/AppBundle/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>
And structure of the project
This configuration will run all the tests from AppBundle and CoreBundle (in the second there are no tests), and if you change
<directory>src/AppBundle/Tests</directory>
to
<directory>src/CoreBundle/Tests</directory>
then there will be no tests at all. I can not understand what's wrong
Let's start of by how your phpunit.xml.dist is configured. You have one test suite defined:
<testsuites>
<testsuite name="App">
<directory>src/AppBundle/Tests</directory>
</testsuite>
</testsuites>
This is the place phpunit will look into for tests. They have to conform to the usual conventions like having a file name ending in Test and each test method must be prefixed with a test.
Also from your screenshot I can gather that you have a top level tests/ folder (right next to app/, src/, etc.). This is probably where your other tests are placed in.
The second folder is where you should also place your tests from the AppBundle if you follow the best practices: https://symfony.com/doc/current/best_practices/tests.html
I think this was established sometime during the 3.x release cycle.
In theory you should be able to copy src/AppBundle/Tests to tests/AppBundle and hopefully everything still works. Now you can update your test suite configuration to:
<testsuites>
<testsuite name="App">
<directory>tests/</directory>
</testsuite>
</testsuites>
Your filter can stay in place as src/CoreBundle/Tests does not actually contain test-classes, only helpers used for tests.
Now that you have all tests in one big tests folder separated by bundle you might want to do a search on this folder for classes extending PHPUnit_Framework_TestCase. Since PHPUnit 6.0 introduced namespaces those need to be updated with PHPUnit\Framework\TestCase otherwise PHPUnit will ignore those tests.

How to whitelist programmatically-generated PHP files in PHPUnit?

I'm generating some PHP code programmatically, which I want to test through PHPUnit.
Code is generated as the test cases require it, and it is then saved in php files in a whitelisted folder.
When PHPUnit finishes running, the files are left in that folder so that I can check the code coverage on them.
However, in my PHPUnit's bootstrap I am emptying that folder, so that previous PHPUnit executions won't affect the result of the execution just launched.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
bootstrap="./test/bootstrap.php"
convertErrorsToExceptions="true"
stopOnError="true"
stopOnFailure="true"
>
<testsuites>
<testsuite name="TestSuite">
<directory>./test/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./src-generated/</directory>
</whitelist>
</filter>
</phpunit>
PHPUnit complains that the folder is empty.
Error: Incorrect whitelist config, no code coverage will be generated.
Is there a way to whitelist files as they are generated? Is there a way to add them dynamically or tell PHPUnit to cover any file encountered regardless of the path?
The paths are correct, test all run correctly, except that I get no code coverage.
Than you in advance.

Resources