+ -
当前位置:首页 → 问答吧 → php一个简单的测试工具simpletest

php一个简单的测试工具simpletest

时间:2011-03-30

来源:互联网

转:jackyrong

php一个简单的测试工具simpletest





phpunit是很好的单元测试工具,而本文介绍一款更轻量级的单元测试工具,开源的,
simpletest,

1 下载:
   http://sourceforge.net/projects/simpletest/
可惜文档及项目主站要那个XXX,大家懂的

2 使用
   下载后,只要测试文件中包含如下两个文件,就可以用了
  1. <?php
  2. require_once('simpletest/autorun.php');
  3. require_once('simpletest/web_tester.php');

  4. ?>
复制代码
3 比如测试一个界面吧
  1. <?php
  2. require_once('simpletest/autorun.php');
  3. require_once('simpletest/web_tester.php');

  4. class SimpleFormTests extends WebTestCase {

  5.   function testDoesContactPageExist() {
  6.     $this->get('http://www.example.com/contact.php');
  7.     $this->assertResponse(200);
  8.   }

  9. }

  10. ?>
复制代码
还可以测试表单的提交动作呢
  1. function testIsProperFormSubmissionSuccessful() {

  2.   $this->get('http://www.example.com/contact.php');
  3.   $this->assertResponse(200);

  4.   $this->setField("name", "Jason");
  5.   $this->setField("email", "[email protected]");
  6.   $this->setField("message", "I look forward to hearing from you!");

  7.   $this->clickSubmit("Contact us!");

  8.   $this->assertResponse(200);
  9.   $this->assertText("We will be in touch within 24 hours.");

  10. }
复制代码
运行后会看到通过的情况



再举一个单元测试的例子:
比如有个类LOG,是在磁盘上建立文件
  1. <?php
  2. require_once('simpletest/unit_tester.php');
  3. require_once('simpletest/reporter.php');
  4. require_once('../classes/log.php');

  5. class TestOfLogging extends UnitTestCase {
  6.    
  7.     function testCreatingNewFile() {
  8.         @unlink('/temp/test.log');
  9.         $log = new Log('/temp/test.log');
  10.         $this->assertFalse(file_exists('/temp/test.log'));
  11.         $log->message('Should write this to a file');
  12.         $this->assertTrue(file_exists('/temp/test.log'));
  13.     }
  14. }

  15. $test = &new TestOfLogging();
  16. $test->run(new HtmlReporter());
  17. ?>
复制代码
测试方法中所有都默认以test开头的,这点要注意,最后用$test->run(new HtmlReporter());表示以HTML格式输出

作者: so_brave   发布时间: 2011-03-30

作者: ws00377531   发布时间: 2011-03-30