教程及文档:

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的更多相关文章

  1. 安装并使用PHPunit

    安装并使用PHPunit Linux 下安装PHPunit PHP 档案包 (PHAR)  要获取 PHPUnit,最简单的方法是下载 PHPUnit 的 PHP 档案包 (PHAR),它将 PHPU ...

  2. PHPUnit笔记

    PHPUnit是一个面向PHP程序员的测试框架,这是一个xUnit的体系结构的单元测试框架. 复杂的项目,通过单元测试能够快速排查bug,有效减少bug的产生.简单的项目,使用php自带的var_du ...

  3. PHPUnit整合ThinkPHP的库TPUnit

    项目地址:https://github.com/web3d/TPUnit ThinkPHP PHPUnit框架集成,基于TP3.2,建议PHP 5.4以上环境. 单元测试应该是提高PHP编码质量的解决 ...

  4. phpunit 测试框架安装

    PHPUnit是一个轻量级的PHP测试框架.它是在PHP5下面对JUnit3系列版本的完整移植,是xUnit测试框架家族的一员(它们都基于模式先锋Kent Beck的设计).来自百度百科 一.下载wg ...

  5. 初试PHP单元测试TDD之安装PHPUnit

    东风吹战鼓擂,一年一度的校招季开始了,最为一名即将踏入社会的搬砖工,自然也闲不下来了.各种总结.恶补.面经在所难免.当遇见敏捷开发时,有点蒙了,这是什么东东,绝对不能吃!既然是一种软件开发的方式,听上 ...

  6. 安装最新版本的PHPUnit后,不能使用

    我使用的是widows系统.本来3.7.8版本的Phpunit用的是非常顺畅的,最近重新安装phpunit,安装了最小版本,然后在使用的时候就会报很多各种错误.无奈之下只能降版本到3.7.8 首先要卸 ...

  7. 基于Netbeans的PHPUnit单元测试环境搭建

    一.配置 PHPUnit截至2015-10-16,稳定版已更新至5.0.6,要求使用PHP v5.6及以上的环境才能使用. PHPUnit的4.8系列要求在PHP v5.3.3以上环境使用. Netb ...

  8. PHP PHPUnit的简单使用

    1.window安装pear的教程:http://jingyan.baidu.com/article/ca41422fd8cf3d1eae99ed3e.html 2.在工作目录下,放两个文件: 1)C ...

  9. phpunit学习 3:

    16:17 2015/12/11phpunit学习 3:单元测试的大概步骤是:编写待测试类,编写测试用例类,编写测试类,测试.1.如果你有多个类,多个测试类的test类,那么可以编写一个AllTest ...

  10. phpunit测试学习 2 分类总结断言涉及哪些方面

    11:27 2015/12/9phpunit测试学习 2,  分类总结断言涉及哪些方面先推荐windows快速打开某处路径下的cmd,进入测试状态:可以在文件夹中,按住Shift+鼠标右键,这时候你就 ...

随机推荐

  1. pyqt5 鼠标操作

    #资料 http://blog.sina.com.cn/s/blog_6483fa330102xo6w.html import sysfrom PyQt5.QtWidgets import QAppl ...

  2. 【IT界的厨子】酱香鲈鱼

    食材: 前世曾经回眸的鲈鱼一条(主要选刺少的鱼,适合孩子吃,大人吃随意,草鱼比较大) 五花肉少许(肥一些的) 豆腐 辅料: 葱姜 蒜(选) 大料 香菜 调味: 啤酒(两罐) 黄豆酱或豆瓣酱(选) 老抽 ...

  3. c#中富文本编辑器Simditor带图片上传的全部过程(项目不是mvc架构)

    描述:最近c#项目中使用富文本编辑器Simditor,记录一下以便以后查看. 注:此项目不是MVC架构的. 1.引用文件 项目中引用相应的css和js文件,注意顺序不能打乱,否则富文本编辑器不会正常显 ...

  4. mysql案例-sysbench安装测试

    一 地址 githup地址https://github.com/akopytov/sysbench二 版本 sysbench 1.0.15 curl -s https://packagecloud.i ...

  5. Mybatis进阶学习笔记——动态sql

    1.if标签 <select id="queryByNameAndTelephone" parameterType="Customer" resultTy ...

  6. Android Retrofit 2.0使用

    实例带你了解Retrofit 2.0的使用,分享目前开发Retrofit遇到的坑和心得. 添加依赖 app/build.gradle 1 compile 'com.squareup.retrofit2 ...

  7. ubuntu14.04 VIM for python 一键配置

    # 超强vim配置文件 [![Build Status](https://travis-ci.org/ma6174/vim.png?branch=master)](https://travis-ci. ...

  8. [转]数据对齐对CPU的影响

    [转]http://www.cnblogs.com/wuzhenbo/archive/2012/06/05/2537465.html 1.前言 在IBM开发社区上发现一篇叫'Data alignmen ...

  9. weblogic实时监控开发

    参考api文档 https://docs.oracle.com/cd/E13222_01/wls/docs90/wlsmbeanref/core/index.html https://docs.ora ...

  10. Oracle 数据库、实例、用户、表空间、表之间的关系

    数据库: Oracle数据库是数据的物理存储.这就包括(数据文件ORA或者DBF.控制文件.联机日志.参数文件).其实oracle数据库的概念和其它数据库不一样,这里的数据库是一个操作系统只有一个库. ...