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

What is the configuration and use of PHPUnit under Windows

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article will explain in detail about the configuration and use of PHPUnit under Windows. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Since our project involves php, we need to unit test the php code. After some understanding, I decided to use PHPUnit to test php. PHPUnit spent a lot of time figuring out how to configure PHPUnit, and the documentation on the official website was a tearful one. But after knowing how to configure it, it is actually very simple.

System: Windows 10 Professional Edition

PHP version: PHP 5.5.12

Server tools: WAMPSERVER 2.5

PHPUnit version: PHPUnit 4.8

1. Configure PHPUnit

First of all, go to the PHPUnit official website (click here to enter) to download the corresponding version. We used php 5.5, so we chose PHPUnit 4.8. Get the .phar file and change the name to phpunit.phar.

Put the file anywhere. Taking myself as an example, I put it in the directory of our project, D:\ repository\ CourseManagement\ mobile_api_test.

Right-click my computer and select Properties. Click Advanced system Settings on the left. At this point, pop up the system properties, select the Advanced tab, and click the environment variable in the lower right corner. In the user variable, double-click PATH and add after the variable value; D:\ repository\ CourseManagement\ mobile_api_test (note the leading semicolon, the path is filled with the path where the phpunit.phar is stored). This is configured so that PHPUnit can be used anywhere. If it is not configured, you need to use PHPUnit under the path of phpunit.phar.

One thing that is not mentioned in the official documentation is that the environment variable for php should also be set. For example, if my php.exe is in E:\ software\ wamp\ bin\ php\ php5.5.12, it should also be added after PATH; E:\ software\ wamp\ bin\ php\ php5.5.12. PS: maybe it's because I don't have PHP's IDE installed, so I haven't configured it all the time, and I happen to think that I might want to add this environment variable.

Press the shortcut key Win + R, enter cmd and enter enter. Enter the path where the phpunit.phar is stored. Enter echo @ php "% ~ dp0phpunit.phar"% * > phpunit.cmd and enter. Then type phpunit-- version and enter. If you get the output PHPUnit x.y.z by Sebastian Bergmann and contributors. It means that the configuration is ready (if there is a mistake, enter exit and enter and start all over again). As shown below:

Second, use PHPUnit for testing

To use PHPUnit, you must use classes. Take login.php as an example (located at D:\ repository\ CourseManagement\ mobile_api), our initial version looks like this (feel free to complain about the code in this blog):

This can not be tested, so make a revision. First, create a folder classes in the folder where login.php is located, and create a new class_login.php in it. The content is the modified version of login.php:

In addition, the content of the original login.php should also be modified as follows:

Login ($workNumber,$password,$tableName); if ($response! = "false") {session_start (); $_ SESSION ['id'] = $tableName;} echo $response; >

Start writing test files

I put the test files in the folder D:\ repository\ CourseManagement\ mobile_api_test. Create a new file `login_test.php' and write the following code:

Login ($workNumber,$password,$tableName); $this- > assertEquals ($expected,$actual);} function testLoginFail () {$expected = 'false'; $workNumber =' 11111clients; $password = '11111clients; $tableName =' user_teacher'; $lg = new Login; $actual = $lg- > login ($workNumber,$password,$tableName); $this- > assertEquals ($expected,$actual) }} >

Execute the test file

Shortcut key Win + R, type cmd and enter enter. Enter the directory of the test file and type phpunit login_test.php to execute the test.

The simple test is complete.

Third, the groping process

At first, I downloaded PHPUnit and got the .phar file. I thought I was going to decompress it. It took a long time to find a website that can extract such files (click here to enter). However, it is of no use.

Following the official documentation, an error occurred at run time:

Php' is not an internal or external command, nor is it a runnable program

Or batch files.

Google search, Bing search, StackOverFlow search, Baidu search, the answers found are useless.

Mainly because they all default that you have configured the PHP environment variable.

* do you think there is something wrong with the previously generated phpunit.cmd? So take a look at the contents of this file. It suddenly occurred to me that the PHP environment variable was not set. Open cmd and enter php-- version. Get:

Php' is not an internal or external command, nor is it a runnable program

Or batch files.

It's the same mistake as above! That's the problem here. So add; E:\ software\ wamp\ bin\ php\ php5.5.12 to the environment variable. Then run php-- version to get:

PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) Copyright (c) 1997-2014 The PHP Group Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans

Then go to the folder where phpunit.cmd is located and run phpunit-- version. Get:

PHPUnit 4.8.18 by Sebastian Bergmann and contributors.

Problem solved!

After this exploration, when you encounter the problem of "can't find xxx" in the future, you will first think of the setting of environment variables.

For example, on the same afternoon when I solved this problem, I wanted to use Visual Studio Code's Git function, but got a hint:

The response was: I obviously installed msysgit.

The second reaction is: could it be that the environment variables are not configured? Open the environment variable configuration, and sure enough, there is no. So add the path to the folder where git.exe is located. Restart visual Studio Code, problem solved!

On the configuration and use of PHPUnit in Windows is how to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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

Development

Wechat

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

12
Report