phpunit
教程及文档:
https://www.jianshu.com/p/abcca5aa3ad6
http://www.phpunit.cn/manual/current/zh_cn/phpunit-book.html#installation.requirements
https://phpunit.readthedocs.io/zh_CN/latest/
https://www.kancloud.cn/manual/phpunit-book/68615
http://phpunit.cn/manual/7.0/zh_cn/index.html
注意安装分为档案包phpunit.phar和composer安装
phpunit.phar :将所有的类都压缩到这个档案包里了 composer: 没有压缩默认会下载到项目的vendor目录里
写好测试类后需要在窗口命令行执行才行!
phpunit ArrayTest 例子中,PHPUnit 命令行测试执行器将在当前工作目录中寻找 ArrayTest.php 源文件并加载之。而在此源文件中应当能找到 ArrayTest 测试用例类,此类中的测试将被执行
phpunit.xml
在使用phpunit时将命令路径移动到测试文件目录,使用phpunit命令要不然会出现读取不了PHPUnit.xml
bootstrap="./autoload.php" 在测试之前加载的的PHP 文件,一般可以做一个初始化工作
name: 套件名称
directory : 套件测试的目录,目录下一般放测试文件的用例
----suffix : 测试文件后缀,如果不填写,则默认后缀为*Test.php,即phpunit 默认会执行*Test.php 的文件
----action: 测试目录名
file: 可以单独设置测试文件
exclude: 排除不需要测试的文件
可以用 phpVersion 和 phpVersionOperator 属性来指定 PHP 版本需求。在以下例子中,仅当 PHP 版本至少为 5.3.0 时才会将 /path/to/*Test.php 文件与 /path/to/MyTest.php 文件添加到测试套件中
<file phpVersion="5.3.0" phpVersionOperator=">=">/path/to/MyTest.php</file>
<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';
目录结构
├── reports
├── phpunit.xml
├── src
│ ├── autoload.php
│ ├── Money.php
└── tests
└── MoneyTest.php
1、定义
D:\xampp\htdocs\test\PHPunit>phpunit --bootstrap ./src/Money.php ./tests/MoneyTest.php
手动指定autoload.php D:\xampp\htdocs\test\PHPunit>phpunit --bootstrap ./src/autoload.php ./tests/MoneyTest
定义xml文件后 D:\xampp\htdocs\test\PHPunit>phpunit ./tests/MoneyTest
<?xml version="1.0" encoding="UTF-8"?>
<!-- 它将在递归遍历添加在tests的所有 *Test.php 文件中找到的 *Test 类 -->
<phpunit bootstrap="./src/autoload.php" verbose="true">
<testsuites>
<testsuite name="money test">
<directory suffix="Test.php">tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
</phpunit>
日志
利用PHP CodeCoverage來计算程序代码覆盖率(code coverage),需要安裝 Xdebug
https://pecl.php.net/package/xdebug 下载 然后将dll扩展放到php/ext ,php.ini extendsion=xxxx...
先在项目下建立一个reports/目录,存放code coverage分析的结果。
然后执行
phpunit --coverage-html reports/ tests/
D:\xampp\htdocs\test\PHPunit>phpunit --coverage-html ./reports/ ./tests/
或者执行
phpunit --bootstrap vendor/autoload.php --coverage-html reports/ tests/
当然,也可以使用XML来设定。
<logging>
<log type="coverage-html" target="./reports" charset="UTF-8"/>
</logging>
接着执行测试:
phpunit tests/EventTest.php
断言:
assertArrayHasKey()
assertClassHasAttribute()
assertArraySubset()
assertClassHasStaticAttribute()
assertContains()
assertContainsOnly()
assertContainsOnlyInstancesOf()
assertCount()
assertEmpty()
assertEqualXMLStructure()
assertEquals()
assertFalse()
assertFileEquals()
assertFileExists()
assertGreaterThan()
assertGreaterThanOrEqual()
assertInfinite()
assertInstanceOf()
assertInternalType()
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonFile()
assertJsonStringEqualsJsonString()
assertLessThan()
assertLessThanOrEqual()
assertNan()
assertNull()
assertObjectHasAttribute()
assertRegExp()
assertStringMatchesFormat()
assertStringMatchesFormatFile()
assertSame()
assertStringEndsWith()
assertStringEqualsFile()
assertStringStartsWith()
assertThat()
assertTrue()
assertXmlFileEqualsXmlFile()
assertXmlStringEqualsXmlFile()
assertXmlStringEqualsXmlString()
标注:
@author
@after
@afterClass
@backupGlobals
@backupStaticAttributes
@before
@beforeClass
@codeCoverageIgnore*
@covers
@coversDefaultClass
@coversNothing
@dataProvider
@depends 测试类中一个方法依赖另一个方法的标注
@expectedException
@expectedExceptionCode
@expectedExceptionMessage
@expectedExceptionMessageRegExp
@group
@large
@medium
@preserveGlobalState
@requires
@runTestsInSeparateProcesses
@runInSeparateProcess
@small
@test
@testdox
@ticket
@uses
phpunit的更多相关文章
- 安装并使用PHPunit
安装并使用PHPunit Linux 下安装PHPunit PHP 档案包 (PHAR) 要获取 PHPUnit,最简单的方法是下载 PHPUnit 的 PHP 档案包 (PHAR),它将 PHPU ...
- PHPUnit笔记
PHPUnit是一个面向PHP程序员的测试框架,这是一个xUnit的体系结构的单元测试框架. 复杂的项目,通过单元测试能够快速排查bug,有效减少bug的产生.简单的项目,使用php自带的var_du ...
- PHPUnit整合ThinkPHP的库TPUnit
项目地址:https://github.com/web3d/TPUnit ThinkPHP PHPUnit框架集成,基于TP3.2,建议PHP 5.4以上环境. 单元测试应该是提高PHP编码质量的解决 ...
- phpunit 测试框架安装
PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...
- 初试PHP单元测试TDD之安装PHPUnit
东风吹战鼓擂,一年一度的校招季开始了,最为一名即将踏入社会的搬砖工,自然也闲不下来了.各种总结.恶补.面经在所难免.当遇见敏捷开发时,有点蒙了,这是什么东东,绝对不能吃!既然是一种软件开发的方式,听上 ...
- 安装最新版本的PHPUnit后,不能使用
我使用的是widows系统.本来3.7.8版本的Phpunit用的是非常顺畅的,最近重新安装phpunit,安装了最小版本,然后在使用的时候就会报很多各种错误.无奈之下只能降版本到3.7.8 首先要卸 ...
- 基于Netbeans的PHPUnit单元测试环境搭建
一.配置 PHPUnit截至2015-10-16,稳定版已更新至5.0.6,要求使用PHP v5.6及以上的环境才能使用. PHPUnit的4.8系列要求在PHP v5.3.3以上环境使用. Netb ...
- PHP PHPUnit的简单使用
1.window安装pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 2.在工作目录下,放两个文件: 1)C ...
- phpunit学习 3:
16:17 2015/12/11phpunit学习 3:单元测试的大概步骤是:编写待测试类,编写测试用例类,编写测试类,测试.1.如果你有多个类,多个测试类的test类,那么可以编写一个AllTest ...
- phpunit测试学习 2 分类总结断言涉及哪些方面
11:27 2015/12/9phpunit测试学习 2, 分类总结断言涉及哪些方面先推荐windows快速打开某处路径下的cmd,进入测试状态:可以在文件夹中,按住Shift+鼠标右键,这时候你就 ...
随机推荐
- mysql 原理 ~ 常规锁
一 模式 RR模式二 mysql锁相关场景 1 有间隙的地方就可能有间隙锁,并非只有辅助索引的场景下才会存在gap lock,典型场景 id主键的范围查询 2 varchar的范围锁定原理和int ...
- 实例详析ImageView的adjustViewBonds和scaleType
android:adjustViewBounds是否保持宽高比.需要与maxWidth.MaxHeight一起使用,否则单独使用没有效果. 设置View的最大高度,单独使用无效,需要与setAdjus ...
- JavaScript客户端签名直传OSS
参考链接: https://help.aliyun.com/document_detail/31925.html?spm=5176.11065259.1996646101.searchclickres ...
- 阿里云apache服务器外网无法访问(配置安全组,添加80服务)
CentOS的系统 ,已经安装好了 apache php mysql 常规排错过程(ps:没耐心的童鞋请直接看最后一步,学习在阿里云控制台配置 安全组,允许 http服务) 第一步:检查apache ...
- MRPT 安装使用
1. 安装mrpt ( apt-get ) $ sudo apt-get install mrpt-apps libmrpt-dev 2. 下载mrpt-1.3 链接:https://github.c ...
- C语言练手游戏-控制台输出一个会移动的坦克
把C语言的知识融合起来做一个练手的小游戏项目,将自己掌握到的数据结构.数组.函数.宏定义等知识综合利用,增加对语法的熟练程度. 操作系统: windows 10 x64 编译IDE : VS2015 ...
- ARMV8 datasheet学习笔记3:AArch64应用级体系结构之Memory Type and Attributes
1.前言 2. Memory类型和属性 memory分为normal memory和device memory,两种类型的Memory有各自的属性,除了下面介绍的几种属性外,还有其他一些杂项属性 2. ...
- eclipse指定项目编译级别
指定项目编译级别Eclipse→Preferences→Java→Compiler→Compiler compliance level:1.6或其他 或者,
- C/C++杂记:虚函数的实现的基本原理
1. 概述 简单地说,每一个含有虚函数(无论是其本身的,还是继承而来的)的类都至少有一个与之对应的虚函数表,其中存放着该类所有的虚函数对应的函数指针.例: 其中: B的虚函数表中存放着B::foo和B ...
- java多线程与线程间通信
转自(http://blog.csdn.net/jerrying0203/article/details/45563947) 本文学习并总结java多线程与线程间通信的原理和方法,内容涉及java线程 ...