简体   繁体   English

PHPStorm,单元测试当前的类

[英]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? 我正在尝试从NetBeans迁移到PHPStorm,而我无法弄清楚的是:如何在不为每个类单独创建运行配置的情况下为当前类(可能是哪个)运行我的测试套件?

In NetBeans, you can run your entire suite with Alt+F6 or run the current class with just F6. 在NetBeans中,您可以使用Alt + F6运行整个套件,或仅使用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. 我无法弄清楚如何在PHPStorm中执行此操作而不为项目中的每个类创建新的运行配置,我显然不想这样做。

First You have to tell PHPStorm where do You store your tests: 首先您必须告诉PHPStorm您存储测试的位置:

  1. Settings->Directories mark your tests folder as tests 设置 - >目录将您的测试文件夹标记为测试
  2. Configure Settings->PHP->PHPUnit 配置设置 - > 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: 这个帖子给了我很多帮助,我很感谢Jakub的答案,但我不得不做更多的挖掘,所以我想我会尝试进一步研究它:

  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 文件>设置>语言和框架> PHP

    • Make sure your php language level and CLI interpreter are setup right 确保您的PHP语言级别和CLI解释器设置正确
  3. File > Settings > Languages & Frameworks > PHP > PHPUnit 文件>设置>语言和框架> PHP> PHPUnit

    • assuming your using composer select use Composer autoloader and set the path to yourprojectroot/vendor/autoload.php 假设您使用composer选择使用Composer自动加载器并设置您的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 或者您可以选择路径到phpunit.phar并导航到vendor/bin/phpunit ,然后选中Default bootstrap文件旁边的框并将其设为vendor/autoload.php的路径

  4. Open any phpunit test file in the editor and press Ctrl Shift F10 to run it 在编辑器中打开任何phpunit测试文件,然后按Ctrl Shift F10运行它

    • 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 不确定mac的键绑定,但是如果单击Run> Run ...也可以,它会告诉你子菜单中的keybind

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. 在项目文件资源管理器中,如果选择任何测试目录或测试文件并按Ctrl Shift F10 ,它将运行该文件或目录中的所有测试文件。

  • 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. Shift F10 - 将重新运行您最后运行的任何测试,因此如果您正在进行TDD并且之前执行了测试并且您正在测试的类中工作,您可以进行更改,只需在每次更改后按Shift F10并重新执行运行以前运行的测试,无需选择测试重新运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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