- 浏览: 239097 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
thepastsee:
304572183股票交流 欢迎加入
实时股票 -
345161974:
很不错,我第一个接触的CSS卡片布局效果,多谢
css卡片效果
今天接到了个任务,需要对数字进行计算,因为涉及到整数,小数,和科学计数法等很多条件,所以人工测试非常麻烦,于是想到了PHP的单元测试工具
PHPUnit,所以写个文档备查。
看了PHPUnit的文档之后基本有了一些了解,
http://pear.php.net/manual/en/packages.php.phpunit.intro.php
工作流程如下:
1.设计你的class/API
2.创建测试程序集
3.实现class/API
4.运行测试
5.修正测试失败或错误,回到第4步。
我们来举个例子:
下面是你要测试的class,其中formatn函数一个取任意数字的5位有效数字的函数。
CODE:
----------format_number.php-----------
class fo {
function fo() {
}
function formatn($num) {
$num = rtrim($num,"0");
$pos = strpos($num,".");
$num = str_replace(".","",$num);
$count1 = strlen($num);
$num = ltrim($num,"0");
$count2 = strlen($num);
$zeroc = $count1 - $count2;
$num = substr($num,0,6);
$num = round($num/10);
//$num = str_pad($num, 5, "0");
if ($pos !== false) {
$num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
$dotl = substr($num,0,$pos);
$dotr = substr($num,$pos);
$num = $dotl.".".$dotr;
}
return $num;
}
}
class fo {
function fo() {
}
function formatn($num) {
$num = rtrim($num,"0");
$pos = strpos($num,".");
$num = str_replace(".","",$num);
$count1 = strlen($num);
$num = ltrim($num,"0");
$count2 = strlen($num);
$zeroc = $count1 - $count2;
$num = substr($num,0,6);
$num = round($num/10);
//$num = str_pad($num, 5, "0");
if ($pos !== false) {
$num = str_pad($num, (strlen($num)+$zeroc), "0", STR_PAD_LEFT);
$dotl = substr($num,0,$pos);
$dotr = substr($num,$pos);
$num = $dotl.".".$dotr;
}
return $num;
}
}
接着创建TestCase,继承自PHPUnit_TestCase
CODE:
----------testcase.php-----------
<?php
require_once 'format_number.php';
require_once 'PHPUnit.php';
class foTest extends PHPUnit_TestCase {
//这个成员变量是存放要测试的类引用
var $abc;
//构造函数
function foTest($name) {
$this->;PHPUnit_TestCase($name);
}
//new一个要测试的类为成员变量abc赋值
function setUp() {
$this->;abc = new fo;
}
//unset要测试的类
function tearDown() {
unset($this->;abc);
}
//自定义的testcase
function testFormatn1() {
//调用要测试的类的方法,结果放到$result变量
$result = $this->;abc->;formatn("100.234");
//期望结果
$expected = "100.23";
//判断是否相等,这里使用assertTrue方法来判断布而值是否为true。
$this->;assertTrue($result == $expected);
}
function testFormatn2() {
$result = $this->;abc->;formatn("0.100234");
$expected = "0.10023";
$this->;assertTrue($result == $expected);
}
function testFormatn3() {
$result = $this->;abc->;formatn("0.100235");
$expected = "0.10024";
$this->;assertTrue($result == $expected);
}
function testFormatn4() {
$result = $this->;abc->;formatn("0.000100235");
$expected = "0.00010024";
$this->;assertTrue($result == $expected);
}
function testFormatn5() {
$result = $this->;abc->;formatn("0.000100232");
$expected = "0.00010023";
$this->;assertTrue($result == $expected);
}
function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn7() {
$result = $this->;abc->;formatn("1343.01");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn8() {
$result = $this->;abc->;formatn("1343.05");
$expected = "1343.1";
$this->;assertTrue($result == $expected);
}
function testFormatn9() {
$result = $this->;abc->;formatn("0");
$expected = "0";
$this->;assertTrue($result == $expected);
}
function testFormatn10() {
$result = $this->;abc->;formatn("105.2342");
$expected = "105.23";
$this->;assertTrue($result == $expected);
}
function testFormatn11() {
$result = $this->;abc->;formatn("105.2375");
$expected = "105.24";
$this->;assertTrue($result == $expected);
}
function testFormatn12() {
$result = $this->;abc->;formatn("0.000523751");
$expected = "0.00052375";
$this->;assertTrue($result == $expected);
}
function testFormatn13() {
$result = $this->;abc->;formatn("0.000523755");
$expected = "0.00052376";
$this->;assertTrue($result == $expected);
}
}
<?php
require_once 'format_number.php';
require_once 'PHPUnit.php';
class foTest extends PHPUnit_TestCase {
//这个成员变量是存放要测试的类引用
var $abc;
//构造函数
function foTest($name) {
$this->;PHPUnit_TestCase($name);
}
//new一个要测试的类为成员变量abc赋值
function setUp() {
$this->;abc = new fo;
}
//unset要测试的类
function tearDown() {
unset($this->;abc);
}
//自定义的testcase
function testFormatn1() {
//调用要测试的类的方法,结果放到$result变量
$result = $this->;abc->;formatn("100.234");
//期望结果
$expected = "100.23";
//判断是否相等,这里使用assertTrue方法来判断布而值是否为true。
$this->;assertTrue($result == $expected);
}
function testFormatn2() {
$result = $this->;abc->;formatn("0.100234");
$expected = "0.10023";
$this->;assertTrue($result == $expected);
}
function testFormatn3() {
$result = $this->;abc->;formatn("0.100235");
$expected = "0.10024";
$this->;assertTrue($result == $expected);
}
function testFormatn4() {
$result = $this->;abc->;formatn("0.000100235");
$expected = "0.00010024";
$this->;assertTrue($result == $expected);
}
function testFormatn5() {
$result = $this->;abc->;formatn("0.000100232");
$expected = "0.00010023";
$this->;assertTrue($result == $expected);
}
function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn7() {
$result = $this->;abc->;formatn("1343.01");
$expected = "1343";
$this->;assertTrue($result == $expected);
}
function testFormatn8() {
$result = $this->;abc->;formatn("1343.05");
$expected = "1343.1";
$this->;assertTrue($result == $expected);
}
function testFormatn9() {
$result = $this->;abc->;formatn("0");
$expected = "0";
$this->;assertTrue($result == $expected);
}
function testFormatn10() {
$result = $this->;abc->;formatn("105.2342");
$expected = "105.23";
$this->;assertTrue($result == $expected);
}
function testFormatn11() {
$result = $this->;abc->;formatn("105.2375");
$expected = "105.24";
$this->;assertTrue($result == $expected);
}
function testFormatn12() {
$result = $this->;abc->;formatn("0.000523751");
$expected = "0.00052375";
$this->;assertTrue($result == $expected);
}
function testFormatn13() {
$result = $this->;abc->;formatn("0.000523755");
$expected = "0.00052376";
$this->;assertTrue($result == $expected);
}
}
最后还需要一个运行测试的程序
CODE:
----------runtest.php-----------
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';
$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);
echo $result->;toString();
?>;
<?php
require_once 'testcase.php';
require_once 'PHPUnit.php';
$suite = new PHPUnit_TestSuite("foTest");
$result = PHPUnit::run($suite);
echo $result->;toString();
?>;
现在就可以通过命令行运行这个testcase
php runtest.php
得到结果如下:
CODE:
TestCase foTest->;testFormatn1() passed
TestCase foTest->;testFormatn2() passed
TestCase foTest->;testFormatn3() passed
TestCase foTest->;testFormatn4() passed
TestCase foTest->;testFormatn5() passed
TestCase foTest->;testFormatn7() passed
TestCase foTest->;testFormatn8() passed
TestCase foTest->;testFormatn9() passed
TestCase foTest->;testFormatn10() passed
TestCase foTest->;testFormatn11() passed
TestCase foTest->;testFormatn12() passed
TestCase foTest->;testFormatn13() passed
TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE
TestCase foTest->;testFormatn2() passed
TestCase foTest->;testFormatn3() passed
TestCase foTest->;testFormatn4() passed
TestCase foTest->;testFormatn5() passed
TestCase foTest->;testFormatn7() passed
TestCase foTest->;testFormatn8() passed
TestCase foTest->;testFormatn9() passed
TestCase foTest->;testFormatn10() passed
TestCase foTest->;testFormatn11() passed
TestCase foTest->;testFormatn12() passed
TestCase foTest->;testFormatn13() passed
TestCase foTest->;testFormatn6() failed: expected TRUE, actual FALSE
其中testFormatn6的测试失败,
我们就可以去检查一下我们的代码在什么地方出问题了。
补充一点
也可以把assertTrue方法换assertEquals,如下:
CODE:
function testFormatn6() {
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertEquals($expected, $result);
}
$result = $this->;abc->;formatn("1343");
$expected = "1343";
$this->;assertEquals($expected, $result);
}
如果失败得到对应的结果会直观一些(可以显示错误的结果):
CODE:
TestCase foTest->;testFormatn8() failed: expected 1343 , actual 134.
发表评论
-
phphphphphphph
2012-03-01 22:27 0回归回归lkjlkjlkjlkjlkjlkjlkj -
php_相对路径_转
2012-03-01 22:07 0<?php $str1 = "/home/i ... -
php_自定义排序 关联数组
2012-03-01 21:11 0<?php //用PHP设计一个函数,对学生英语考试得 ... -
php_讲义
2012-02-29 13:07 0ewrwerwer -
php 禁止危险的函数_转
2012-02-27 12:28 0阿酷影 一些需要禁用的PHP危险函数 ... -
转_phpcan't return a result set in the given context及参数解释
2012-02-13 23:49 1276关键就是两点 defin ... -
php_可以编的表格
2012-01-29 10:42 841虽 -
php_oa
2011-10-20 16:48 657dfsdfsfsf -
jquery_upload
2011-09-12 13:07 675http://fangyong2006.iteye.com/b ... -
php__表单处理
2011-08-21 17:04 820sdfadfadsf -
$_SESSION 无法跨页保存数据
2011-08-21 11:46 916今天可遇到问题 $_SESSION 无法跨页保存数据 原 ... -
php 下载地址
2011-08-13 14:51 747window http://windows.php.n ... -
订单号__14位_转
2011-06-27 23:45 875http://bbs.phpchina.com/thread- ... -
9个免费 php 库_转
2011-06-24 21:17 10069个强大免费的PHP库 & 20 你应该知道的PHP ... -
php_错误解决_sessioin
2011-06-01 16:29 1060写购物车时遇到问题, Fatal error ... -
php面试题_转
2011-05-27 01:25 771kjljk -
jquery_ajax_table_php
2011-05-27 01:04 688jljklkj -
php算法_王伟阳
2011-05-23 13:17 786232342 -
php简历
2011-05-18 00:39 1068kjlkjlkj -
黑夜路人转__
2011-05-13 21:41 771lklk
相关推荐
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetTitle('图片转PDF示例'); // 添加页面 $pdf->AddPage(); // 将图片...
php__unit getUTCDay get_utc__day 该库可以相互转换camelCase | StudlyCaps和火车案例。 camelCase / StudlyCaps 火车箱 勿忘我 勿忘我 WebkitFlexDirection -webkit-flex-direction 用法 这是完整案例的...
"unit-converter"是一个专为PHP开发的工具,主要用于进行各种物理量单位之间的转换。这个库的使用简单高效,尤其适合在PHP 5.4及更高版本中使用,它能够帮助开发者快速、准确地处理涉及不同单位的计算任务。下面我们...
composer require mightymango/unit_conversions 用法 $ unit_conversions = new Mightymango \ UnitConversions (); echo $ unit_conversions -> echoPhrase ( 'Hello, Simon!' ); 测验 composer test 变更日志 ...
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Your Name'); $pdf->SetTitle('HTML转PDF示例'); $pdf->SetSubject...
php封装好的人民币数值转中文大写类 class Num2RmbClass{ /** * 人民币数值转中文大写 * @author SunsCheung * @time 2015.11.11 * @param string $number 数值 默认为0 * @param string $int_unit 币种单位...
use Crisu83\Conversion\Quantity\DigitalInformation\Unit as DIUnit; use Crisu83\Conversion\Size\HatSize\System as HatSizeSystem; use Crisu83\Conversion\Size\ShoeSize\System as ShoeSizeSystem...
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->AddPage(); $pdf->writeHTMLCell(0, 0, '', '', $data, 0, 1, 0, true, '', true); $filePath = $filename ....
use Crisu83\Conversion\Unit\Length\UnitLength; ``` ### 单位转换的基本用法 PHP Conversion库提供了丰富的预定义单位和大小类,你可以创建相应的对象来进行转换。例如,转换长度单位: ```php $length = new ...
$unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb'); return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . '' . $unit[$i]; } echo convert(memory_get_usage(true)); ?> ``` 这个函数会根据...
总结来说,"PDFTOTEXT.rar"可能包含的是一个PHP实现的PDF转文本解决方案,利用了如`pdf2text`或`TCPDF`这样的工具或库。这个技术对于WEB开发者而言,尤其是在处理大量PDF数据时,能够极大地提高工作效率和数据处理...
辅助函数`_cny_map_unit()`的主要作用是对数字进行分级,例如将"12345"转换为"一万二千三百四十五"。它遍历数字列表,根据当前数字和已处理的数字列表构建中文表示。同时,这个函数会处理"零"的情况,确保在非零数字...
function rmb_format($money = 0, $int_unit = '元', $is_round = true, $is_extra_zero = false) { // ... } ``` - `$money`:这是要转换的数字,默认值为0。 - `$int_unit`:币种单位,默认为"元",但可根据需求...
ECShop是一款基于PHP语言开发的开源电子商务系统,其2.0版本的数据字典涉及到的关键信息主要是关于商品管理和分类管理的数据库表结构。这些表对于理解ECShop如何存储和处理商品数据至关重要。 首先,我们来看商品...
`TCPDF`是一个PHP类库,用于创建和编辑PDF文档,它不直接支持PDF转PNG,但可以结合其他工具如`ImageMagick`来实现。而`Ghostscript`则是一个强大的PDF处理工具,可以直接将PDF转换为各种图像格式。 1. **安装依赖**...
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('作者名'); $pdf->SetTitle('文档标题'); $pdf->SetSubject('PDF...
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetAuthor('Your Name'); $pdf->SetTitle('PDF Title'); $pdf->SetSubject('...
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetFont('stsongstdlight', '', 12, '', true); ``` 3. **HTML到PDF的转换**:HTML2PDF提供了一个`createPDF...
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); $pdf->SetCreator(PDF_CREATOR); $pdf->SetTitle('我的PDF文件'); $pdf->SetAuthor('作者'); $pdf->SetSubject('...