简体   繁体   中英

PHPStorm, unit testing the current class

I'm trying to migrate from NetBeans to PHPStorm and the thing I cannot figure out is: how do I run my test suite for the current class (which ever that might be) without creating a run configuration for each class separately?

In NetBeans, you can run your entire suite with Alt+F6 or run the current class with just F6. This enables you to run just the tests you need and not your entire suite all the time which is obviously a time saver.

I cannot figure out how to do this in PHPStorm without creating a new run configuration for each and every class in the project which I obviously do not want to do.

First You have to tell PHPStorm where do You store your tests:

  1. Settings->Directories mark your tests folder as tests
  2. Configure Settings->PHP->PHPUnit
  3. Right click on any test in Your test dir and choose "Run" it will create test configuration for this particular test file on the fly.

You can even right click on a single test method and choose "Run" (or Debug), Same for any test folder (if you have nested tests directories).

This thread helped me a lot and I was thankful for Jakub's answer however I had to do additional digging so I thought I would try to take it a bit further:

  1. File > settings > Directories

    • select any directories containing tests (perhaps yourprojectroot/tests) and click Mark as: Tests
    • note that you can repeat this step if you have multiple directories containing tests and that the selection will include sub directories so no worries.
  2. File > Settings > Languages & Frameworks > PHP

    • Make sure your php language level and CLI interpreter are setup right
  3. File > Settings > Languages & Frameworks > PHP > PHPUnit

    • assuming your using composer select use Composer autoloader and set the path to yourprojectroot/vendor/autoload.php

    • or you can select Path to phpunit.phar and navigate to vendor/bin/phpunit , then check the box next to Default bootstrap file and make that the path to vendor/autoload.php

  4. Open any phpunit test file in the editor and press Ctrl Shift F10 to run it

    • Not sure about the key bind for mac but if you click Run > Run... that will work also, and it will tell you the keybind in the submenu

Additional Tips : Since posting this and using the feature I learned a few more helpful things and thought I would just add them here:

  • In the project file explorer if you select any test directory or test file and do Ctrl Shift F10 it will run that file or all test files in the directory.

  • Shift F10 - will re-run whatever test you ran last, so if you are doing TDD and previously executed the test and you are working in the class being tested you can make a change and just press Shift F10 after each change and it re-runs the previously run test, no need to select the test to re-run it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM