Running The Test Case
So now all our tests are ready. We then instantiate the test case by:
$test = new fileTest('test.txt'); |
Now try running this script….
Nothing appeared, right? Remember when we talked about the line $this->UnitTestCase('File Manipulation Test') and how it's optional and it will only create a header in the reporter? Yes, We have to use the reporter to show how the tests went!
To use the reporter, we have to include the file reporter.php (at the top for convenience) so our file top should look like this:
include_once 'simpletest/unit_tester.php'; include_once 'simpletest/reporter.php';
include_once 'file.class.php'; |
and then create the reporter and run it by the test case object by adding this line at the end:
$test = new fileTest('test.txt'); $test->run(new HtmlReporter()); |
Finally, our test case script should be like this:
[fileTest.php]
<?php include_once 'simpletest/unit_tester.php'; include_once 'simpletest/reporter.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 = 'contents'; $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()); ?> |
Now run the script again and if everything goes fine you should see something similar to this if all tests passed:
File Manipulation Test
1/1 test cases complete: 11 passes, 0 fails and 0 exceptions. |
In case we had any failure, you will see something similar to:
File Manipulation Test
Fail: testAppend -> Pattern [~^nevermind$~i] not detected in [String: SimpleTestNeverMind] at line [61]
1/1 test cases complete: 10 passes, 1 fails and 0 exceptions. |
As you can see, there was a failure in the method testAppend() and the error message has clearly stated what and where the error is.
Note that this test failed because I edited the pattern and forced the contents of the file to be "NeverMind" only in order to pass (case-insensitive) which, of course, made the test fails.
In case you had any failures, the script will continue and will not terminate until all tests are performed.
If any PHP errors occurred, such as warnings or notices, they will be caught and reported as exceptions.