PhpStorm writes and runs tests

Tram Ho

PhpStorm writes and runs tests

Bài viết lấy ví dụ với framwork Laravel .

PhpStorm is a great tool for writing php code. And it also supports most user needs including writing tests.

You’re tired of waiting for Unittest to run when you run ./vendor/bin/phpunit . Or too tired to have to write additional parameters passed to it

Test 1 class:

Test 1 method:

Run the test with namespace:

vendor/bin/phpunit --filter 'Tests\Feature'

Each time you run a function, you have to edit the name to correct the path, 😱 Oh God why not make it faster, that’s a saying when you don’t have PhpStorm. Once you have PhpStorm, writing test and run test becomes very simple, just by clicking a button before the function is done!

Here I will introduce this wonderful feature of PhpStorm

How to write down

There are 2 ways to write unittest with PhpStorm: after cấu hình xong viết is cấu hình xong viết or viết xong cấu hình .

Cấu hình xong viết :

First you need a project such as Laravel

Open the project with the PhpStorm tool.

PHP configuration: Choose File -> Setting -> PHP from the toolbar

At CLI Interpreter choose PHP 7.1 (hoặc 7.2 ...) instead of <no interpreter> .

If you do not have CLI , you can install it using google 😁

Next is the configuration of Test Frameworks . Continue to open the PHP tab and select the Test Frameworks tab.

There will be 3 ways to configure phpunit that you can use:

  • Use Composer autoloader : Use the composer’s automatic loader to configure
  • Path to phpunit.phar : Use the phpunit.phar file to configure
  • Load form include path (PEAR) : Not configured. It will be configured individually when running each test file using include() or normally using require()

Usually I will choose option 1 or 2 because method 3 cannot be used 😅

When configuring without phpunit.phar phpstorm file will give you 3 options:

  • The first and easiest way is to Reload phpinfo if you have installed and phpstorm can find it, this IDE will update the path for you automatically.
  • Option 2 is to choose or fill the path yourself 😅
  • And if you do not have the file, then use the 3-way Download phpunit.phar... just below the box.

With method 3, there are two cases you can use it, it is to load down into a certain folder, usually /usr/local/bin for convenient use for other projects. But the problem is that if you download it, only your computer can run and the other device cannot be used. So I usually download directly to the project directory but it will be limited because every time I have to reconfigure.

How to write tests so quickly with PhpStorm (*)

Select the file to write the test like HomeController.php Right-click the file name -> New -> PHP Test -> PHPUnit Test.

Configure New PHP Test:

I mainly adjust the Directory section – the path to create test files (If you do not have a directory, just edit the path of the name of the new directory, the IDE will create the directory and add files to it for you) 😍 ) and Member – functions you’ll write tests.

The IDE will generate the test file from the file to test:

Can you see the green button next to the newly created function or class ?? This is what it would be more convenient to run the test just by clicking.

Initially, it will produce PHPUnit 8.4.3 by Sebastian Bergmann and contributors. This test did not perform any assertions...

Simply because your test has no content 😜 . You can add $this->assertTrue(true); Get into the function to test the result!

Reverse writing

It is finished writing a new configuration. It sounds a bit reverse, but writing the configuration is still ok, guys, because PhpStorm is quite smart, it will guide me to fix errors when it fails to run the test.

Initially you generate the test file from the file to be tested (see section *). Still clicking test, but now instead of running the IDE will fire a configuration dialog for you.

There is a bold button Fix , click on it and reconfigure as above. That’s it then. If you want to edit the configuration file, you are currently in another test file. You can click Edit Configurations select the file to edit and edit.

Bonus

The IDE generates the file itself, so it should know where it came from! 🙃 Of course it is.

To return from a test function to the function it needs to test we can choose the way:

  • Hover the mouse at the name of the test function (eg test__construct )
  • Right-click -> Select Go To -> select Test Subject (or simply Ctl + Shift + T )

(For those who need – By the way, the bonus of adding a human image on the image is due to the IDE’s background configuration).

summary

Thank you everyone for taking the time to read your post. 😘

Share the news now

Source : Viblo