您的位置:首页 > 编程语言 > PHP开发

phpunit api PHPUnit_Extensions_OutputTestCase

2012-05-26 18:57 447 查看
PHPUnit_Extensions_OutputTestCase 断言方法如下

1 void expectOutputRegex(string $regularExpression)设定期望值为输出匹配$regularExpression。

2 void expectOutputString(string $expectedString)设定期望值为输出同$expectedString一样

3 bool setOutputCallback(callable $callback)
设定回调方法,例如用于规格化实际输出。

4 这个扩展应该在做action 测试时应该很有用

<?php

// 测试输出MyOutputTestCase.php

function hanshu($str){

$match = "/^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/";

return preg_match($match,$str);

}

class MyOutputTestCase extends PHPUnit_Extensions_OutputTestCase {

public function testExpectFooActualFoo()

{

$this->expectOutputString('foo');

print 'foo';

}



public function testExpectBarActualBaz()

{

$this->expectOutputString('bar');

print 'baz';

}

public function testExpectEmail()

{

$this->expectOutputRegex('/^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/');

print 'admin@163.com';

}

public function testExpectEmailNo()

{

$this->expectOutputRegex('/^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$/');

print '12537838383';

}

public function testExpectCallbackEmail()

{

$this->setOutputCallback("hanshu");

//print 'admin@163.com';

}

public function testExpectCallbackEmailNo()

{

$this->setOutputCallback("hanshu");

//print '12537838383';

}

}

?>

注意: 一开开始测试setOutputCallback 老是返回true 结果,但是安装了DbUnit 后 就正常了,也不知道什么原因

<?php

// 测试套件TestSuite.php

require_once 'PHPUnit/Autoload.php';

class MyTestSuite extends PHPUnit_Framework_TestSuite {

// SetUp方法是在每个测试用例运行前进行一些初始化的工作,比如创建业务对象,让其他测试方法也可以使用业务对象,或数据库连接对象

protected function setUp(){

$this->data= "somesthing";

}

//tearDown则在每个测试用例运行后进行一些比如资源的释放等工作的工作

protected function tearDown(){

//$this->sharedFixture= null;

}

//注意此处设置为static

public static function suite() {

$suite = new self();

$suite->addTestFile("MyTestCase.php");

$suite->addTestFile("MyOutputTestCase.php");

return $suite;

}

}

?>

执行phpunit TestSuite.php
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: