Run phpunit for current file Follow
Hi everyone, I am new to phpStorm and I currently try to set it up for my needs. But right now I'm stuck with the configuration of PHPUnit. My problem is that I just want to run the current file with PHPUnit. In the run configuration I can only choose between these options: - All in Directory - Class - Method - Defined in the configuration file But "All in Directory" wastes a lot of time and creating a Run Config for each unit test is tiresome as well, since they all share the same configuration and bootstrap file. All I want to do is having something like Rightclick -> Run "MyTest.php" for unit tests. Can't image that this really isn't possible but rather I can't find the right settings. Thanks in advance
Please sign in to leave a comment.
In Windows, ALT-SHIFT-F6 with the cursor in a test method runs that method, with the cursor in a test class, but not in a method runs the file.
I am using Mac OS X. I tried pretty much every combination with F6, the only ones that worked were F6 (move) and Shift + F6 (refactor).
I looked in the shortcuts section in the built-in help and found this:
This seems like the option you suggested, but that command doesn't work and I can't find it in any of the menus either.
Thanks for your patience, somehow I still haven't got it working.



When I execute the command it doesn't use PHPUnit but the normal php run configuration and I obviously get this error:
/Applications/XAMPP/xamppfiles/bin/php /Applications/XAMPP/xamppfiles/htdocs/myproject/protected/tests/functional/EventTest.php
Fatal error: Class 'WebTestCase' not found in /Applications/XAMPP/xamppfiles/htdocs/myproject/protected/tests/functional/EventTest.php on line 10
Call Stack: 0.0003 322936 1. {main}() /Applications/XAMPP/xamppfiles/htdocs/myproject/protected/tests/functional/EventTest.php:0
Process finished with exit code 255
This is obvious because the WebTestCase class gets included in the bootstrap file, which isn't executed with this configuration. I made two full window screenshots:
Before Test:
After I executed "Run context configuration"
Oh and if it matters (since it doesn't get applied anyway), my PHPUnit configuration, which works if I really want to run all tests:
Hi Daniel,
On your last screenshot -- expand "Defaults" on the left side list, find "PHPUnit" option and fill the "Configuration file" and "Bootstrap file" fields. Then, when you use "Run context configuration", the autocreated run configuration will be created with those fields already filled, which should solve the "Class not found" issue for you (as long as everything else is working fine/configured properly).
Alternatively, try adding path to your bootstrap file into your phpunit.xml . For example:
Regarding this:
You choosing wrong Run Configuration type -- you have chosen the normal PHP Script, while you need PHPUnit (they have different icons -- PHPUnit has that green tick icon).
Sometimes PhpStorm is unable to properly identify that the file is actually a PHPUnit test and offers only normal "PHP Script" or similar type option. I recommend finding this file in Project View panel, right click and choose approperiate option from "Run" submenu -- somehow PhpStorm displays more appropriate run options there (quite likely it foorces to re-analyze that file so it can detect that this is PHPUnit test file). After first run the temporary run configuration will be created and you can continue using it via that "Run context configuration" shortcut (works fine for me).
I have already filled in the bootstrap path and phpunit configuration path in the "Defaults" tab. Also my phpunit.xml contains the path to the bootstrap file.
Hello Daniel,
What is the parent class for the 'WebTestCase' class?
Thank you for feedback!
Hello Andriy,
Thanks for your help!
BTW, I have created an issue about moving these settings to the project configuration (I expect it will be move intuitive) - http://youtrack.jetbrains.net/issue/WI-9045
Could you please provide any example?
Thank you for feedback!
The parent class of WebTestCase is CWebTestCase, a part of the Yii Framework. The corresponding files are included in the bootstrap.php file.
Okay, I even made a video now.
This is what I did before:
1. Webroot is in /Applications/XAMPP/htdocs
2. Yii Framework (http://www.yiiframework.com) is in /Applications/XAMPP/htdocs/Yii
3. Executed "/Applications/XAMPP/htdocs/Yii/yiic webapp /Applications/XAMPP/htdocs/phpstorm" which creates a skeleton website + some tests.
http://www.vidup.de/v/PLqp9/
This is what I did in the video (unfortunately I don't have a working microphone):
0:00 - 0:17 This is my configuration for PHPUnit
0:17 - 0:24 I used Alt + Shift + F6 to "Run context configuration"
0:24 - end That's the content of the involved files.
Oh and by the way this is what I get when I just click on "Run PHPUnit" so all tests are executed:
However all the tests get executed. What also seems kind of strange is the path to phpunit that's used here.
Hi Nikolay,
1) I have like 2 "normal" PHP files already opened in editor;
2) Open a PHPUnit test file in editor (I just double clicked on such file in Project view panel). This makes 3 opened tabs;
3) Right click in editor and see what options you will get under "Create Run configuration". From what I see it greatly depends on where in the editor you currently have your cursor:
4) If you right click on the same file but in Project view panel, you will see "PHPUnit" for that class and "PHPUnit on Server" for that class options straight away.
Now, my comment about "Sometimes PhpStorm is unable to properly identify that the file is actually a PHPUnit test and offers only normal "PHP Script" or similar type option." means that I would expect to see "PHPUnit for class/whole file" option always available regardless of cursor position in editor and I definitely do not expect to see "PHP Script" etc option for such file.
BTW -- I think I really should create a ticket for this, but would rather confirm this with you first while we are on this subject:
Question: why there is no "All in File" scope/option available for local PHPUnit configuration ... or why it's only available for PHPUnit on Server ?? I do have 1 test class per file, so it does not affect me that much, but still...
Yes, together with other Unit testing settings. But I hope you will still leave those fields available .. as sometimes you may need completely different test setup which requires different config file (e.g. most tests will do fine without Process Isolation (much faster tests execution), but some do rely on this, so you end up creating 2 configs and run them one after another). I personally do not need it in my projects (so far), but have seen it done like that in 2 open source projects (maybe there is another way of doing this, but that is what I have seen).
Daniel,
1) Problem with automatical creation of the test run configuration:
The base class for 'CWebTestCase' is 'PHPUnit_Extensions_SeleniumTestCase'. Please add 'PHPUnit_Selenium' package to your include path. In your case PhpStorm can't understand that 'PHPUnit_Extensions_TestCase' is inheritor of the 'PHPUnit_Framework_TestCase' and therefore don't suggest to create 'PHPUnit' run configuration instead of 'PHP Script'.
2) Problem with warning console message:
This message means that PHP interpreter can't find this class in your include path. You need to install 'DBUnit' package.
You can install these packages from PEAR or manually from github - https://github.com/sebastianbergmann/
Thank you for feedback!
Andriy,
Please submit separate issues for every case. Thanks!
In PhpStorm 3.0 we rewrote runner of 'PHPUnit' run configuration and it is just wrapper for standard console test runner now. Standard console test runner doesn't support 'File' scope and this is why we removed this check box from run configuration editor dialog (also I never have meet 2 test classes in a single file). 'PHPUnit On Server' is still on old custom test runner, because of some technical reasons.
You can overwrite process isolation flag using 'Test Runner Options' field in the 'PHPUnit' run configuration.
Thank you for feedback!
I did everything what you said, but it still didn't work. However I realized that the path to my Yii framework was still a windows path (I exported it from there). So probably phpStorm didn't find CWebTestCase and didn't even look for SeleniumTestCase.php. Since my application itself works with just relative paths it wasn't a problem if all unit tests were run.
But finally, the most important thing: IT WORKS NOW! Thank you a thousand times.
Thanks Nikolay.
Done:
I see -- thanks for explanation.
Yes, but this is PhpStorm-specific solution. What if there are more than one person working on the same project/tests using different IDE ... or author wants to publish his/her library/project open source (or something like that) so that everyone can run PHPUnit tests if needed on their own computers .. or to be used with some Continuous Integration server/service (Hudson etc). Native PHPUnit implementation (via config files) is the way to go here.
Andriy,
Thanks a lot!
Ok, it could be discussed later on (directly in the correspondent issue) when we start to refactor GUI.
Thank you for feedback!