配置说明

全局安装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 配置文件

PhpStorm 配置 PHPUnit的更多相关文章

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

    1. 如何安装PHPunit,这里不展述(如需打断点debug测试,安装PHP的xdebug扩展方法也不展开说了 https://xdebug.org/) 2.如何进行配置 以 PHP设计模式的代码为 ...

  2. Phpstorm配置phpunit对php进行单元测试

    在 phpstorm 中配置 php 项目的单元测试,项目使用 Composer 进行管理,为了避免在项目中直接引入 phpunit 相关代码包,使项目的 vendor 目录变得臃肿,这里采用全局安装 ...

  3. phpcs,phpmd,phan安装部署,phpstorm配置phpunit

    git参考地址:https://github.com/YunhanTech/overview/blob/master/php/learn-road.md phpcs 安装 composer globa ...

  4. phpstorm配置phpunit进行单元测试

    1.配置单元测试目录: (1)autoload.php <?php function autoloader($dir){ spl_autoload_register(function($name ...

  5. PHPStorm 10 配置PHPUnit

    PHPStorm 10 配置PHPUnit PHPUnit的安装 自己用的是Xampp,PHPUnit好像自带不好用. 不说废话: 自己看 According to official site htt ...

  6. phpstorm集成phpunit(转)

    转自http://blog.csdn.net/zhuziying99/article/details/49028321 phpstorm集成phpunit1.下载phpunit.phar,将该文件放到 ...

  7. phpstorm配置svn

    phpstorm配置svn 发表于3年前(2013-02-28 10:50)   阅读(8249) | 评论(0) 4人收藏此文章, 我要收藏 赞1 9月19日成都 OSC 源创会正在报名,送机械键盘 ...

  8. phpStorm 配置关联php手册

    phpStorm 配置关联php手册 pasting php开发中我尝试过很多个编辑器,但用的最多的是phpStorm ,但一直因为英文太烂,很多phpStorm功能,都没用过.. 最近发现有些编辑器 ...

  9. 使用PHPStorm 配置自定义的Apache与PHP环境

    使用PHPStorm 配置自定义的Apache与PHP环境之一   关于phpstorm配置php开发环境,大多数资料都是直接推荐安装wapmserver.而对于如何配置自定义的PHP环境和Apach ...

随机推荐

  1. Shell Step by Step (3) —— Stdin &amp; if

    4.输入输出 #! /bin/bash # Read users input and then get his name read -p "Please input your first n ...

  2. 深入理解ArrayList与LinkedList的区别

    一.先来看看ArrayList与LinkedList 在JDK中所在的位置 从图中可以看出,ArrayList与LinkedList都是List接口的实现类,因此都实现了List的所有未实现的方法,只 ...

  3. [Songqw.Net 基础]WPF插件化中同步Style

    原文:[Songqw.Net 基础]WPF插件化中同步Style 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei1988/a ...

  4. WPF:将Office文档、任意类型文件嵌入到EXE可执行文件中

    原文:WPF:将Office文档.任意类型文件嵌入到EXE可执行文件中 版权声明:本文为博主原创文章,未经博主允许可以随意转载 https://blog.csdn.net/songqingwei198 ...

  5. ASP.NET Core 用户注册 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 用户注册 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 用户注册 上一章节我们终于迁移完了 Identity 的数据,也创建 ...

  6. JDBC学习笔记——简单的连接池

    1.使用LinkedList保存连接                                                               即使是最简单的JDBC操作,也需要包含 ...

  7. 关于VS2015中的code snippet无法使用的问题

    什么是code snippet? Code snippets are small blocks of reusable code that can be inserted in a code file ...

  8. 系统引导文件之 boot.ini(有很多参数)

    Windows NT类的操作系统,也就是Windows NT/2000/XP中,有一个特殊文件,也就是“BOOT.INI”文件,这个文件会很轻松地按照我们的需求设置好多重启动系统. “BOOT.INI ...

  9. 不得不说,我太佩服node了,连openXML也搞定了!

    https://github.com/Ziv-Barber/officegen 开源项目地址 使用报告等有空完成!

  10. SVG路径动画解密

    原文:SVG路径动画解密 原文链接:http://www.gbtags.com/gb/share/5581.htm SVG路径动画效果现在貌似越来越多网站都使用了,给我的感觉就像是一段时间的流行而已, ...