开发者

Trouble Building a Simpletest Suite From Working Unit Test Cases

开发者 https://www.devze.com 2023-04-07 18:43 出处:网络
I\'ve started using unit testing for my PHP programs, and figured Simpletest was as good a place to dive in as any other.I added the Simpletest files to my testing server, and ran the following tests

I've started using unit testing for my PHP programs, and figured Simpletest was as good a place to dive in as any other. I added the Simpletest files to my testing server, and ran the following tests on my custom PDO classes:

<?php
require_once('../simpletest/autorun.php');
require_once('../includes/inc_sql.php');

class TestOfSQL extends UnitTestCase{
    function testRead(){
        ...
    }

    function testWriteAndDelete(){
        ...
    }
}
?>

That all works out smashingly. I try to build a test suite involving (so far) just that testing file, as follows:

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

class AllTests extends TestSuite {

    function __construct(){
        parent::__construct();
        $this->addFile('inc_sql_test.php');
    }
}

This crashes and burns, and I get the following readout:

Warning: include_once(inc_sql_test.php) [function.include-once]: failed to open stream: No such file or directory in E:\xampp\htdocs\historicMuncie\simpletest\test_case.php on line 382

Warning: include_once() [function.include]: Failed opening 'inc_sql_test.php' for inclusion (include_path='.;E:\xampp\php\PEAR') in E:\xampp\htdocs\historicMuncie\simpletest\test_case.php on line 382

Warning: file_get_contents(inc_sql_test.php) [function.file-get-contents]: failed to open stream: No such file or directory in E:\xampp\htdocs\historicMuncie\simpletest\test_case.php on line 418
all_tests.php
Fail: AllTests -> inc_sql_test.php -> Bad TestSuite [inc_sql_test.php] with error [No runnable test cases in [inc_sql_test.php]]
0/0 test cases complete: 0 passes, 1 fails and 0 exceptions.

I've played around with include paths, web root vs. server root notation - anythi开发者_Go百科ng that came to mind, but nothing is allowing that test suite to run properly. Any ideas?


I always cringe when I see relative paths in PHP scripts. It's much easier to implement and maintain when using "semi-absolute" paths based on a common root. Try:

$this->addFile( $_SERVER['DOCUMENT_ROOT'] . '/inc_sql_test.php' );


You can also do this:

$this->addFile(dirname(__FILE__) . '/inc_sql_test.php');

Regards!

0

精彩评论

暂无评论...
验证码 换一张
取 消