采用SimpleTest进行PHP单元测试
来源:互联网 发布:一个人的车站知乎 编辑:程序博客网 时间:2024/05/16 12:07
SimpleTest : http://simpletest.org/index.html
文件操作类 file.class.php
- <?php
- class File
- {
- var $filename;
- function File($file)
- {
- if (emptyempty($file)) {
- die('Filename should not be empty');
- }
- $this->filename = $file;
- if (!$this->exists() && !$this->create()) die('Unable to find / create the file');
- }
- function exists()
- {
- return file_exists($this->filename);
- }
- function create()
- {
- if ($this->exists()) return true;
- return $this->putContents(null);
- }
- function getContents()
- {
- return @file_get_contents($this->filename);
- }
- function putContents($content)
- {
- $fp = @fopen($this->filename, 'wb');
- $write = @fwrite($fp, $content);
- @fclose($fp);
- return !($write === false);
- }
- function append($content)
- {
- $fp = @fopen($this->filename, 'ab');
- $append = @fwrite($fp, $content);
- @fclose($fp);
- return !($append === false);
- }
- function delete()
- {
- return @unlink($this->filename);
- }
- }
- ?>
单元测试类 fileTest.php
- <?php
- //include_once '../simpletest/unit_tester.php';
- //include_once '../simpletest/reporter.php';
- include_once('../simpletest/autorun.php');
- include_once 'file.class.php';
- class fileTest extends UnitTestCase
- {
- var $file;
- var $filename;
- function fileTest($filename)
- {
- $this->filename = $filename;
- $this->file = new File($this->filename);
- $this->UnitTestCase('File Manipulation Test');
- }
- /**
- * 描述:该函数将会在每次测试开始前自动调用,
- * 在编写测试类的时候,是个可选函数,可写不可不写
- */
- function setUp()
- {
- $fp = @fopen($this->filename, 'x');
- if (!($fp == false)) {
- fwrite($fp, '');
- fclose($fp);
- }
- }
- /**
- * 描述:该函数将会在每次测试结束后自动调用,
- * 在编写测试类的时候,是个可选函数,可写不可不写
- */
- function tearDown()
- {
- @unlink($this->filename);
- }
- function testFileExists()
- {
- $this->assertTrue($this->file->exists());
- clearstatcache();
- $this->assertTrue(file_exists($this->file->filename));
- }
- function testCreate()
- {
- $this->tearDown();
- $this->assertFalse(file_exists($this->file->filename));
- clearstatcache();
- $this->assertTrue($this->file->create());
- $this->assertTrue(file_exists($this->file->filename));
- }
- function testReadWrite()
- {
- $contents = 'content';
- $this->assertTrue($this->file->putContents($contents));
- $this->assertEqual($this->file->getContents(), $contents);
- }
- function testAppend()
- {
- $this->file->putContents('SimpleTest');
- $this->assertTrue($this->file->append('NeverMind'));
- $this->assertWantedPattern('~nevermind$~i', $this->file->getContents());
- }
- function testDelete()
- {
- $this->assertTrue($this->file->delete());
- $this->assertFalse(file_exists($this->filename));
- }
- }
- $test = new fileTest('test.txt');
- $test->run(new HtmlReporter());
- ?>
- 采用SimpleTest进行PHP单元测试
- PHP 单元测试工具 SimpleTest
- 【转】在PHP中使用SimpleTest进行单元测试
- android 怎么进行调试程序 如何采用AndroidTestCase进行单元测试
- CakePHP: SimpleTest找不到arguments.php文件
- php一个简单的测试工具simpletest
- Php web测试工具simpletest使用
- junit单元测试时,java.lang.ClassNotFoundException: java.pakc.SimpleTest
- 单元测试报错java.lang.ClassNotFoundException: java.pakc.SimpleTest
- 利用单元测试对PHP 代码进行检查
- 利用单元测试对PHP 代码进行检查
- phpstorm配置phpunit,进行php单元测试
- 进行单元测试
- 使用 PHPUnit 进行 PHP 的单元测试(概念介绍)
- php单元测试
- 单元测试 PHP
- php单元测试
- php单元测试
- js技巧收集(200多个)
- JS实现浏览器菜单命令
- MyEclipse快捷键[转]
- 极酷的多级右键菜单
- 刷新不变化树形菜单(可做为后台)
- 采用SimpleTest进行PHP单元测试
- 非常棒的绿色下拉透明菜单
- Eclipse高亮显示
- 不按Ctrl实现Multiple Select多选
- 超强超靓网页日历代码
- PowerShell 杂记(一)
- 全国省市县无刷新多级关联菜单
- (转)国际观点:有助于你编程而立即要做的10件事
- 从firefox在中国占有率从下载日之后急剧下降所想到的