PHPUnit:如何从浏览器运行测试?

时间:2013-07-12 06:36:05

标签: zend-framework phpunit

我知道PHPUnit测试可以从命令行执行,但有一种简单的方法可以从浏览器运行它。例如,我有:

class testsSuite extends PHPUnit_Framework_TestSuite
{

    public function __construct ()
    {
        $this->setName('testsSuite');
        $this->addTestSuite('MyFirstTest');
    }

    public static function suite ()
    {
        return new self();
    }
}

然后我有:

class MyFirstTest extends PHPUnit_Framework_TestCase
{

    protected function setUp ()
    {        
        parent::setUp();
    }

    protected function tearDown ()
    {
        parent::tearDown();
    }

    public function __construct ()
    {

    }

    public function test__construct ()
    {

    }

    public function test__destruct () {

    }

    public function testCalculateCost ()
    {
        print 1; die();
    }

}

我可以在浏览器中输入一个URL来执行此测试吗?类似的东西:

http://localhost/tests/testsSuite/calculateCost

或类似的东西

2 个答案:

答案 0 :(得分:4)

VisualPHPUnit

在工作中,我们有时会从浏览器运行,在其基本形式中使用包含以下内容的php脚本:

$argv = array(
    '--configuration', 'phpunit.xml',
    './unit',
);
$_SERVER['argv'] = $argv;


PHPUnit_TextUI_Command::main(false);

因此,您基本上将通常在命令行上键入的所有参数放入数组中,并将其设置在$ _SERVER-global中。 PHPUnit_TextUI_Cmmand ::主(假);然后运行你的测试。 false参数确保不调用exit()。 在PHPUnit.xml中,我配置为登录到JSON文件,并且php-script读取该文件以显示结果(在测试之后它可以执行,因为没有调用exit)。

注意:这是非常准确,简单和粗糙的。

答案 1 :(得分:1)

我找到了一个行之有效的解决方案:

<?php

define("PHPUNIT_COMPOSER_INSTALL","vendor/autoload.php");

require PHPUNIT_COMPOSER_INSTALL;

$query = $_SERVER["QUERY_STRING"];

//$_SERVER["QUERY_STRING"] to $_SERVER['argv'];

$_SERVER['argv'] = explode("&",$query);

//PHPUnit use $_SERVER['argv'] for input value

PHPUnit\TextUI\Command::main(false);

/*Use

command line "./vendor/bin/phpunit param1 param2 param..."

browser URI "localhost?param1&param2&param..."

*/
相关问题