首先是目录结构

源文件夹为 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. php用户名密码

    http://112.124.47.59:8090/activity/index/free?mobile=15652701923&tcode=f9380859085200714&s=7 ...

  2. C# 反射常见用法

    定义: 反射是.NET中的重要机制,通过反射,可以在运行时获得程序或程序集中每一个类型(包括类.结构.委托.接口和枚举等)的成员和成员的信息.有了反射,即可对每一个类型了如指掌.另外我还可以直接创建对 ...

  3. mybatis四(动态sql)

    <1><select id="selectUserByConditions" parameterType="user" resultType= ...

  4. Spring-data-jpa 常用的时间注解

    @Entity //不写@Table默认为user @Table(name="t_user",schema="DB_name") //自定义表名 public ...

  5. div的全屏与退出全屏

    div的全屏与退出全屏 作用:将div全屏与退出全屏,一般播放器使用较多. html按钮: <button onclick="showFull();"> 全屏 < ...

  6. VMware Harbor学习

    同时安装Clair和Notary# ./install.sh --with-notary --with-clair 与notary或者Clair一起安装时管理Harbor的生命周期当Harbour与N ...

  7. Visual SVN Server备份脚本

    set tt=%date:~0,4%%date:~5,2%%date:~8,2% mkdir D:\SVN_BACKUP_%tt%\Repositories xcopy C:\Repositories ...

  8. Zabbix笔记

    简单检查中的icmppingloss[<target>,<packets>,<interval>,<size>,<timeout>]   结 ...

  9. uml用例关系

    关联关系 关联关系是指执行者与用例之间的关系,又称为通信关系,如果某个执行者可以对某个用例进行操作,它们之间就具有关联关系,如下图所示,“经理”有一个功能为“查看库存报表”,因此可以在执行者“经理”和 ...

  10. Haskell语言学习笔记(90)Default

    安装 data-default-class $ cabal install data-default-class Installed data-default-class-0.1.2.0 Prelud ...