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

问题描述 投票:4回答:2

我知道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

或类似的东西

zend-framework phpunit
2个回答
4
投票

VisualPHPUnit

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

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


PHPUnit_TextUI_Command::main(false);

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

注意:这是非常准系统,简单和粗略的。


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..."

*/
© www.soinside.com 2019 - 2024. All rights reserved.