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 ...
随机推荐
- 分布式计算课程补充笔记 part 1
▶ 高性能计算机发展历程 真空管电子计算机,向量机(Vector Machine),并行向量处理机(Parallel Vector Processors,PVP),分布式并行机(Parallel Pr ...
- secureCRT 设置字体时,显示字体较少问题
控制面板->字体->选择字体,右击"显示".就可以再crt中看到了.
- twisted如何生成deferred的
@implementer(interfaces.IStreamClientEndpoint)class TCP4ClientEndpoint(object): """ ...
- kubectl version报did you specify the right host or port
现象: [root@localhost shell]# kubectl version Client Version: version.Info{Major:", GitVersion:&q ...
- react-native获取设备信息app版本信息,react-native-device-info
安装 yarn add react-native-device-info react-native link react-native-device-info link 之后就可以直接使用了,ios ...
- jsfl 选择图层 选择帧 转化成mc
//打开fla var _openDOC = fl.openDocument("file:///E|TE/爱.fla"); //获取图层4的总帧 var _Length=fl.ge ...
- APP-7-百度地图移动轨迹
1.代码部分 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...
- vue .map 文件
参数: productionSourceMap:false 这个改为false.去掉打包产生的map文件 map文件的作用:定位线上错误代码位置;
- 尚硅谷springboot学习9-配置文件值注入
首先让我想到的是spring的依赖注入,这里我们可以将yaml或者properties配置文件中的值注入到java bean中 配置文件 person: lastName: hello age: 18 ...
- oracle第三天笔记
DDL语句管理表 /* Oracle体系结构: 数据库 ---> 数据库实例ORCL ---> 表空间 (用户里面的创建表) ---> 数据文件 地球 ---> 中国 ---& ...