【问题标题】:PHPUnit: How to run tests from browser?PHPUnit:如何从浏览器运行测试?
【发布时间】:2013-07-10 15:16:40
【问题描述】:

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


【解决方案1】:

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 脚本读取该文件以显示结果(它可以在测试后执行,因为没有调用退出)。

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

【讨论】:

    【解决方案2】:

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

    <?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..."
    
    */
    

    【讨论】:

    • 不鼓励仅使用代码回答。请添加一些解释,说明这如何解决问题,或者这与现有答案有何不同。 From Review
    猜你喜欢
    • 2014-06-17
    • 2012-04-24
    • 1970-01-01
    • 2011-05-25
    • 1970-01-01
    • 2014-12-27
    • 2018-03-06
    • 1970-01-01
    • 2021-11-06
    相关资源
    最近更新 更多