3.3 示例
文件 Demo.php:
<?php
class Demo
{
public function sum($a, $b)
{
return $a + $b;
}
public function subtract($a, $b)
{
return $a - $b;
}
}
?>
测试用例:文件 DemoTest.php:
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__). '/Demo.php');
class DemoTest extends PHPUnit_Framework_TestCase
{
public function testSum()
{
$demo = new Demo();
$this->assertEquals(4, $demo->sum(2, 2));
$this->assertNotEquals(3, $demo->sum(1, 1));
}
}
?>
测试结果:
PHPUnit 3.4.14 by Sebastian Bergmann.
.
Time: 0 seconds, Memory: 3.75Mb
OK (1 test, 3 assertions)
3.4 示例
文件 Foo.php:
<?php
class Foo
{
function foo()
{
}
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;
}
}
?>
测试用例:文件 FooTest.php:
<?php
require_once('PHPUnit/Framework.php');
require_once(dirname(__FILE__). '/Foo.php');
class fooTest extends PHPUnit_Framework_TestCase
{
//这个成员变量是存放要测试的类引用
private $obj;
//构造函数
function fooTest($name)
{
}
//new一个要测试的类为成员变量obj赋值
function setUp()
{
$this->obj = new Foo;
}
//unset要测试的类
function tearDown()
{
unset($this->obj);
}
//自定义的testcase
function testformatn1()
{
//调用要测试的类的方法,结果放到$result变量
$result = $this->obj->formatn("100.234");
//期望结果
$expected = "100.23";
//判断是否相等,这里使用assertTrue方法来判断布而值是否为true。
$this->assertTrue($result == $expected);
}
function testformatn2()
{
$result = $this->obj->formatn("0.100234");
$expected = "0.10023";
$this->assertTrue($result == $expected);
}
function testformatn3()
{
$result = $this->obj->formatn("0.100235");
$expected = "0.10024";
$this->assertTrue($result == $expected);
}
function testformatn4()
{
$result = $this->obj->formatn("0.000100235");
$expected = "0.00010024";
$this->assertTrue($result == $expected);
}
function testformatn5()
{
$result = $this->obj->formatn("0.000100232");
$expected = "0.00010023";
$this->assertTrue($result == $expected);
}
function testformatn6()
{
$result = $this->obj->formatn("1343");
$expected = "1343";
$this->assertTrue($result == $expected);
}
function testformatn7()
{
$result = $this->obj->formatn("1343.01");
$expected = "1343";
$this->assertTrue($result == $expected);
}
function testformatn8()
{
$result = $this->obj->formatn("1343.05");
$expected = "1343.1";
$this->assertTrue($result == $expected);
}
function testformatn9()
{
$result = $this->obj->formatn("0");
$expected = "0";
$this->assertTrue($result == $expected);
}
function testformatn10()
{
$result = $this->obj->formatn("105.2342");
$expected = "105.23";
$this->assertTrue($result == $expected);
}
function testformatn11()
{
$result = $this->obj->formatn("105.2375");
$expected = "105.24";
$this->assertTrue($result == $expected);
}
function testformatn12()
{
$result = $this->obj->formatn("0.000523751");
$expected = "0.00052375";
$this->assertTrue($result == $expected);
}
function testformatn13()
{
$result = $this->obj->formatn("0.000523755");
$expected = "0.00052376";
$this->assertTrue($result == $expected);
}
}
?>
测试结果:
PHPUnit 3.4.14 by Sebastian Bergmann.
.....F.......
Time: 0 seconds, Memory: 4.00Mb
There was 1 failure:
1) fooTest::testformatn6
Failed asserting that <boolean:false> is true.
C:\Program Files\Zend\Apache2\htdocs\unit_test-2\FooTest.php:70
FAILURES!
Tests: 13, Assertions: 13, Failures: 1.
3.5 测试套件示例:
文件 AllTests.php:
<?php
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'DemoTest.php';
require_once 'FooTest.php';
class AllTests
{
public static function suite()
{
$suite = new PHPUnit_Framework_TestSuite('Zend Framework - Zend');
$suite->addTestSuite('DemoTest');
$suite->addTestSuite('FooTest');
return $suite;
}
public static function main()
{
PHPUnit_TextUI_TestRunner::run(self::suite());
}
}
?>
测试结果:
PHPUnit 3.4.14 by Sebastian Bergmann.
......F.......
Time: 0 seconds, Memory: 4.00Mb
There was 1 failure:
1) fooTest::testformatn6
Failed asserting that <boolean:false> is true.
C:\Program Files\Zend\Apache2\htdocs\unit_test-2\FooTest.php:70
FAILURES!
Tests: 14, Assertions: 16, Failures: 1.
======================================================================
相关链接:
PHPUnit官方:http://www.phpunit.de/
PHPUnit文档:http://www.phpunit.de/pocket_guide/3.2/en/index.html
作者:张庆(网眼) 西安 PHP 教育培训中心 2010-7-4
来自“网眼视界”:http://blog.why100000.com
作者微博:http://t.qq.com/zhangking
“十万个为什么”电脑学习网:http://www.why100000.com