简体   繁体   中英

PHP Unit tests in NetBeans 7.3

I using NetBeans 7.3 with PHP Unit test.

Test file creation works. I can create test file by right-click: tools->create PHP unit test. New test is created in Test folder (filenameTest.php). When I run the test (Ctr+F6), in output window, I am getting error saying: that it can't find the file I am trying to test. If I include_once the original file into test file everything works, and I can run the tests.

I would prefer not to add that include_once line manually into each testFile. Is there a way to have NetBeans to do that for me automatically? Or how do I configure bootstrap file and/or phpunit.xml file, so it works without including the original file into testfile?

Thanks in advance.

You can create a bootstrap.php file in your tests folder that registers an autoloader to load the class that you are testing. You would then have a phpunit.xml file with the following:

<phpunit bootstrap="bootstrap.php">
</phpunit>

Registering an autoloader would be the easiest solution as then you don't have to remember to include files or if you or someone else isn't using NetBeans there aren't any problems with creating new tests.

Inside the bootstrap.php would be:

function autoloader($className) {
    *** do logic to set path of file for the class ***
    $classPath = "/base/path/for/file/" . $className;
    require_once($classPath);
}

spl_autoload_register('autoloader');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM