首先是目录结构

源文件夹为 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. 46.纯 CSS 创作一个在容器中反弹的小球

    原文地址:https://segmentfault.com/a/1190000015221260 练习地址:https://scrimba.com/c/c3GEWmTb 感想: 原来animation ...

  2. leetcode998

    class Solution: def __init__(self): self.prelist = list() def preTree(self,root:TreeNode): if root ! ...

  3. JavaScript:今天是今年第几周?

    用js实现,今天是今年第几周? 基本思路: 1.当前时间 - 今年1月1日0时,拿到时间差(毫秒数) 2.时间差/7天毫秒数,向上取整 var d = new Date('2018-01-01 00: ...

  4. Win2008R2配置WebDeploy(转)

    一.配置服务器 1.安装管理服务 2.点击管理服务进行配置 3.安装WebDeploy 3.1通过离线安装包方式安装: https://www.iis.net/downloads/microsoft/ ...

  5. Word Ladder 有必要深究。非图的广度优先遍历。标记

    感觉很生疏. https://leetcode.com/problems/word-ladder/

  6. Xe7 System.Json解析数据格式

    一.Demo一 解析嵌套数组 Json数据 {"code":1,"msg":"","data":{"Grade ...

  7. vim主题设定

    Vim的颜色主题在/usr/share/vim/vim74/colors文件夹里. 打开vim后在normal模式下输入“:colorscheme”查看当前的主题,修改主题使用命令“:colorsch ...

  8. How to Pronounce AR, ORN, etc.

    How to Pronounce AR, ORN, etc. Share Tweet Share The R consonant can be really tricky.  In this vide ...

  9. Haskell语言学习笔记(80)req

    req req 是一个好用,类型安全,可扩展,上层的HTTP客户端的库. $ cabal install req Installed req-1.1.0 Prelude> :m +Network ...

  10. Linux 完整卸载oracle和grid软件

    本文主要描述如何在Linux下完全卸载oracle和grid软件. 操作环境 SuSE11 oracle用户完整删除操作步骤 1.登录oracle用户,停止Oracle数据库监听和数据库实例 orac ...