首先是目录结构

源文件夹为 src/

测试文件夹为 tests/

User.php

<?php
class Errorcode
{
const NAME_IS_NULL = 0;
} class User
{
public $name;
public function __construct($name)
{
$this->name=$name;
} public function Isempty()
{ try{
if(empty($this->name))
{
throw new Exception('its null',Errorcode::NAME_IS_NULL);
}
}catch(Exception $e){
return $e->getMessage();
} return 'welcome '.$this->name;
}
}

对应的单元测试文件  UserTest.php

<?php
use PHPUnit\Framework\TestCase; class UserTest extends TestCase
{
protected $user;
public function setUp()
{
$this->user = new User('');
}
public function testIsempty()
{
$this->user->name='mark';
$result =$this->user->Isempty();
$this->assertEquals('welcome mark',$result); $this->user->name='';
$results =$this->user->Isempty();
$this->assertEquals('its null',$results); } }

第二个单元测试代码因为要引入 要测试的类  这里可以用 自动载入 避免文件多的话 太多include

所以在src/ 文件夹里写 autoload.php

<?php

function __autoload($class){
include $class.'.php';
} spl_autoload_register('__autoload');

当需要User类时,就去include User.php。写完__autoload()函数之后要用spl_autoload_register()注册上。

虽然可以自动载入,但是要执行的命令变得更长了。

打开cmd命令如下

phpunit --bootstrap src/autoload.php tests/UserTest

所以我们还可以在根目录写一个配置文件phpunit.xml来为项目指定bootstrap,这样就不用每次都写在命令里了。

phpunit.xml

<phpunit bootstrap="src/autoload.php">
</phpunit>

然后

打开cmd命令 执行MoneyTest 命令如下

phpunit tests/UserTest

打开cmd命令 执行tests下面所有的文件 命令如下  

phpunit tests

  

PHPUnit 组织测试的更多相关文章

  1. 【夯实PHP基础】PHPUnit -- PHP测试框架

    本文地址 分享提纲: 1.概述 2.安装 3.编写第一个测试用例 4.PHPUnit高级 5.参考 1.概述 1)[测试框架] 它是一款轻量级的PHP测试框架,是一个xUnit的体系结构的单元测试框架 ...

  2. Windows10 + IntelliJ IDEA 2017.3.2 + wamp2e + Yii + PHPunit 搭建测试环境

    一.环境 系统: windows10 WampServer:  wampserver2.2e-php5.3.13-httpd2.2.22-mysql5.5.24-32b.exe IDE:  Intel ...

  3. phpunit——执行测试文件和测试文件中的某一个函数

    phpunit --filter testDeleteFeed // 执行某一个测试函数 phpunit tests/Unit/Services/Feed/FeedLogTest.php // 执行某 ...

  4. 第二节 PHPUnit测试的剖析

    现在,让我们仔细看看测试结构的样子. 让我们从一个简单的测试用例开始,它将显示基本的PHPUnit测试结构. 以下代码片段是测试用于排序数组的两个PHP函数的一个非常基本的示例:asort()用于对数 ...

  5. 使用 PHPUnit 和 Selenium 进行测试

    适用于 PHP 的 NetBeans IDE 支持 PHPUnit 自动测试.通过 PHPUnit,NetBeans IDE 可为 PHP 提供代码覆盖率,这与 IDE 为 Python 提供的代码覆 ...

  6. PHPUnit 手册

    PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 ...

  7. 19-06 【phpunit和docker】

    phpunit简介 在用PHP做项目的时候,有时候我们需要写一些测试代码,其中可能包含单元测试(比如字符串处理,ip解析,mobile解析等). 我们常用的工具是phpunit,它很方便地帮我们组织测 ...

  8. PHPUnit 手册(转)

    PHPUnit 手册 PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, ...

  9. PhpStorm 配置 PHPUnit

    配置说明 全局安装phpunit代码 composer global require phpunit/phpunit 该代码会自动保存在 /User/你的用户名/.composer/vendor/ph ...

随机推荐

  1. win10 mstsc 远程 windows2008R2

  2. Appium环境安装

    Appium 是开源的.跨平台的.多语言支持的 移动应用 自动化工具 原生app,如计算器 混合(Hybrid)app 内嵌web + 原生 移动 web app 手机浏览器打开的网址 安装appiu ...

  3. sqlserver中 事物 索引及视图

      事务 1.什么是事务 事务是一个不可分割的工作逻辑单元,它包含了一组数据库的操作命令,并且所有命令作为一个整体一起向系统提交或撤销操作请求,即要么都执行,要么都不执行 2.事务的4个属性 (1). ...

  4. 16_虚拟dom和dom diff算法

    虚拟dom的作用:是为了减少操作真实的dom 初始化显示界面的过程: 1.创建虚拟dom树——>真实dom树——>绘制页面显示 更新界面的过程: 2.绘制页面显示——>setStat ...

  5. jquery接触初级-----juqery选择器实例

    jquery 选择器用于触发事件,可以少写很多js代码,一般来说,基本的特效功能都能够完成 这里列举一个简单的jquery写的例子: 要求:有两种情况: 1.产品最初状态显示简约形式的品牌,即显示部分 ...

  6. UNITY2018开启deepprofiling

    ADB方式调试游戏步骤 前提: 1,手机开启 [开发者模式][USB调试] 2,数据线连接手机和电脑 3,安装adb(注意adb版本不对可能导致adb deveices找不到设备,那就换个adb版本) ...

  7. ARP工作过程、ARP欺骗的原理和现象、如何防范ARP欺骗

      地址解析协议(Address Resolution Protocol,ARP)是在仅知道主机的IP地址时确定其物理地址的一种协议. 下面假设在一个局域网内,主机A要向主机B发送IP数据报. ARP ...

  8. SimpleDateFormat 使用时出现的线程同步问题。。。

    错误使用: public static final SimpleDateFormat DAY_UI_MONTH_DAY_FORMAT = new SimpleDateFormat("MM-d ...

  9. Nodejs 菜鸟教程学习-创建第一个应用

    注:为了解学习,都是参照http://www.runoob.com/nodejs/nodejs-tutorial.html书写,做下笔记. 对于Nodejs开发来说,在开发一个应用时,我们不仅仅是实现 ...

  10. Python异常和调试.md

    异常捕获 try 基本概念 我们使用try except来捕获异常,python的try except有几个特点: 不管函数内部嵌套几层,只要在try的范围内就可以被捕获.这句话的意思是一个函数被tr ...