本节内容:
一例PHPUnit单元测试的代码
例子:
<?php
/*PHPUnit单元测试
* 环境:widnows zendstudio 7.2
* 右击项目:properties PHPincludePath Library Add PHPUnitCase
* 新建PHPUnit Test Case 自动生成如下文件
* Run As PHPUnit Case :alt+shit+x U
* @ site www.jb200.com
*/
require_once 'PHPUnitFrameworkTestCase.php';
/**
* test case.
*/
class PHPTest extends PHPUnit_Framework_TestCase {
/**
* Prepares the environment before running a test.
*/
protected function setUp() {
parent::setUp ();
// TODO Auto-generated PHPTest::setUp()
}
/**
* Cleans up the environment after running a test.
*/
protected function tearDown() {
// TODO Auto-generated PHPTest::tearDown()
parent::tearDown ();
}
/**
* Constructs the test case.
*/
public function __construct() {
// TODO Auto-generated constructor
}
/*
* 测试方法名 前面加 test+测试方法名
* 注意 lessThan 是 后者lessThan前者
*/
public function testFun()
{
$this->assertEquals(1,$this->Fun());//判断是否相等
$this->assertNotEquals(2,$this->Fun());//不相等
$this->assertLessThan(2,$this->Fun());
$this->assertLessThanOrEqual(2,$this->Fun());
$this->assertGreaterThan(0,$this->Fun());
}
//被测试的方法
public function Fun()
{
return 1;
}
public function testFun1()
{
$this->assertArrayHasKey('one',$this->Fun1());
$this->assertContains(1,$this->Fun1(),'Fun1() return not contains 1');
}
public function Fun1()
{
return array('one'=>1,'two'=>2);
}
}