Grouping phpunit tests. Launching test from specified directory - phpunit

I have a directory tests where I store all my tests.
Hierarchy of tests dirrectory
tests->
ApplicationTests->
IntegrationTests->
Factory->
Service->
UnitTests->
How can I make phpunit launch tests only from for example Service directory, instead of the whole tests directory? I read about #Groups, but I think that's not what I'm looking for and it would be best if I would not need to edit config files, but some how, do it from the command line.

Found a quite easy way, even with config editing.
phpunit.xml
See: The XML Configuration File Phpunit Docs and Organizing Tests Phpunit Docs<
<testsuite name="Service">
<directory>tests/IntegrationTests/Service</directory>
</testsuite>
Command line:
php ./vendor/bin/phpunit --testdox --testsuite Service

Related

PhpStorm doesn't get the folder's configuration when running a single test method

I have a Laravel project with three test directories - Browser, Feature, and Unit.
In Run/Debug configurations I've configured the Feature and the Unit directories to use phpunit.xml while the Browser directory uses phpunit.e2e.xml. I've also created a default configuration for all tests which uses the phpunit.xml.
If I open a class that is in the Feature/Unit directories and run a single test method or the whole class, it picks the default configuration, but when I run a single test method in the Browser directory, it uses phpunit.xml instead of phpunit.e2e.xml.
If I select a directory and run tests, it picks the proper configuration, but when I open a class in the Browser directory, it doesn't
Why doesn't PhpStorm use the configuration of the Browser directory recursively? How can I handle this problem?
As far as I know PhpStorm has never had this functionality and I wouldn't count on them adding it either. It seems way too specific.
What you can do though, is specify the configuration file you want to use through the commandline.
See: https://phpunit.de/manual/6.5/en/textui.html#textui.clioptions
So your command would look something like this:
phpunit tests\Unit\SomeTest.php --configuration phpunit.xml
or
phpunit tests\Browser\SomeTest.php --configuration phpunit.e2e.xml

Different configurations for Unit and Integration Tests in PHPUnit

I would like to have different configuration files for PHPUnit for my
Unit Tests: no database available, no cache, no everything
integration tests: everything is there
AND
to be able to execute these tests in PhpStorm all together or separated per file with automatically the right configuration.
There are several opportunities, which do not work for all needs:
Option 1:
Multiple phpunit.xml files
In PhpStorm can only set one PHPUnit default configuration, for executing a single test file, this will not work.
Option 2: Using a PHPUnit_Framework_BaseTestListener::startTestSuite($suite).
This works with a single phpunit.xml and if you are just executing the whole test suite, this works. But when you want to execute a single test file in PhpStorm, you do not have a $suite available and can not load the right configuration.
How do you handle different test configurations with PhpStorm?
Because there seems to be no offical possiblity on PHPStorm, I figured out a way by using a PHPUnit TestListener:
https://sebastianviereck.de/run-phpunit-unit-integrations-test-configurations-phpstorm/
PHPStorm can handle unit tests and integration tests in the same phpunit.xml file
Create two separate test suites in the same xml file. Each suite must have a distinct name in the name attribute.
Create two separate configurations for the tests
in PHPStorm (one for the unit tests and one for the integration tests) and indicate the same xml configuration file path for both tests
In the "Test Runner options" field, indicate the test suite name (the same as the one in the xml file) like this: --testsuite unit_tests
<testsuites>
<testsuite name="unit_tests">
<directory>/foo</directory>
</testsuite>
<testsuite name="integration_tests">
<directory>/bar</directory>
</testsuite>
</testsuites>
Once you have done that you can run unit tests and integration tests separately and still have them configured in the same xml file

Excluding certain tests from loading in phpunit

Some of my testcases use a custom testing library. Also these testcases are very slow. So I would like to run them only in the build server and not in my local. I want to run the other tests locally.
Following is the directory structure. The ones inside the slow directory are the slow test cases that should be excluded.
/tests/unit-tests/test-1.php
/tests/unit-tests/test-2.php
/tests/unit-tests/slow/test-1.php
/tests/unit-tests/slow/test-2.php
/tests/unit-tests/foo/test-1.php
/tests/unit-tests/bar/test-2.php
I tried creating groups using #group annotation. This works, but the issue is that these test files are getting loaded (tests not executed though). Since they require the test library which is not installed locally, it is giving an error.
What is the best way to create the phpunit.xml configuration such that these slow tests are excluded (and not even loaded) by default and can be executed if needed?
There are 2 options:
In your phpunit.xml create 2 test suits - one for CI server and one for local development
<testsuites>
<testsuite name="all_tests">
<directory>tests/unit-tests/*</directory>
</testsuite>
<testsuite name="only_fast_tests">
<directory>tests/unit-tests/*</directory>
<!-- Exclude slow tests -->
<exclude>tests/unit-tests/slow</exclude>
</testsuite>
</testsuites>
So on CI server you can run
phpunit --testsuite all_tests
And locally
phpunit --testsuite only_fast_tests
Obviously, you can name test suites as you want.
I think preferable way is:
Create phpunit.xml.dist and configure default execution of phpunit (for CI server and all those who just cloned repository)
Modify phpunit.xml by configuring your local execution of phpunit
(by adding <exclude>tests/unit-tests/slow</exclude> to default
testsuite)
Exclude phpunit.xml from version control.
From the docs:
If phpunit.xml or phpunit.xml.dist (in that order) exist in the
current working directory and --configuration is not used, the
configuration will be automatically read from that file.
Some links:
The XML Configuration File. Test Suites
How to run a specific phpunit xml testsuite?

PHPUnit: bootstrap file isn't included via phpunit.xml but works well if i use the --bootstrap directive

Ok, i really don't understand what is happening with my PHPUnit's bootstrap file.
I have a folder structure like the following one:
[Root]
- joo_component
-- administrator
-- site
-- Tests
--- admin
--- site
--- bootstrap.php
--- phpunit.xml
- vendor
In the phpunit.xml file, i have the following code:
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuites>
<testsuite... [all the things that go here]</testsuite>
</testsuites>
</phpunit>
Now, if i call PHPUnit explicitly specifying the bootstrap file all works well: the testing starts and properly signals errors and all other things it has to signal.
This is the command that works:
vendor/bin/phpunit --bootstrap="joo_component/Tests/bootstrap.php" joo_component/Tests
But, if i try to launch the testing without explicitly specifying the bootstrap file but specifying it in the phpunit.xml file it seems like the bootstrap file isn't loaded.
vendor/bin/phpunit joo_component/Tests
Is there anyone who can help me underdtand why this happens? Is there a way to force PHPUnit to tell me which bootstrap file it is using to load the test suites?

PHPUnit testing individually in Symfony

I would like to run some unit Tests individually with PHPUnit, but I have certain classes separated from the Tests, since I am using the symfony framework, and I group the Tests and the Classes in different folders.
I would like to run the Tests individually like this:
php phpunit.phar MyTest.php
The problem is that the test file uses the classes from the controllers, and phpunit doesnt seem to be able to import the needed classes for the test.
This is not a problem to run all the tests together, thanks to phpunit.xml but when I want to run them individualy, its a problem.
How could I fix this?
You have to point phpunit where you have your phpunit.xml config file (because it must know the autoloader for example). If you have default symfony 2 structure it will be in app directory, so just run your test like that (I assume that you are in project root path):
phpunit -c app/ --filter="concreteTestPattern" src/Acme/DemoBundle/Tests/MyTest.php
edit:
Above will run all tests which names match to the pattern: /.*concreteTestPattern.*/
You would use the --filter argument in your PHPUnit command string. This will only run tests that match the pattern given. If you pass only the complete name of the test that you want run, phpunit should only run that test.
If you have a data provider associated with the test and only want to run one test case, you can also filter that by using --filter <testName>::<testcase name>
PHPUnit can be set to execute using a configuration file.
In our Symfony2 project this file is located at app/phpunit.xml.dist.
As this file is suffixed with .dist, you need to copy its contents into a file called app/phpunit.xml.
If you are using a VCS such as Git, you should add the new app/phpunit.xml file to the VCS ignore list.
You will also notice the configuration is specifying the bootstrap file located at app/bootstrap.php.cache. This file is used by PHPUnit to get the testing environment setup.
We can execute this test by running the following command from the root directory of the project. The -c option specifies that PHPUnit should load its configuration from the app directory.
$ phpunit -c app

Resources