Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

A simple example of unit testing through PHPUnit in PhpStorm

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "a simple example of unit testing through PHPUnit in PhpStorm". In daily operation, I believe that many people have doubts about the simple example of unit testing through PHPUnit in PhpStorm. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the questions of "simple examples of unit testing through PHPUnit in PhpStorm". Next, please follow the editor to study!

Run tests on specified directories, classes, and methods

The test demonstration classes that come with Laravel are located in the tests subdirectory under the root of the project. In PhpStorm, the directory is automatically marked green, indicating that it is a test directory:

Unit testing based on PHPUnit in PhpStorm is very simple. PhpStorm has built-in support for running test cases through local PHPUnit (phpunit dependency has been installed when the Laravel project is initialized, and PhpStorm will unit test the project based on this phpunit). No additional configuration is required, just select the directory, file, and method to be tested, and then select Run Test from the right-click drop-down menu (as long as you write the corresponding test case code first):

The test results are displayed in the Run window below PhpStorm:

If your PhpStorm version is older and cannot automatically recognize PHPUnit and create a corresponding test framework configuration, you can create it manually in the Preferences | Languages & Frameworks | PHP | Test Frameworks interface (replace Preferences with Settings for Windows system). Select PHPUnit Local when you create it, and then:

The script path maps to the vendor/autoload.php at the current project root, and the configuration file maps to the phpunit.xml at the current project root.

If you want to use other test frameworks (Behat, PHPSpec, Codeception, etc.), you can also create the corresponding test framework configuration here:

To test the specified file, you can also right-click anywhere in the code editing area where the file is opened and select the test code that runs the file:

Or click the green run test icon on the left side of the line where the test class is declared to test:

You can also do the same for testing a specified method:

If the test is successful, the result of the run is green, and if the test fails, it is prompted in red:

And it will identify the cause and location of the error, which is convenient for troubleshooting.

PHPUnit Code coverage Analysis

When you run the test case above, careful students may have seen that in the drop-down menu, in addition to the option to run the test, there is also a test option with coverage:

PHPUnit's code coverage analysis uses the PHP_CodeCoverage component, and this component uses the code coverage feature provided by the PHP Xdebug extension, so to use the code coverage feature, you need to install the Xdebug extension first. If the system does not install the extension, you can refer to the previous installation and configuration Xdebug extension tutorial for installation and setup.

To tell PHPUnit which source code files to include in the code coverage report, you must configure a whitelist. This configuration is already included in the phpunit.xml that comes with Laravel (located in the project root directory):

. / app

The above configuration shows that all PHP code with the suffix .php under the app directory will be included in the code coverage report, even if some files are not covered by the test case, you can also know which lines are executable in the unoverwritten file, in addition, you can also configure which code blocks to skip. For more setting details, please refer to the PHPUnit official documentation.

We run the test containing code coverage on the tests directory, and after the run is complete, a window containing the test code coverage analysis pops up in the right area of PhpStorm:

Click the app directory to see the code coverage of all subdirectories:

In addition, we click on the left Project area to expand the project directory structure tree, and we can also see the test code coverage:

In the code editing area, you can see a green bar on the left side of the covered code and a red bar on the left side of the uncovered code.

At this point, the study on "simple examples of unit testing through PHPUnit in PhpStorm" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report