PhpStorm 配置 PHPUnit

配置说明

全局安装phpunit代码

composer global require phpunit/phpunit

该代码会自动保存在 /User/你的用户名/.composer/vendor/phpunit

全局安装phpunit命令脚本

从上一步安装结果可以得知当前环境PHP版本可兼容的phpunit的版本,我这里的PHP是5.6的,最大可兼容phpunit5.7

wget https://phar.phpunit.de/phpunit-5.7.phar
chmod +x phpunit-5.7.phar
sudo mv phpunit-5.7.phar /usr/local/bin/phpunit
phpunit --version

创建 phpunit.xml

放在你的项目根目录,这个文件是 phpunit 会默认读取的一个配置文件

<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="service">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

配置 PhpStorm 的 PHP CLi

Languages & Frameworks > PHP

点击 + 新增一个 PHP 解释器

  • 配置 php 执行程序
  • 点击那个 同步的小图标,如果看到 successfully 就说明配置有效

?

配置 phpunit.phar 路径和 phpunit.xml 路径

Languages & Frameworks > PHP > Test Frameworks
点击 + 新增一个 PHPUnit Local

例如我的phpunit本地的路径为/usr/local/bin/phpunit

?

配置单元测试类提示

Languages & Frameworks > PHP

如我的phpunit包本地的路径为/Users/maritni/.composer/vendor/phpunit

?

单元测试编写

  1. Class为Demo的测试类为DemoTest
  2. 测试类继承于 PHPUnit\Framework\TestCase
  3. 测试方法
    • 必须为public权限,
    • 一般以test开头,也可以给其加注释@test来标识
    • 在测试方法内,类似于 assertEquals() 这样的断言方法用来对实际值与预期值的匹配做出断言。
<?php
/**
 * Description:数组压入和弹出测试用例
 * Created by Martini
 * DateTime: 2019-06-29 16:09
 */

use PHPUnit\Framework\TestCase;

class DemoTest extends TestCase
{
    public function testPushAndPop()
    {
        $stack = [];
        $this->assertEquals(0, count($stack));

        array_push($stack, ‘foo‘);
        $this->assertEquals(‘foo‘, $stack[count($stack)-1]);
        $this->assertEquals(1, count($stack));

        $this->assertEquals(‘foo‘, array_pop($stack));
        $this->assertEquals(0, count($stack));
    }
}

执行单元测试

执行单个文件单元测试

方式1: Phpstorm方式,当前测试类右键Run即可

?

方式2:命令行方式,进入项目目录执行

phpunit Creational/SimpleFactory/Tests/DemoTest.php

?

执行全局单元测试

全局单元测试,实际上phpunit会根据xml配置文件进行测试。

phpstorm方式

?

命令行方式

命令行下进入当前项目执行

phpunit

XML 文件用法

PHPUnit 的目标之一是测试应当可组合:我们希望能将任意数量的测试以任意组合方式运行,例如,整个项目的所有测试,或者项目中的某个组件内的所有类的测试,又或者仅仅某单个类的测试。

PHPUnit 支持好几种不同的方式来组织测试以及将它们编排组合成测试套件。

PHPUnit的 XML 配置文件可以用于编排测试套件。Example1, “用 XML 配置来编排测试套件”展示了一个最小化的 phpunit.xml 例子,它将在递归遍历 tests 时添加所有在 *Test.php 文件中找到的 *Test 类。

Example1.最小化xml文件

<phpunit bootstrap="vendor/autoload.php">
    <testsuites>
        <testsuite name="money">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
</phpunit>

如果 phpunit.xml 或 phpunit.xml.dist (按此顺序)存在于当前工作目录并且未使用 --configuration,将自动从此文件中读取配置。

可以明确指定测试的执行顺序:

Example 2. 用 XML 配置来编排测试套件

<phpunit bootstrap="vendor/autoload.php">
  <testsuites>
    <testsuite name="money">
      <file>tests/IntlFormatterTest.php</file>
      <file>tests/MoneyTest.php</file>
      <file>tests/CurrencyTest.php</file>
    </testsuite>
  </testsuites>
</phpunit>

xml实例1


<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
    <testsuites>
        <testsuite name="Design Patterns">
            <directory suffix="Test.php">Behavioral/*/Tests</directory>
            <directory suffix="Test.php">Creational/*/Tests</directory>
            <directory suffix="Test.php">More/*/Tests</directory>
            <directory suffix="Test.php">Structural/*/Tests</directory>
        </testsuite>
    </testsuites>
    <filter>
        <blacklist>
            <directory>./vendor</directory>
        </blacklist>
    </filter>
</phpunit>

xml实例2

<phpunit bootstrap="./booten.php">

    <testsuite name="actionsuitetest">
      <directory suffix=".php">action</directory>

      <file>HuiyuanZhanghuOrder.php</file>

     <exclude>/action/HuiyuanJifenTest.php</exclude>
    </testsuite>

    <testsuite name="modelsuitetest">
      <directory suffix=".php">model</directory>
    </testsuite>

    <testsuite name="htmlsuitetest">
      <directory suffix=".php">html</directory>
    </testsuite>

    <!-- 代码覆盖率 -->
    <!-- 覆盖率的测试文件,blacklist 黑名单(不需要统计覆盖率的文件),whitelist 白名单(统计覆盖率的测试文件) 当黑名单与白名单文件重复时,白名单起作用 

    -->
    <filter>
<blacklist>
    <directory suffix=".php">action</directory>
    <file>ArrayTest.php</file>
  </blacklist>

  <whitelist addUncoveredFilesFromWhitelist="true">
   <directory suffix=".php">action</directory>
   <directory suffix=".php">model</directory>
   <directory suffix=".php">html</directory>
   <file>ArrayTest.php</file>
   <exclude>
   <directory suffix=".php">action/lib</directory>
   <directory suffix=".php">model</directory>
   <file>action/lib/Loginxxx.php</file>
   </exclude>
  </whitelist>
</filter>

    <!--代码覆盖率报告,可以生成很多类型报告,有html(coverage-html),xml(coverage-clover),txt ,json 等等
    <log type="coverage-php" target="/tmp/coverage.serialized"/>
  <log type="coverage-text" target="php://stdout" showUncoveredFiles="false"/>
  <log type="json" target="/tmp/logfile.json"/>
  <log type="tap" target="/tmp/logfile.tap"/>
  <log type="junit" target="/tmp/logfile.xml" logIncompleteSkipped="false"/>
  <log type="testdox-html" target="/tmp/testdox.html"/>
  <log type="testdox-text" target="/tmp/testdox.txt"/>

    -->

        <logging>
         <!-- target(report/html) 生成html 文件的目录-->
  <log type="coverage-html" target="report/html" charset="UTF-8" yui="true" highlight="false" lowUpperBound="35" highLowerBound="70"/>
  <!-- target(report/coverage/coverage.xml) 生成xml的文件名-->
  <log type="coverage-clover" target="report/coverage/coverage.xml"/>
</logging>
    <!-- 代码覆盖率 -->

    <php>
 <includePath>.</includePath>
 <ini name="foo" value="bar"/>
 <const name="foo" value="bar"/>
 <var name="foo" value="bar"/>
 <env name="foo" value="bar"/>
 <post name="foo" value="bar"/>
 <get name="foo" value="bar"/>
 <cookie name="foo" value="bar"/>
 <server name="foo" value="bar"/>
 <files name="foo" value="bar"/>
 <request name="foo" value="bar"/>
</php>  

</phpunit>

xml 解释

xml 解释

bootstrap="./vendor/autoload.php"

在测试之前加载的的PHP 文件,一般可以做一个初始化工作

<testsuite name="actionsuitetest">
      <directory suffix=".php">action</directory>
      <file>Order.php</file>
</testsuite>

测试套件,如果想测试页面,action,model 可以多加几个测试套件

name: 套件名称

directory :套件测试的目录,目录下一般放测试文件的用例

       suffix :测试文件后缀,如果不填写,则默认后缀为*Test.php,即phpunit 默认会执行*Test.php  的文件

       action:测试目录名

file:可以单独设置测试文件

exclude:排除不需要测试的文件

<filter> 元素及其子元素用于配置代码覆盖率报告所使用的白名单。
blacklist 黑名单(不需要统计覆盖率的文件),whitelist 白名单(统计覆盖率的测试文件) 当黑名单与白名单文件重复时,白名单起作用

<logging> 元素及其 <log> 子元素用于配置测试执行期间的日志记录。

 <php>
  <includePath>.</includePath>
  <ini name="foo" value="bar"/>
  <const name="foo" value="bar"/>
  <var name="foo" value="bar"/>
  <env name="foo" value="bar"/>
  <post name="foo" value="bar"/>
  <get name="foo" value="bar"/>
  <cookie name="foo" value="bar"/>
  <server name="foo" value="bar"/>
  <files name="foo" value="bar"/>
  <request name="foo" value="bar"/>
</php>  

这段xml 可以对应以下PHP 代码

includePath

ini_set(‘foo‘, ‘bar‘);
define(‘foo‘, ‘bar‘);
$GLOBALS[‘foo‘] = ‘bar‘;
$_ENV[‘foo‘] = ‘bar‘;
$_POST[‘foo‘] = ‘bar‘;
$_GET[‘foo‘] = ‘bar‘;
$_COOKIE[‘foo‘] = ‘bar‘;
$_SERVER[‘foo‘] = ‘bar‘;
$_FILES[‘foo‘] = ‘bar‘;
$_REQUEST[‘foo‘] = ‘bar‘;

Reference

PHPUnit 手册
在phpstorm中安装、配置和运行phpunit详细教程
PHP单元测试使用
phpunit5.0中文手册
phpunit XML 配置文件

原文地址:https://www.cnblogs.com/martini-d/p/phpstorm-pei-zhiphpunit.html

时间: 2024-08-29 11:35:33

PhpStorm 配置 PHPUnit的相关文章

PHPstorm配置PHPunit对composer引入的php代码进行单元测试

1. 如何安装PHPunit,这里不展述(如需打断点debug测试,安装PHP的xdebug扩展方法也不展开说了 https://xdebug.org/) 2.如何进行配置 以 PHP设计模式的代码为例 https://github.com/domnikl/DesignPatternsPHP.git 先克隆下来 git colonel https://github.com/domnikl/DesignPatternsPHP.git 再打开 cd DesignPatternsPHP 安装依赖 co

Atitit phpstorm配置attilax总结

1. 前期准备 1 1.1. 配置interpreter 1 1.2. debug需要xdebug的支持,不管是script模式还是web模式 3 2. 以php script运行 3 2.1.1. 以php web运行 4 3. Php web debug的配置与使用 4 3.1. 多用户远程调试 xdebug.remote_connect_back=1 4 3.2. 参考资料 5 1. 前期准备 1.1. 配置interpreter 首先,在phpstorm中是直接可以运行PHP程序而不需要

phpStorm 配置关联php手册

phpStorm 配置关联php手册 pasting php开发中我尝试过很多个编辑器,但用的最多的是phpStorm ,但一直因为英文太烂,很多phpStorm功能,都没用过.. 最近发现有些编辑器可以 选中函数名,通过相应的快捷键就可以调用 浏览器 打开相应 函数的 在线帮助文档. 一番查找,我终于发现 phpStorm 也有相应的功能: 工具栏  view 选项   -> external documentation 即 选中 函数名 后 按 shift + f1  就可以打开 相应函数的

phpstorm配置react开发环境

phpstorm配置react开发环境 打开 file->setting->Plugins搜索安装 react-templates和jscs插件file->setting->Preferences -> Languages and Frameworks -> JavaScript -> language version下拉框里选JSX Harmony此时新建一个js文件就直接支持jsx语法了

PHPStorm 配置XDebug 调试PHP代码 详细教程

[PHP配置] 为PHP安装xdebug,方法略. 配置文件php.ini [XDebug] zend_extension = "C:\xampp\php\ext\php_xdebug.dll" ;开启自动跟踪 xdebug.auto_trace = On ;开启异常跟踪 xdebug.show_exception_trace = On ;开启远程调试自动启动 xdebug.remote_autostart = On ;开启远程调试 xdebug.remote_enable = 1 ;

方便mac os 10.9系统中phpstorm配置php运行环境

自己安装php,不用mac安装,这样就有php开发环境了. 安装很简单,直接运行一个命令, 需要几分钟,请慢慢等待. curl -s http://php-osx.liip.ch/install.sh | bash -s 5.5 (注5.5是php版本可以是5.6等等) 新安装的php目录是/usr/local/php5/bin,接下来在PhpStorm中设置新安装的php解释器: PhpStorm->Preferences->PHP 会来到解释器设置页 设置我们安装的php解释器:/usr/

PHPstorm 配置xdbug

1.下载xdebug模块 xdebug下载地址:https://xdebug.org/download.PHP 注意:带"ts"是线程安全的意思,"nts"的他没有标示,也就是说,如果是nts的要下载没标示的,下载下来的文件名其实是有标示的. Wampserver里面是有相应模块的:bin\php\php5.6.25\zend_ext. 2.把xdebug安装到php 把下载的dll拷贝到php里面的ext文件夹里(扩展dll都在这里),改名为php_xdebug.

mac下phpstorm配置xdebug工具

参考:PhpStorm中如何使用Xdebug工具,入门级操作方法 http://blog.csdn.net/knight_quan/article/details/51953269 如果你用的集成环境xmapp,mamp,就百度相关,比如: http://www.cnblogs.com/lishiyun19/p/4470086.html http://www.linuxidc.com/Linux/2016-06/132333.htm 而下面视频里讲的是phpstudy2016下如何安装的. 可以

phpstorm配置Xdebug进行调试PHP教程

ps : php版本和xdebug版本一定要相对应 1. PHP安装xdebug扩展 php.ini的配置,下面的配置仅供参考,路径要换成自己的! [xdebug] zend_extension="D:\wamp\php-5.6.2-x64\ext\php_xdebug-2.2.5-5.6-vc11-x86_64.dll" xdebug.remote_enable = On xdebug.remote_handler = dbgp xdebug.remote_host= localho