首先是目录结构

源文件夹为 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. CPU Usage Sampling Profiles (cpu=samples)

    HPROF工具通过对采样线程搜集CPU使用信息.以下是从运行的javac中样例输出的一部分. $ javac -J-agentlib:hprof=cpu=samples Hello.java CPU ...

  2. 修改json文件

    第三方库jq https://stedolan.github.io/jq/manual/ cat old_deploy.json \ | jq --arg cpu_limit $cpu_limit ' ...

  3. python学习笔记_week12_mysql

    一.数据库介绍 (一)什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库, 每个数据库都有一个或多个不同的API(接口)用于创建,访问,管理,搜索和复制所保存的数据. ...

  4. scrapy中deferred的回调

    def _next_request_from_scheduler(self, spider):#在引擎中处理一个请求 slot = self.slot request = slot.scheduler ...

  5. asp.net webform/mvc导出Excel通用代码

    最近将自己在项目中经常用到的excel导出方法分析如下,如有不妥之处望他人指出,如果有更好的方法希望展示出来互相学习. //导出事件 protected void btnexcel_Click(obj ...

  6. JSR303 分組数据验证的使用

    场景:一个Bean ,需要在不同情况下分别做验证 1.依赖:springboot 已经集成 2.定义一个bean (验证对象) import javax.validation.constraints. ...

  7. MD5类(MD5Helper)

    项目中经常需要使用到MD5来进行加密 代码: namespace MyProject.Common { public class MD5Helper { /// <summary> /// ...

  8. 查看进程中的socket状态和数量

    程序运行时查看,结果是这样子的 C:\Users\Administrator>netstat -ano|findstr TCP TIME_WAIT TCP TIME_WAIT TCP TIME_ ...

  9. 【转】Jenkins 二次开发 - Python

    马克,备用: Jenkins 二次开发 https://testerhome.com/topics/14988?locale=zh-TW python-jenkins api 文档:https://p ...

  10. c++Builder debug DataSet Visualizer

    c++Builder debug DataSet Visualizer delphi 正常,c++builder报错. fdMemTable->SaveToFile("d:\\DSdb ...