I'm trying to configure PhpStorm 2017.2 to use PhpUnit 5 for my PHP 5.6 project.
I've downloaded the phpunit-5.7.21.phar file from the official source and placed it in my PHP 5.6 installation dir.
In PhpStorm Settings >> Languages & Frameworks >> PHP >> Test Frameworks, I've linked to the .phar executable and set the default config file to a phpunit.xml in the project root directory
Here are the contents of phpunit.xml:
.
<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
<testsuites>
<testsuite name="Test suite">
<directory>tests</directory>
</testsuite>
</testsuites>
</phpunit>
I'm trying to structure tests in a tests/unit directory within which my source file project structure would be mirrored as described in the manual. For instance:
// project files:
ClassOne.php
vendor/
ClassTwo.php
Utility.php
// test files
tests/unit/
ClassOneTest.php
vendor/
ClassTwoTest.php
UtilityTest.php
I have two problems though:
First, I don't know how to configure PhpStorm to create tests within tests/unit/ mirroring the sructure with respect to the project root. When I create a test, by default the file is put in the same directory as the project file.
Secondly, I don't know how to get PhpStorm to index the PHPUnit source code. Even though I've linked to phpunit-5.7.21.phar file as shown above, the IDE complains when I create a test:
namespace vendor;
class UtilityTest extends \PHPUnit_Framework_TestCase{}
Undefined class PHPUnit_Framework_TestCase
Update 1
I solved the 2nd problem by adding the directory where I had saved the .phar to the PhpStorm include path, set in Settings >> Languages & Frameworks >> PHP >> Include Path tab. Alternatively, I could just put the .phar file within the project directory and it will be indexed.
I still need help with my first problem.
Update 2
Thanks to Ástþór's answer I figured out how to get PhpStorm to mirror the project structure within a dedicated tests directory. Go to PhpStorm Settings >> Directories and select the base testing directory. The click Test near the top to mark it as a Test Sources Root
The next time you create a test, it will automatically be placed in that directory.
Mark a directory with your source classes as "Source root" and a directory with tests as "Test Sources Root". After that directories will be pre-filled on a test creation (e.g. via ctrl+shift+T on a source class).
Not sure though if that would work fine with your "mirroring" system: I guess you would still have to manually adjust directories for the part of your tests
Related
I'm developing a Zend Framework 2 application with a common folder structure, so that the folder /vendor contains all (project external) libraries. Setting up the unit testing environment I would like to be able to run all vendor tests. The folders structures are different depending on the library. Some packages have no tests at all.
A possible solution would be to create a test suite "vendor" and manually define there the paths to every single test folder, e.g.:
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
<testsuites>
<testsuite name="vendor">
<directory>../vendor/lib-foo/path/to/tests</directory>
<directory>../vendor/package-bar/path/to/tests</directory>
...
</testsuite>
...
</testsuites>
...
</phpunit>
I don't like this solution. First of all because then I'd have to handle every package manually.
Another solution would be to define /vendor as test folder:
phpunit.xml
<?xml version="1.0" encoding="UTF-8"?>
<phpunit ...>
<testsuites>
<testsuite name="vendor">
<directory>../vendor</directory>
...
</testsuite>
...
</testsuites>
...
</phpunit>
Well, but then PHPUnit has to scan a lot of folders, that it doesn't need, and the tests will need much more time.
Is there a better solution, that would make possible to automate the process and avoid much manual configuration?
It would probably be difficult to run all PHPUnit vendor test suites with a single test run. One issue is that each of the different test suites might ship its own configuration file or even require a custom bootstrap configuration file. You cannot cover that when running all test suites with a single command.
I'd probably use some shell magic for this. Note that this example relies on the presence of a phpunit.xml(.dist) file in each of your 3rd party packages (for most libraries that's a reasonable assumption). You could even integrate this into your continuous integration process to test this continuously:
for FILE in $(find . -name 'phpunit.xml*') ; do
sh -c 'cd '$(dirname $FILE)' && composer install'
vendor/bin/phpunit -c $FILE
done
I've started learning how to use PHPUnit. However, I'm facing a problem which I have no clue how to solve.
I have a folder called lessons and inside there is a composer.json which I installed PHPUnit with.
The output resulted in a folder called vendor and a sub-folder called bin which has the phpunit file in it.
In the cmd I typed: cd c:/xampp/htdocs/lessons/vendor/bin. Now the cmd folder sets to the same folder as phpunit. I've created a directory in lessons which I called tests (lessons/tests) which I store all of my tests in. I've created a file called PracticeTest.php with a very simple test script in it.
When I go back to the cmd and type phpunit tests I get cannot open file tests.php When I try to type phpunit PracticeTest I get cannot open file PracticeTest.php. When I try phpunit tests/PracticeTest (with .php or without) I get the same error that the file could not be opened.
My suspicious that it has something to do with that face that the cmd is pointing at the lessons/vendor/bin directory, but I'm not sure if it is really the problem or how to fix it.
just to arrange the paths:
lessons\vendor\bin\
lessons\tests\
lessons\tests\PracticeTest.php
Thanks in advance!
Go to path project:
D:\>cd www
D:\wwww>cd lessons
And execute:
D:\www\lessons>vendor\bin\phpunit tests
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
.....E.
Time: 1.34 seconds, Memory: 5.50MB
There was 1 error:
1) UserTest::it_should_be_able_to_construct
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert
:assertInstanceOf() must be a class or interface name
D:\www\lessons\tests\UserTest.php:10
FAILURES!
Tests: 7, Assertions: 4, Errors: 1.
It Works!!!!
This is what worked for me.
vendor/bin/phpunit ./tests/PracticeTest
I was getting the same, but if you type phpunit then tab you will see what directory you are in and it should autosuggest. Then navigate to your test file.
I had include_path sections separated by comma. It has to be semicolon on Windows.
I have created a phpunit.bat in the root folder of my project containing
#ECHO OFF SET BIN_TARGET=%~dp0/vendor/phpunit/phpunit/phpunit php "%BIN_TARGET%" %*
Now it works.
I have already posted this question on php unit first test symfony
I installed phpunit via the composer as a per project installation.
When trying vendor/bin>phpunit -c ../../app every thing is ok and I get a positive answer.
Whereas this command give the answer to all the tests in the tests directory.
But I want the result to every test alone.
When trying /vendor/bin>phpunit -c ../../src/xxx/Bundle/tests/entity/yyy.php and I get the following message : could not load c:\wamp\www\symfony\src/xxx/Bundle/tests/entity/yyy.php Parse PI : PI php never end ... Start ttag expected, '<' not found
and when trying /vendor/bin>phpunit -c ../../src/xxx/Bundle/tests/entity/yyy and I get the following message : could not read "..\..\src/xxx/Bundle/tests/entity/yyy"
Could anybody help me to know how should I write the command and from where execute it???
Any ideas???
Don't use the -c option here. The -c option is a shortcut for --configuration and it points to the directory of a PHPunit configuration file (like app/phpunit.xml.dist). That configuration tells PHPunit where to look for the test classes and some other configuration, like the bootstrap file.
If you want to run tests for a specific test, you can do it like phpunit path/to/tests/MyTest.php. But you'll loose the autoloading then. To get that back, you can use the --bootstrap option to point to the bootstrap file. So it'll be phpunit --bootstrap vendor/autoload.php path/to/tests/MyTest.php.
If you want to run this command more often, you can better edit the app/phpunit.xml.dist file and create a new suite:
<?xml version="1.0" encoding="utf-8" ?>
<phpunit ...>
<!-- ... -->
<testsuites>
<testsuite name="MyBundle">
<file>path/to/tests/MyTest.php</file>
</testsuite>
</testsuites>
<!-- ... -->
</phpunit>
And then run: phpunit -c app --testsuite MyBundle
Happened with me too, but in fact we are misreading the documentation you are forgetting 'app' in the command line look:
phpunit -c app src/AppBundle/Tests/Util/CalculatorTest.php
Note the app parameter in command sentence.
I have this config:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals = "false"
backupStaticAttributes = "false"
colors = "false"
convertErrorsToExceptions = "true"
convertNoticesToExceptions = "true"
convertWarningsToExceptions = "true"
processIsolation = "false"
stopOnFailure = "false"
stopOnError = "false"
stopOnIncomplete = "false"
syntaxCheck = "false"
bootstrap = "test_bootstrap.php"
>
<testsuites>
<testsuite name="UnitTests">
<file>unit/api/2/ApiControllerTest.php</file>
<file>unit/api/2/RoutesTest.php</file>
</testsuite>
it runs the Test files. If I replace the files with
<directory>unit</directory>
// or
<directory>unit/api/2</directory>
// or
<directory>unit/api/2/*</directory>
// or
<directory>unit/api/2/*Test.php</directory>
// or
<directory suffix="Test.php">unit/api/2</directory>
It simply says: No tests executed!
Please, what could be wrong?
Ubuntu 12.04 LTS, php 5.3.10, phpunit 3.7.16
Where in you path relative to the unit directory is the phpunit.xml file?
if your path looks anything like this:
projectroot
|- phpunit.xml
|- unit/
|- api/
You could try setting the directory to the following in phpunit.xml:
<directory>./unit</directory>
And then run phpunit from root like this: phpunit -c phpunit.xml
If that doesn't work, something else is wrong. What happens if you run this:
phpunit ./unit/api/2
If no tests are being run then, please answer the following questions:
maybe your methods in the testcases don't start with 'test'?
Do all testcase files end with Test.php?
Do all testcase classes extend PHPUnit_Framework_TestCase?
Do all testcase classnames end with 'Test'?
PHPUnit defaults to the suffix Test.php, which isn't obvious.
If your tests use another suffix (e.g. TestUser.php or /test/User.php, instead of UserTest.php), the test will be skipped.
This default can be overwritten using the --test-suffix flag on the CLI:
phpunit --test-suffix _test.php
or with the suffix attribute in the XML configuration:
<include>
<directory suffix="_test.php">src</directory>
</include>
Sources:
PHPUnit: The Command-Line Test Runner
PHPUnit: The XML Configuration File
This might depend a little bit on the version of Phpunit.
I could get it to work with two more older ones (3.7.22 and 3.7.38) but had a similar problem earlier. Here is my solution:
I first had the directory configured wrong:
<testsuites>
<testsuite name="Default">
<directory>/tests</directory>
</testsuite>
</testsuites>
As you can see in this wrong example, it's prefixed with a slash ("/"). Removing that slash results in the following XML excerpt that is working fine then:
<testsuites>
<testsuite name="Default">
<directory>tests</directory>
</testsuite>
</testsuites>
Ensure the directory is not prefixed with a slash and that it exists. The existence part is crucial, because the Phpunit testrunner will not display na error message if it does not exists. You will only see that no tests are executed.
No tests executed!
Taking what you have in your question as an example:
<directory>unit</directory>: It fails because the directory does not exists (you think it does, I know, but it fails if the directory does not exists, Phpunit is not lying to you).
<directory>unit/api/2</directory>: If it fails, it fails because the directory does not exists (you think it does, I know, but it fails if the directory does not exists, Phpunit is not lying to you).
<directory>unit/api/2/*</directory>: This will always fail because the directory does not exists. Most file-systems do not allow to use the "*" character in directory- and file-names.
<directory>unit/api/2/*Test.php</directory>: Same here, this is not a valid directory name, it fails because the directory does not exists.
<directory suffix="Test.php">unit/api/2</directory>: If it fails, it fails because the directory does not exists (you think it does, I know, but it fails if the directory does not exists, Phpunit is not lying to you). Additionally the suffix parameter with the value "Test.php" is superfluous because this is the default suffix. As it is superfluous, this is cruft and you should remove it from the XML file.
No software is without bugs and this naturally applies to Phpunit as well, but you should first of all consider that Phpunit is not lying to you in the first place.
I for myself had not problems with the two versions documented after I corrected the directory configuration. Your mileage may vary. This applies to the points 1, 2 and 5 specifically: From your question it looks like those directories do exist, but from the description you give and what is the Phpunit behaviour I can see, those directories do not exist.
Points 3 and 4 should be clear, they simply do not work because you put wildcards into the directory name which was meant to describe a concrete path not to hint that wildcard usage was intended / allowed. You can find this point outlined as well in a previous Q&A question: Attempting to run Phpunit with a configuration XML file results in exception.
The information in this answer is based on
PHPUnit 3.7.22 and 3.7.38 by Sebastian Bergmann.
PHP 5.4.36 by php.net
Is there a way to automatically load a Listener (or phpunit configuration file for that matter), without using no more than:
phpunit testdir
? Today I'm using:
phpunit -c phpunit.xml --bootstrap bootstrap.php testdir
and want to exclude all switches. I know that I could have a phpunit.xml file in every directory, but thats not an option..
Thanks in advance!
PHPUnit by itself should look in the current directory for a phpunit.xml file. So your first and second example should both find and include the same phpunit.xml.
Use a Makefile:
unittest:
phpunit -c phpunit.xml --bootstrap bootstrap.php testdir
And simply call it with make unittest
Now you can add more for different cases, in the same pattern.