TP5单元测试
tp5版本: 5.0.24
单元测试版本:1.*
1. 安装单元测试扩展:
composer require topthink/think-testing .*
2.安装完毕,运行 php think unit 出现问题
[think\exception\ErrorException]
The each() function is deprecated. This message will be suppressed on further calls
php7.2版本将each()方法废除,项目中使用each()的地方都会出现以上的错误
解决:将each()替换成foreach()
while (list($key, $val) = each($para)) { } 改成 foreach ($para as $key => $val) { }
配置环境变量中的PATH参数:

这时候将E:\phpStudy\PHPTutorial\php\php-5.6.27-nts目录中 php.exe 复制一份 php56.exe,在命令行就可以使用 php -v = php7.2 使用php56 -v = php5.6 的了,当然也可以同理使用 php71 -v = php7.1
3. 问题解决了,现在可以重新执行 php71 think unit 了
执行代码:
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace tests; class ExampleTest extends TestCase
{ // public function testBasicExample()
// {
// $this->visit('/')->see('ThinkPHP');
// } // 测试查询模型返回,查询到数据则返回ok,否则返回失败
// public function testModel()
// {
// $this->assertNotNull(User::getOne('123'));
// }
// 测试方法返回,如果结果和输入的值相同返回ok,否则返回失败
public function testMethod()
{
$this->assertEquals(4,$this->add(1,3));
}
// 方法
public function add($a,$b)
{
return $a+$b;
}
}
执行结果:
E:\code\tp\tp_5_0_24>php71 think unit
PHPUnit 4.8.36 by Sebastian Bergmann and contributors. . Time: 124 ms, Memory: 4.00MB OK (1 test, 1 assertion)
再试试测试,如果不通过的结果:
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yunwuxin <448901948@qq.com>
// +----------------------------------------------------------------------
namespace tests; class ExampleTest extends TestCase
{ // public function testBasicExample()
// {
// $this->visit('/')->see('ThinkPHP');
// } // 测试查询模型返回,查询到数据则返回ok,否则返回失败
// public function testModel()
// {
// $this->assertNotNull(User::getOne('123'));
// }
// 测试方法返回,如果结果和输入的值相同返回ok,否则返回失败
public function testMethod()
{
$this->assertEquals(5,$this->add(1,3));
}
// 方法
public function add($a,$b)
{
return $a+$b;
}
}
E:\code\tp\tp_5_0_24>php71 think unit
PHPUnit 4.8.36 by Sebastian Bergmann and contributors. F Time: 131 ms, Memory: 4.00MB There was 1 failure: 1) tests\ExampleTest::testMethod
Failed asserting that 4 matches expected 5. E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\Constraint\IsEqual.php:137
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\Assert.php:2255
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\Assert.php:513
E:\code\tp\tp_5_0_24\tests\ExampleTest.php:29
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\TestCase.php:908
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\TestCase.php:768
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\TestResult.php:612
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\TestCase.php:724
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\TestSuite.php:722
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\Framework\TestSuite.php:722
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\TextUI\TestRunner.php:440
E:\code\tp\tp_5_0_24\vendor\phpunit\phpunit\src\TextUI\Command.php:149
E:\code\tp\tp_5_0_24\vendor\topthink\think-testing\src\command\Test.php:42
E:\code\tp\tp_5_0_24\thinkphp\library\think\console\Command.php:175
E:\code\tp\tp_5_0_24\thinkphp\library\think\Console.php:674
E:\code\tp\tp_5_0_24\thinkphp\library\think\Console.php:242
E:\code\tp\tp_5_0_24\thinkphp\library\think\Console.php:185
E:\code\tp\tp_5_0_24\thinkphp\library\think\Console.php:145
E:\code\tp\tp_5_0_24\thinkphp\console.php:20 FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
测试函数,更多测试请看帮助文档
assertTrue 为判断是否真
assertFalse 为判断是否假
assertGreaterThan为判断是否大于
assertLessThanOrEqual判断是否小于或等于
assertEquals为判断是否相等
tp5单元测试使用帮助文档 http://www.kancloud.cn/manual/thinkphp5/182511
最后可能存在很多单元测试错误,可以将错误结果重定向到一个日志文件中:
php71 think unit > unit.error.txt
一些问题
问题1:如果tp框架和app不再同一级目录,我们该怎么办呢?
1. 重写一个cmd文件,内容如下:
#!/usr/bin/env php
<?php // 定义项目路径
define('APP_PATH', __DIR__ . '/application/');
// 定义配置文件目录和应用目录同级
define('CONF_PATH', __DIR__ . '/config/');
// 定义第三方项目文件根目录
define('VENDOR_PATH', __DIR__ . '/tp_5_0_24/vendor/');
// 扩展类库目录
define('EXTEND_PATH', __DIR__ . '/extend/');
// 加载框架引导文件
require __DIR__.'/tp_5_0_24/thinkphp/console.php';
2. 将 tp_5_0_24/phpunit.xml 文件复制到当前目录
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false">
<testsuites>
<testsuite name="Application Test Suite">
<directory>./tests/</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">application/</directory>
</whitelist>
</filter>
</phpunit>
3. 新建目录 tests

运行
php71 cmd unit
TP5单元测试的更多相关文章
- PHPUnit简介及使用(thinkphp5的单元测试安装及使用)
PHPUnit简介及使用(thinkphp5的单元测试安装及使用) 一.总结 一句话总结:直接google这个phpunit(how to use phpunit),然后去官网看使用样例和手册,那些英 ...
- TP5学习基础二:目录结构、URL路由、数据操作
一.安装1.使用git或者composer(composer update)进行实时更新,区别在于git不会清空核心框架目录而composer会清空.2.使用官网打包好的TP压缩包(解压即可用)-&g ...
- C#,单元测试
C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...
- C#单元测试(转)
C#,单元测试入门(以下内容可能来自网络) 一.什么叫单元测试(unit testing)? 是指对软件中的最小可测试单元进行检查和验证.对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体 ...
- Intellij idea添加单元测试工具
1.idea 版本是14.0.0 ,默认带有Junit,但是不能自动生成单元测试,需要下载JunitGererator2.0插件 2.Settings -Plugins,下载 JunitGenerat ...
- Python的单元测试(二)
title: Python的单元测试(二) date: 2015-03-04 19:08:20 categories: Python tags: [Python,单元测试] --- 在Python的单 ...
- Python的单元测试(一)
title: Python的单元测试(一) author: 青南 date: 2015-02-27 22:50:47 categories: Python tags: [Python,单元测试] -- ...
- javascript单元测试框架mochajs详解
关于单元测试的想法 对于一些比较重要的项目,每次更新代码之后总是要自己测好久,担心一旦上线出了问题影响的服务太多,此时就希望能有一个比较规范的测试流程.在github上看到牛逼的javascript开 ...
- 使用NUnit为游戏项目编写高质量单元测试的思考
0x00 单元测试Pro & Con 最近尝试在我参与的游戏项目中引入TDD(测试驱动开发)的开发模式,因此单元测试便变得十分必要.这篇博客就来聊一聊这段时间的感悟和想法.由于游戏开发和传统软 ...
随机推荐
- Flink源码阅读(二)——checkpoint源码分析
前言 在Flink原理——容错机制一文中,已对checkpoint的机制有了较为基础的介绍,本文着重从源码方面去分析checkpoint的过程.当然本文只是分析做checkpoint的调度过程,只是尽 ...
- Java实现数据批量导入mysql数据库
本文完全照搬别人的. 原文标题:Java实现数据批量导入数据库(优化速度-2种方法) 原文地址:https://blog.csdn.net/qy20115549/article/details/526 ...
- 剑指offer:二维数组的查找
题目描述 在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数 ...
- #《你们都是魔鬼吗》第八次团队作业:第五天Alpha冲刺
<你们都是魔鬼吗>第八次团队作业:Alpha冲刺 项目 内容 这个作业属于哪个课程 任课教师博客主页链接 这个作业的要求在哪里 作业链接地址 团队名称 你们都是魔鬼吗 作业学习目标 完成最 ...
- ORA-12638: Credential retrieval failed 解决办法
ORA-12638 ORA-12638: Credential retrieval failed 身份证明检索失败 解决办法: 修改sqlnet.ora文件(位置:$ORACLE_HOME ...
- 使用Jackson的@JsonFormat注解时出现少了 8 个小时
比如数据库存的日期是2018-01-05,转成json则变成了2018-01-04 解决办法: @JsonFormat(pattern="yyyy-MM-dd") public D ...
- Tiny C Compiler简介-wiki
Tiny C Compiler(缩写为TCC.tCc或TinyCC)是一个用于x86(16/32位)或x86-64(64位)系统的C编译器,开发者为Fabrice Bellard.软件是设计用于低级计 ...
- NOI.ac模拟赛20181021 ball sequence color
T1 ball 可以发现每次推动球时,是将每个球的位置 −1-1−1 ,然后把最左边的球放到 P−1P-1P−1 处. 记个 −1-1−1 次数,再用set维护就好了. #include <bi ...
- Oracle 分区表中本地索引和全局索引的适用场景
背景 分区表创建好了之后,如果需要最大化分区表的性能就需要结合索引的使用,分区表有两种索引:本地索引和全局索引.既然存在着两种的索引类型,相信存在即合理.既然存在就会有存在的原因,也就是在特定的场景中 ...
- C++反汇编中的循环语句
do while 效率是最高的 #include "pch.h" #include <iostream> int main() { ; ; do { nSum += n ...