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+鼠标右键,这时候你就 ...
随机推荐
- python技巧 switch case语句
不同于C语言和SHELL,python中没有switch case语句,关于为什么没有,官方的解释是这样的 使用Python模拟实现的方法: def switch_if(fun, x, y): ...
- 第16月第9天 opengl glCltDispatchTable
1.glCltDispatchTable typedef struct _GLTEBINFO { // glCltDispatchTable must be the first field for t ...
- 【文件】java生成PDF文件
package test; import java.awt.Color; import java.io.FileOutputStream; import org.junit.Test; import ...
- sizeof strlen区别于联系
http://www.cnblogs.com/carekee/articles/1630789.html
- android AysncTask使用
1.继承AysncTask类 例子: class downloadTask extends AsyncTask<Void,Integer,Boolean> 第一个参数是传入的参数 第二个参 ...
- 012_如何清除DNS缓存
运维过程中经常会进行切换域名解析等的操作,就需要查看是否更新.但常常DNS设置已经更新了,但是用户那边的DNS还是没有更新. 以下分析几点原因及我的解决方案. 一. <1>本地你的dns缓 ...
- zabbix系列(四)Zabbix3.0.4添加对Nginx服务的监控
Zabbix3.0.4添加对Nginx服务的监控 通过Nginx的http_stub_status_module模块提供的状态信息来监控,所以在Agent端需要配置Nginx状态获取的脚本,和添加ke ...
- sqlserver数据库系统性能监控步骤
1.部署好环境JDK+tomcat+数据库 ①修改数据库连接账号密码db.properties ②修改applicationContext.xml文件,开启任务 <bean id="o ...
- 解决报错error the @annotation pointcut expression is only supported at Java 5
eclipse搭建环境后报错 error the @annotation pointcut expression is only supported at Java 5 错误意思大致是:注释切入点表达 ...
- 转载:2.2.2 配置项的语法格式《深入理解Nginx》(陶辉)
原文:https://book.2cto.com/201304/19627.html 从上文的示例可以看出,最基本的配置项语法格式如下: 配置项名 配置项值1 配置项值2 - ; 下面解释一下配置项的 ...