PHPUnit runs only from the command line. Why?

2019-06-22 16:40发布

I have the following code:

<?php
    class MyTest extends PHPUnit_Framework_TestCase
    {
        public function testCalculate()
        {
            $this->assertEquals(2, 1 + 1);
        }
    }
?>

When I open the PHP file in the browser, I get the following error:

Fatal error: Class 'PHPUnit_Framework_TestCase' not found

However, if I use the command line it works fine: phpunit [local_path_here]/testcase.php

Result:

.

Time: 0 seconds, Memory: 5.00Mb

OK (1 test, 1 assertion)

Why is that? How can I make it to run it in the browser as well?

4条回答
你好瞎i
2楼-- · 2019-06-22 16:44

You may have a different include path on the command line. Check to see whether you have a php-cli.ini file in addition to the normal php.ini file. The first one will be used when you run PHP from the command line. That's probably got a different include_path setting. It might include the PEAR directory, for example, if PHPUnit was installed via PEAR.

查看更多
我欲成王,谁敢阻挡
3楼-- · 2019-06-22 16:49

You can integrate add-on for running unit tests via native web GUI:

https://github.com/NSinopoli/VisualPHPUnit

查看更多
在下西门庆
4楼-- · 2019-06-22 16:55

You can't run unit tests in the browser. Maybe in the future: http://sebastian-bergmann.de/archives/638-PHPUnit-3.0.html#c4983

If you want to view the code coverage run

phpunit --coverage-html=coverage testcase.php

and then open the index.html file in the coverage directory.

Otherwise, you have to run your tests from the command line.

查看更多
冷血范
5楼-- · 2019-06-22 17:07

You can use eclipse also to run phpunit. follow the link below

http://pkp.sfu.ca/wiki/index.php/Configure_Eclipse_for_PHPUnit

查看更多
登录 后发表回答