Run phpunit for current file

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

16 comments

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.

0

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:

Command Alt F6 Switch to another coverage suite. Open the Coverage Suites popu-up                  window and select the desired suite to run.



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.

0

run_test1.JPGNo, it's Run context configuration:

0

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:
BeforeTest.jpeg
After I executed "Run context configuration"
AfterTest.jpeg

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:
phpunit_config.jpg

0

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:

<?xml version="1.0"?>
<phpunit backupGlobals="false"
         backupStaticAttributes="true"
         bootstrap="./bootstrap.php"
         colors="false"
         convertErrorsToExceptions="true"
         convertNoticesToExceptions="false"
         convertWarningsToExceptions="false"
         processIsolation="false"
         stopOnFailure="false"
         strict="true"
         syntaxCheck="false"
         verbose="true">

....

</phpunit>

Regarding this:

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:

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).

0

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.     

0

Hello Daniel,

What is the parent class for the 'WebTestCase' class?

Thank you for feedback!

0

Hello Andriy,

Thanks for your help!

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).

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

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.

Could you please provide any example?

Thank you for feedback!

0

The parent class of WebTestCase is CWebTestCase, a part of the Yii Framework. The corresponding files are included in the bootstrap.php file.

0

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:

/Applications/XAMPP/xamppfiles/bin/php /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php --bootstrap /Applications/XAMPP/xamppfiles/htdocs/phpstorm/protected/tests/bootstrap.php --configuration /Applications/XAMPP/xamppfiles/htdocs/phpstorm/protected/tests/phpunit.xml /Applications/XAMPP/xamppfiles/htdocs/phpstorm/protected/tests
Testing started at 12:50 ...
PHPUnit 3.6.4 by Sebastian Bergmann.

Configuration read from /Applications/XAMPP/xamppfiles/htdocs/phpstorm/protected/tests/phpunit.xml


Warning: include(PHPUnit_Extensions_Database_TestCase.php): failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/Yii/YiiBase.php on line 421

Call Stack:
    0.0011     425192   1. {main}() /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php:0
    0.0076     931740   2. IDE_PHPUnit_TextUI_Command::main() /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php:386
    0.0076     934972   3. PHPUnit_TextUI_Command->run() /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php:194
    0.0664    5191768   4. PHPUnit_TextUI_TestRunner->doRun() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/TextUI/Command.php:187
    0.0685    5353752   5. PHPUnit_Framework_TestSuite->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/TextUI/TestRunner.php:325
    0.0686    5354096   6. PHPUnit_Framework_TestSuite->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:705
    0.0687    5354284   7. PHPUnit_Framework_TestSuite->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:705
    0.0688    5354500   8. PHPUnit_Framework_TestSuite->runTest() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:745
    0.0688    5354500   9. PHPUnit_Extensions_SeleniumTestCase->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:772
    0.0688    5354528  10. PHPUnit_Framework_TestResult->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Extensions/SeleniumTestCase.php:561
    3.8349    5536164  11. PHPUnit_Framework_TestResult->endTest() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestResult.php:742
    3.8350    5536496  12. PHPUnit_Util_Test::getSize() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestResult.php:390
    3.8350    5536992  13. class_exists() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Util/Test.php:427
    3.8351    5537288  14. YiiBase::autoload() /Applications/XAMPP/xamppfiles/htdocs/Yii/YiiBase.php:0


Warning: include(): Failed opening 'PHPUnit_Extensions_Database_TestCase.php' for inclusion (include_path='.:/Applications/XAMPP/xamppfiles/htdocs/phpstorm/protected/components:/Applications/XAMPP/xamppfiles/htdocs/phpstorm/protected/models:/Applications/XAMPP/xamppfiles/lib/php:/Applications/XAMPP/xamppfiles/lib/php/pear') in /Applications/XAMPP/xamppfiles/htdocs/Yii/YiiBase.php on line 421

Call Stack:
    0.0011     425192   1. {main}() /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php:0
    0.0076     931740   2. IDE_PHPUnit_TextUI_Command::main() /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php:386
    0.0076     934972   3. PHPUnit_TextUI_Command->run() /private/var/folders/b6/qrfsvbbj6rb8g37pd7p26b340000gn/T/ide-phpunit.php:194
    0.0664    5191768   4. PHPUnit_TextUI_TestRunner->doRun() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/TextUI/Command.php:187
    0.0685    5353752   5. PHPUnit_Framework_TestSuite->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/TextUI/TestRunner.php:325
    0.0686    5354096   6. PHPUnit_Framework_TestSuite->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:705
    0.0687    5354284   7. PHPUnit_Framework_TestSuite->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:705
    0.0688    5354500   8. PHPUnit_Framework_TestSuite->runTest() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:745
    0.0688    5354500   9. PHPUnit_Extensions_SeleniumTestCase->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestSuite.php:772
    0.0688    5354528  10. PHPUnit_Framework_TestResult->run() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Extensions/SeleniumTestCase.php:561
    3.8349    5536164  11. PHPUnit_Framework_TestResult->endTest() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestResult.php:742
    3.8350    5536496  12. PHPUnit_Util_Test::getSize() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Framework/TestResult.php:390
    3.8350    5536992  13. class_exists() /Applications/XAMPP/xamppfiles/lib/php/pear/PHPUnit/Util/Test.php:427
    3.8351    5537288  14. YiiBase::autoload() /Applications/XAMPP/xamppfiles/htdocs/Yii/YiiBase.php:0



However all the tests get executed. What also seems kind of strange is the path to phpunit that's used here.

0

Hi Nikolay,

Could you please provide any example?

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:

  • if cursor is inside setUp() or tearDown() methods (the 2 standard methods that I use in every test file), then I see only "JavaScipt debug" and "PHP Script" options (the whole project is local, nothing is remote). This is very unexpected behaviour in my opinion;
  • if cursor is inside any test method (e.g. testXXXXX() ), then I have 2 options: "PHPUnit" for that method and "PHPUnit on Server" for that method. This makes sense and is very handy, but I still would expect to see an option for the whole file or class;
  • if cursor is outside of any method, then I see these options: "PHPUnit" for that class and "PHPUnit on Server" for that class


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:

  • PHPUnit on Server configuration type has these scopes/options: All inDirectory, All in File, Class or Suite, Method, XML File;
  • at the same time PHPUnit configuration type has: All In Directory, Class, Method, Defined in configuration file.


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...

BTW, I have created an issue about moving these settings to the project configuration (I expect it will be move intuitive)

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).
0

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!

0

Andriy,

  • if cursor is inside setUp() or tearDown() methods (the 2 standard methods that I use in every test file), then I see only "JavaScipt debug" and "PHP Script" options (the whole project is local, nothing is remote). This is very unexpected behaviour in my opinion;
  • if cursor is inside any test method (e.g. testXXXXX() ), then I have 2 options: "PHPUnit" for that method and "PHPUnit on Server" for that method. This makes sense and is very handy, but I still would expect to see an option for the whole file or class;

Please submit separate issues for every case. Thanks!

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:

  • PHPUnit on Server configuration type has these scopes/options: All inDirectory, All in File, Class or Suite, Method, XML File;
  • at the same time PHPUnit configuration type has: All In Directory, Class, Method, Defined in configuration file.


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...


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.

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).

You can overwrite process isolation flag using 'Test Runner Options' field in the 'PHPUnit' run configuration.

Thank you for feedback!

0

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.

0

Thanks Nikolay.

Please submit separate issues for every case. Thanks!

Done:

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.

I see -- thanks for explanation.

You can overwrite process isolation flag using 'Test Runner Options' field in the 'PHPUnit' run configuration.

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.

0

Andriy,

Done:

Thanks a lot!

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.


Ok, it could be discussed later on (directly in the correspondent issue) when we start to refactor GUI.

Thank you for feedback!

0

Please sign in to leave a comment.