Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPUnit: How to run tests from browser?

I know PHPUnit tests can be executed from the command line, but is there an easy way to run it from the browser. For example, I have:

class testsSuite extends PHPUnit_Framework_TestSuite
{

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

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

And then I have:

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();
    }

}

Is there a URL I can enter in my browser to execute this test? Something like:

http://localhost/tests/testsSuite/calculateCost

Or something similar

like image 272
coderama Avatar asked Jul 12 '13 06:07

coderama


People also ask

How do I run a test case in PHP?

From the Menu-bar, select Run | Run As | PHPUnit Test . To debug the PHPUnit Test Case, click the arrow next to the debug button on the toolbar, and select Debug As | PHPUnit Test . From the Main Menu, select Run | Debug As | PHPUnit Test . The unit test will be run and a PHP Unit view will open.

How do I start PHPUnit?

PHPUnit InstallationThe first command creates a folder in your current directory, test-project and the second command moves into it. The last command starts an interactive shell. Follow the prompt, filling in the details as required (the default values are fine).


1 Answers

There is VisualPHPUnit.

At work we sometimes run from browser, using in its basic form a php-script containing:

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


PHPUnit_TextUI_Command::main(false);

So you basically put all parameters you normally type on the commandline in an array, and set it in the $_SERVER-global. PHPUnit_TextUI_Cmmand::main(false); then runs your tests. The false-parameter makes sure no exit() is called. In the PHPUnit.xml I configure to log to a JSON file, and the php-script reads that file to show the results (which it can do after the tests because no exit was called).

Note: this is very barebone, simplistic and crude.

like image 122
qrazi Avatar answered Nov 15 '22 08:11

qrazi