Table of Contents
PHPUnit 在 Windows 下的配置及使用教程
Home php教程 php手册 PHPUnit 在 Windows 下的配置及使用教程

PHPUnit 在 Windows 下的配置及使用教程

Jun 13, 2016 am 08:50 AM
phpunit windows Configuration

PHPUnit 在 Windows 下的配置及使用教程

由于我们项目涉及到php,因此需要对php代码进行单元测试。经过一番了解,决定用PHPUnit来测试php。PHPUnit花了不少时间摸索如何配置PHPUnit,看官网的文档也是一把泪。但知道怎么配置后,其实还是很简单的。

  • 系统:Windows 10 专业版

  • PHP版本:PHP 5.5.12

  • 服务器工具:WAMPSERVER 2.5

  • PHPUnit版本:PHPUnit 4.8

一、配置PHPUnit

首先到PHPUnit官网点此进入)下载相应的版本。我们使用的是php 5.5,于是选择PHPUnit 4.8。得到 .phar 文件,并把名字改为 phpunit.phar 。

把该文件放到任意位置。以我自己为例,我把它放到我们项目的目录下,即D:\repository\CourseManagement\mobile_api_test 。

右键 我的电脑 ,选择 属性 。点击左侧的 高级系统设置 。此时弹出 系统属性 ,选择 高级 选项卡,点击右下角的 环境变量 。 在用户变量里面,双击 PATH ,在 变量值 后面添加;D:\repository\CourseManagement\mobile_api_test 注意最前面的分号,路径填存放phpunit.phar的路径)。配置这个是为了在任意位置都能使用PHPUnit,不配置的话,需要在phpunit.phar的路径下才能使用PHPUnit。

官方文档里有一点没有提到,就是php的环境变量也要设置。比如我的 php.exe 在E:\software\wamp\bin\php\php5.5.12 里,那么也要在 PATH 后面添加;E:\software\wamp\bin\php\php5.5.12 。PS:可能是因为我没有安装PHP的IDE,所以一直没有配置它,偶然之间才想到可能要添加这个环境变量。

按快捷键 Win + R ,输入cmd并回车。进入存放phpunit.phar的路径。输入 echo @php "%~dp0phpunit.phar" %* > phpunit.cmd 并回车。接着输入 phpunit --version 并回车。如果得到输出 PHPUnit x.y.z by Sebastian Bergmann and contributors. 则表示配置好了如果有误,输入exit 并回车,重新来一遍)。如下图:

二、使用PHPUnit进行测试

使用PHPUnit,必须使用  。以login.php为例位置在D:\repository\CourseManagement\mobile_api ),我们的初始版本是这样的欢迎吐槽本博客内的代码):

<ol class="dp-c"><li class="alt"><span><span><php </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="func">error_reporting</span><span>(0); </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$workNumber</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">"login-user"</span><span>]; </span></span></li><li><span>    <span class="vars">$password</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">"login-password"</span><span>]; </span></span></li><li class="alt"><span>    <span class="vars">$tableName</span><span> = </span><span class="vars">$_POST</span><span>[</span><span class="string">"ident"</span><span>]; </span></span></li><li><span> </span></li><li class="alt"><span>    <span class="vars">$con</span><span> = mysqli_connect(</span><span class="string">"localhost"</span><span>, </span><span class="string">"root"</span><span>, </span><span class="string">""</span><span>, </span><span class="string">"teacher_class_system"</span><span>); </span></span></li><li><span>    <span class="keyword">if</span><span> (!</span><span class="vars">$con</span><span>) { </span></span></li><li class="alt"><span>        <span class="keyword">die</span><span>(</span><span class="string">'Could not connect: '</span><span> . mysql_error()); </span></span></li><li><span>    } <span class="keyword">else</span><span> { </span></span></li><li class="alt"><span>        mysqli_query(<span class="vars">$con</span><span>, </span><span class="string">"SET NAMES utf8"</span><span>); </span></span></li><li><span> </span></li><li class="alt"><span>        <span class="vars">$result</span><span> = mysqli_query(</span><span class="vars">$con</span><span>, </span><span class="string">"SELECT * FROM $tableName where workNumber = $workNumber and password = $password"</span><span>); </span></span></li><li><span>        <span class="keyword">if</span><span> (mysqli_num_rows(</span><span class="vars">$result</span><span>) < 1) { </span></span></li><li class="alt"><span>                <span class="func">echo</span><span> </span><span class="string">"false"</span><span>; </span></span></li><li><span>        } <span class="keyword">else</span><span> { </span></span></li><li class="alt"><span>                <span class="vars">$result_arr</span><span> = mysqli_fetch_assoc(</span><span class="vars">$result</span><span>); </span></span></li><li><span>                <span class="func">echo</span><span> json_encode(</span><span class="vars">$result_arr</span><span>, JSON_UNESCAPED_UNICODE); </span></span></li><li class="alt"><span>        } </span></li><li><span>    } </span></li><li class="alt"><span> </span></li><li><span>    > </span></li></ol>
Copy after login

这样没法测试,于是进行一翻修改。首先在 login.php 所在文件夹里建立一个文件夹 classes,并在里面新建 class_login.php ,内容为 login.php 修改后的版本:

<ol class="dp-j"><li class="alt"><span><span><php </span></span></li><li><span> </span></li><li class="alt"><span>   <span class="keyword">class</span><span> Login { </span></span></li><li><span>       <span class="comment">// 测试工具PHPUnit要求一定要在这里给变量默认值,于是默认为空。</span><span> </span></span></li><li class="alt"><span>       <span class="keyword">public</span><span> function login($workNumber = </span><span class="string">""</span><span>,$password = </span><span class="string">""</span><span>,$tableName = </span><span class="string">""</span><span>) {        </span></span></li><li><span>           $con = mysqli_connect(<span class="string">"localhost"</span><span>, </span><span class="string">"root"</span><span>, </span><span class="string">""</span><span>, </span><span class="string">"teacher_class_system"</span><span>); </span></span></li><li class="alt"><span>           <span class="keyword">if</span><span> (!$con) { </span></span></li><li><span>               die(<span class="string">'Could not connect: '</span><span> . mysqli_error()); </span></span></li><li class="alt"><span>           } <span class="keyword">else</span><span> { </span></span></li><li><span>               mysqli_query($con, <span class="string">"SET NAMES utf8"</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span>               $result = mysqli_query($con, <span class="string">"SELECT * FROM $tableName where workNumber = $workNumber and password = $password"</span><span>); </span></span></li><li class="alt"><span>               <span class="keyword">if</span><span> (!$result || mysqli_num_rows($result) == </span><span class="number">0</span><span>) { </span></span></li><li><span>                   <span class="keyword">return</span><span> </span><span class="string">"false"</span><span>; </span></span></li><li class="alt"><span>               } <span class="keyword">else</span><span> { </span></span></li><li><span>                   $result_arr = mysqli_fetch_assoc($result); </span></li><li class="alt"><span>                   <span class="keyword">return</span><span> json_encode($result_arr, JSON_UNESCAPED_UNICODE); </span></span></li><li><span>               } </span></li><li class="alt"><span>           } </span></li><li><span>       } </span></li><li class="alt"><span>   } </span></li><li><span> </span></li><li class="alt"><span>   > </span></li></ol>
Copy after login

除此之外,也要修改原来那个 login.php 的内容,修改后如下:

<ol class="dp-j"><li class="alt"><span><span><php </span></span></li><li><span>  error_reporting(<span class="number">0</span><span>); </span></span></li><li class="alt"><span> </span></li><li><span>  require_once <span class="string">'./classes/class_login.php'</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span>  $workNumber = $_POST[<span class="string">"login-user"</span><span>]; </span></span></li><li class="alt"><span>  $password = $_POST[<span class="string">"login-password"</span><span>]; </span></span></li><li><span>  $tableName = $_POST[<span class="string">"ident"</span><span>]; </span></span></li><li class="alt"><span> </span></li><li><span>  $log = <span class="keyword">new</span><span> Login; </span></span></li><li class="alt"><span>  $response = $log->login($workNumber,$password,$tableName); </span></li><li><span> </span></li><li class="alt"><span>  <span class="keyword">if</span><span>($response != </span><span class="string">"false"</span><span>) { </span></span></li><li><span>      session_start(); </span></li><li class="alt"><span>      $_SESSION[<span class="string">'id'</span><span>]=$tableName; </span></span></li><li><span>  } </span></li><li class="alt"><span> </span></li><li><span>  echo $response; </span></li><li class="alt"><span> </span></li><li><span>  > </span></li></ol>
Copy after login

开始写测试文件

我把测试文件放在 D:\repository\CourseManagement\mobile_api_test 这个文件夹内。新建一个文件 `login_test.php’ ,并写入以下代码:

<ol class="dp-j"><li class="alt"><span><span><php </span></span></li><li><span>  require_once dirname(__FILE__).<span class="string">'/../mobile_api/classes/class_login.php'</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span>  <span class="keyword">class</span><span> LoginTest </span><span class="keyword">extends</span><span> PHPUnit_Framework_TestCase { </span></span></li><li class="alt"><span>      <span class="keyword">public</span><span> function testLoginSuccess() { </span></span></li><li><span>          $expected = <span class="string">'{"workNumber":"00001","password":"00001","name":"西瓜","sex":"男","birthday":"20151201","department":"计算机","telephone":"110","email":"git@github.com"}'</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span>          $workNumber = <span class="string">'00001'</span><span>; </span></span></li><li class="alt"><span>          $password = <span class="string">'00001'</span><span>; </span></span></li><li><span>          $tableName = <span class="string">'user_teacher'</span><span>; </span></span></li><li class="alt"><span>          $lg = <span class="keyword">new</span><span> Login; </span></span></li><li><span>          $actual = $lg->login($workNumber,$password,$tableName); </span></li><li class="alt"><span> </span></li><li><span>          $<span class="keyword">this</span><span>->assertEquals($expected,$actual); </span></span></li><li class="alt"><span>      } </span></li><li><span> </span></li><li class="alt"><span>      function testLoginFail() { </span></li><li><span>          $expected = <span class="string">'false'</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span>          $workNumber = <span class="string">'11111'</span><span>; </span></span></li><li class="alt"><span>          $password = <span class="string">'11111'</span><span>; </span></span></li><li><span>          $tableName = <span class="string">'user_teacher'</span><span>; </span></span></li><li class="alt"><span> </span></li><li><span>          $lg = <span class="keyword">new</span><span> Login; </span></span></li><li class="alt"><span>          $actual = $lg->login($workNumber,$password,$tableName); </span></li><li><span>          $<span class="keyword">this</span><span>->assertEquals($expected,$actual); </span></span></li><li class="alt"><span>      } </span></li><li><span>  } </span></li><li class="alt"><span> </span></li><li><span>  > </span></li></ol>
Copy after login

执行测试文件

快捷键 Win + R ,输入cmd并回车。进入该测试文件的目录,输入 phpunit login_test.php 执行测试。

简单的测试就完成了。

三、摸索过程

一开始下载PHPUnit,得到 .phar 文件,以为要解压,囧。找了半天才发现有一个能够解压这种文件的网站点此进入)。然而并没有什么用……

照着官方文档来做,运行时出现错误:

‘php’ 不是内部或外部命令,也不是可运行的程序
或批处理文件。

Google搜索,必应搜索,StackOverFlow搜索,百度搜索,搜到的答案都没有用。

主要是因为他们都默认你已经配置好了PHP环境变量……

最后想着是不是之前生成的 phpunit.cmd 有问题?于是查看一下这个文件的内容。突然想到是不是PHP环境变量没设置的原因?打开cmd,输入 php --version 。得到:

‘php’ 不是内部或外部命令,也不是可运行的程序
或批处理文件。

和上面的错误一样!果然是这里的问题。于是把 ;E:\software\wamp\bin\php\php5.5.12 添加到环境变量中。再运行 php --version 得到:

<ol class="dp-c"><li class="alt"><span><span>PHP 5.5.12 (cli) (built: Apr 30 2014 11:20:58) </span></span></li><li><span>Copyright (c) 1997-2014 The PHP Group </span></li><li class="alt"><span>Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies </span></li><li><span>with Xdebug v2.2.5, Copyright (c) 2002-2014, by Derick Rethans </span></li></ol>
Copy after login

再进入 phpunit.cmd 所在文件夹,运行 phpunit --version。得到:

PHPUnit 4.8.18 by Sebastian Bergmann and contributors.

问题解决!

经过这次的探索,以后碰到”找不到xxx”这种问题的时候,首先会想起环境变量的设置。

比如解决这个问题的同一天下午,我想使用Visual Studio Code的Git功能,但是却得到提示:

第一个反应是:我明明安装了msysgit啊。

第二个反应是:会不会是环境变量没配置?打开环境变量配置,果然没有。于是将 git.exe 所在文件夹的路径添加进去。重启visual Studio Code,问题解决!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

How to speed up the loading speed of PS? How to speed up the loading speed of PS? Apr 06, 2025 pm 06:27 PM

Solving the problem of slow Photoshop startup requires a multi-pronged approach, including: upgrading hardware (memory, solid-state drive, CPU); uninstalling outdated or incompatible plug-ins; cleaning up system garbage and excessive background programs regularly; closing irrelevant programs with caution; avoiding opening a large number of files during startup.

How to distinguish between closing a browser tab and closing the entire browser using JavaScript? How to distinguish between closing a browser tab and closing the entire browser using JavaScript? Apr 04, 2025 pm 10:21 PM

How to distinguish between closing tabs and closing entire browser using JavaScript on your browser? During the daily use of the browser, users may...

How to pull the vertical reference line of PS How to pull the vertical reference line of PS Apr 06, 2025 pm 08:18 PM

Pull vertical guides in Photoshop: Enable ruler view (View &gt; ruler). Hover the mouse over the vertical edge of the ruler, and then the cursor becomes a vertical line with double arrows and hold and drag the mouse to pull out the reference line. Click Delete by dragging the guide, or hovering it into a cross.

Solutions to the errors reported by MySQL on a specific system version Solutions to the errors reported by MySQL on a specific system version Apr 08, 2025 am 11:54 AM

The solution to MySQL installation error is: 1. Carefully check the system environment to ensure that the MySQL dependency library requirements are met. Different operating systems and version requirements are different; 2. Carefully read the error message and take corresponding measures according to prompts (such as missing library files or insufficient permissions), such as installing dependencies or using sudo commands; 3. If necessary, try to install the source code and carefully check the compilation log, but this requires a certain amount of Linux knowledge and experience. The key to ultimately solving the problem is to carefully check the system environment and error information, and refer to the official documents.

MySQL can't be installed after downloading MySQL can't be installed after downloading Apr 08, 2025 am 11:24 AM

The main reasons for MySQL installation failure are: 1. Permission issues, you need to run as an administrator or use the sudo command; 2. Dependencies are missing, and you need to install relevant development packages; 3. Port conflicts, you need to close the program that occupies port 3306 or modify the configuration file; 4. The installation package is corrupt, you need to download and verify the integrity; 5. The environment variable is incorrectly configured, and the environment variables must be correctly configured according to the operating system. Solve these problems and carefully check each step to successfully install MySQL.

【Rust Self-study】Introduction 【Rust Self-study】Introduction Apr 04, 2025 am 08:03 AM

1.0.1 Preface This project (including code and comments) was recorded during my self-taught Rust. There may be inaccurate or unclear statements, please apologize. If you benefit from it, it's even better. 1.0.2 Why is RustRust reliable and efficient? Rust can replace C and C, with similar performance but higher security, and does not require frequent recompilation to check for errors like C and C. The main advantages include: memory security (preventing null pointers from dereferences, dangling pointers, and data contention). Thread-safe (make sure multi-threaded code is safe before execution). Avoid undefined behavior (e.g., array out of bounds, uninitialized variables, or access to freed memory). Rust provides modern language features such as generics

See all articles