SimpleTest

Grouping tests

Next up we will fill in some blanks and create a test suite.

Another test

Adding another test can be as simple as adding another method to a test case...

class TestOfLogging extends UnitTestCase {
    function TestOfLogging() {
        $this->UnitTestCase('Log class test');
    }
    function testCreatingNewFile() {
        @unlink('../temp/test.log');
        $log = new Log('../temp/test.log');
        $this->assertFalse(file_exists('../temp/test.log'), 'Created before message');
        $log->message('Should write this to a file');
        $this->assertTrue(file_exists('../temp/test.log'), 'File created');
        @unlink('../temp/test.log');
    }
    function testAppendingToFile() {
        @unlink('../temp/test.log');
        $log = new Log('../temp/test.log');
        $log->message('Test line 1');
        $messages = file('../temp/test.log');
        $this->assertWantedPattern('/Test line 1/', $messages[0]);
        $log->message('Test line 2');
        $messages = file('../temp/test.log');
        $this->assertWantedPattern('/Test line 2/', $messages[1]);
        @unlink('../temp/test.log');
    }
}

The assertWantedPattern() test case method uses Perl style regular expressions for matching.

All we are doing in this new test method is writing a line to a file and reading it back twice over. We simply want to confirm that the logger appends the text rather than writing over the old file. A little pedantic, but hey, it's a tutorial!

In fact this unit test actually passes straight away...

Log class test

1/1 test cases complete. 4 passes and 0 fails.
The trouble is there is already a lot of repetition here, we have to delete the test file before and after every test. With outrageous plagarism from JUnit, SimpleTest has setUp() and tearDown() methods which are run before and after every test respectively. File deletion is common to all the test methods so we should move that operation there.

Our tests are green so we can refactor...

class TestOfLogging extends UnitTestCase {
    function TestOfLogging() {
        $this->UnitTestCase('Log class test');
    }
    function setUp() {
        @unlink('../temp/test.log');
    }
    function tearDown() {
        @unlink('../temp/test.log');
    }
    function testCreatingNewFile() {
        $log = new Log('../temp/test.log');
        $this->assertFalse(file_exists('../temp/test.log'), 'Created before message');
        $log->message('Should write this to a file');
        $this->assertTrue(file_exists('../temp/test.log'), 'File created');
    }
    function testAppendingToFile() {
        $log = new Log('../temp/test.log');
        $log->message('Test line 1');
        $messages = file('../temp/test.log');
        $this->assertWantedPattern('/Test line 1/', $messages[0]);
        $log->message('Test line 2');
        $messages = file('../temp/test.log');
        $this->assertWantedPattern('/Test line 2/', $messages[1]);
    }
}

The test stays green. We can add non-test methods to the test case as long as the method name does not start with the string "test". Only the methods that start "test" are run. This allows further optional refactoring...

class TestOfLogging extends UnitTestCase {
    function TestOfLogging() {
        $this->UnitTestCase('Log class test');
    }
    function setUp() {
        @unlink('../temp/test.log');
    }
    function tearDown() {
        @unlink('../temp/test.log');
    }
    function getFileLine($filename, $index) {
        $messages = file($filename);
        return $messages[$index];
    }
    function testCreatingNewFile() {
        $log = new Log('../temp/test.log');
        $this->assertFalse(file_exists('../temp/test.log'), 'Created before message');
        $log->message('Should write this to a file');
        $this->assertTrue(file_exists('../temp/test.log'), 'File created');
    }
    function testAppendingToFile() {
        $log = new Log('../temp/test.log');
        $log->message('Test line 1');
        $this->assertWantedPattern('/Test line 1/', $this->getFileLine('../temp/test.log', 0));
        $log->message('Test line 2');
        $this->assertWantedPattern('/Test line 2/', $this->getFileLine('../temp/test.log', 1));
    }
}

It is a matter of taste whether you prefer this version to the previous one. There is a little more code, but the logic of the test is clearer.

A group test

A test case does not function alone for very long. When coding for real we usually want to run as many tests as quickly and as often as we can. This means grouping them together into test suites that could easily include every test in the application.

Firstly we have to clean out the test running code from our existing test case...

<?php
require_once('../classes/log.php');

class TestOfLogging extends UnitTestCase {
    ...
}
?>

Next we create a group test called all_tests.php in the tests folder...

<?php
require_once(dirname(__FILE__) . '/simpletest/autorun.php');
require_once('log_test.php');

$test = &new GroupTest('All tests');
$test->addTestCase(new TestOfLogging());
$test->run(new HtmlReporter());
?>

We hardly notice the difference when things work...

All tests

1/1 test cases complete. 4 passes and 0 fails.
Group tests add to the test case count. Adding new test cases is very straight forward. Simply include the test cases file and add any contained test cases individually. You can also nest group tests within other group tests (although you should avoid loops).

In the next page we will add these more quickly

转载于:https://www.cnblogs.com/candybox/articles/1803342.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值