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
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
相关文章推荐
- phpunit api PHPUnit_Extensions_SeleniumTestCase
- phpunit api PHPUnit_Extensions_Database_TestCase
- AbstractDbunitTestCase
- PHP Unit Testing & Mock Objects with SimpleTest plugin for Eclipse
- yaf 添加phpunittest
- Yii and PHP Unit test
- 让一个继承unittest.TestCase的类下的setUp和tearDown只执行一次
- Python基础-单元测试-unittest.TestCase(setUp和tearDown)
- ActivityUnitTestCase示例
- python+requests+unittest API接口测试实例(详解)
- 关于测试框架junit dbunit strutstestcase
- instrumentation中的ActivityUnitTestCase学习笔记(4)
- Django unittest 设置跳过某些case
- [zz]How to produce html unit test output in Python?
- ActivityInstrumentationTestCase2 和 ActivityUnitTestCase
- phpunit api 未完成和跳过的测试
- Android Studio 自带测试ActivityInstrumentationTestCase2,ActivityUnitTestCase使用
- Struts2+Spring的UnitTest编写(使用StrutsTestCase的子类StrutsSpringTestCase)
- python进阶之Unittest写case(3)
- 应用Selenium unittest写单元测试case