PHPUnit 组织测试
首先是目录结构

源文件夹为 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 组织测试的更多相关文章
- 【夯实PHP基础】PHPUnit -- PHP测试框架
本文地址 分享提纲: 1.概述 2.安装 3.编写第一个测试用例 4.PHPUnit高级 5.参考 1.概述 1)[测试框架] 它是一款轻量级的PHP测试框架,是一个xUnit的体系结构的单元测试框架 ...
- 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 ...
- phpunit——执行测试文件和测试文件中的某一个函数
phpunit --filter testDeleteFeed // 执行某一个测试函数 phpunit tests/Unit/Services/Feed/FeedLogTest.php // 执行某 ...
- 第二节 PHPUnit测试的剖析
现在,让我们仔细看看测试结构的样子. 让我们从一个简单的测试用例开始,它将显示基本的PHPUnit测试结构. 以下代码片段是测试用于排序数组的两个PHP函数的一个非常基本的示例:asort()用于对数 ...
- 使用 PHPUnit 和 Selenium 进行测试
适用于 PHP 的 NetBeans IDE 支持 PHPUnit 自动测试.通过 PHPUnit,NetBeans IDE 可为 PHP 提供代码覆盖率,这与 IDE 为 Python 提供的代码覆 ...
- PHPUnit 手册
PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 ...
- 19-06 【phpunit和docker】
phpunit简介 在用PHP做项目的时候,有时候我们需要写一些测试代码,其中可能包含单元测试(比如字符串处理,ip解析,mobile解析等). 我们常用的工具是phpunit,它很方便地帮我们组织测 ...
- PHPUnit 手册(转)
PHPUnit 手册 PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, ...
- PhpStorm 配置 PHPUnit
配置说明 全局安装phpunit代码 composer global require phpunit/phpunit 该代码会自动保存在 /User/你的用户名/.composer/vendor/ph ...
随机推荐
- 43.scrapy爬取链家网站二手房信息-1
首先分析:目的:采集链家网站二手房数据1.先分析一下二手房主界面信息,显示情况如下: url = https://gz.lianjia.com/ershoufang/pg1/显示总数据量为27589套 ...
- 17. 判断是否IE浏览器
var isNotIE = !window.VBArray; //判断是否IE浏览器 if(isNotIE) { //如果不是IE浏览器 $("#da ...
- 廖雪峰老师Python3教程练习整理
1.定义一个函数quadratic(a, b, c),接收3个参数,返回一元二次方程:ax2 + bx + c = 0的两个解 # -*- coding: utf-8 -*-import mathde ...
- k8s资料转载
K8S入门(二) kubeadmin单机部署 (kubernetes)k8s入门.yum单机版安装.kuberctl指令.k8s服务实例. kubernetes---CentOS7安装kubernet ...
- 利用STM32CubeMX来生成USB_HID_host工程
修改时钟(备注这边使用25mhz的主晶振) 选择debug_level等级为3
- oracle 内连接(inner join)、外连接(outer join)、全连接(full join)
转自:https://premier9527.iteye.com/blog/1659689 建表语句: create table EMPLOYEE(EID NUMBER,DEPTID NUMBER,E ...
- day26-保护属性
如果有一个对象,当需要对其进行修改属性时,有2种方法 1.对象名.属性名 = 数据 --->直接修改 2.对象名.方法名() --->间接修改 为了更好的保护属性安全,即不能随意修改,一般 ...
- 爬虫--Scrapy-CrawlSpider&基于CrawlSpide的分布式爬虫
CrawlSpider 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调par ...
- 爬虫--Scrapy-持久化存储操作2
1.管道的高级操作 将爬取到的数据值分别存储到本地磁盘.redis数据库.mysql数据. 需求:将爬取到的数据值分别存储到本地磁盘.redis数据库.mysql数据. 1.需要在管道文件中编写对应平 ...
- 伪AJAX
<h3>3,伪ajax</h3> <h6>学习iframe(嵌套别人家网站的)</h6> <div> <input id=" ...