Home » Php » Run a PHPUnit Selenium test case programatically ("within PHP")

Run a PHPUnit Selenium test case programatically ("within PHP")

Posted by: admin July 12, 2020 Leave a comment


How can I run a test “within PHP” instead of using the ‘phpunit’ command? Example:

require_once 'PHPUnit/Extensions/SeleniumTestCase.php';
class MySeleniumTest extends PHPUnit_Extensions_SeleniumTestCase {

    protected function setUp() {

    public function testMyTestCase() {


$test = new MySeleniumTest();
//I want to run the test and get information about the results so I can store them in the database, send an email etc.

Or do I have to write the test to a file, invoke phpunit via system()/exec() and parse the output? 🙁

How to&Answers:

Just use the Driver that’s included.

require_once 'PHPUnit/Extensions/SeleniumTestCase/Driver.php';
//You may need to load a few other libraries.  Try it.

Then you need to set it up like SeleniumTestCase does:

$driver = new PHPUnit_Extensions_SeleniumTestCase_Driver;

Then just:



Here’s an example from the phpunit docs:

require_once 'PHPUnit/Framework.php';

require_once 'ArrayTest.php';
require_once 'SimpleTestListener.php';

// Create a test suite that contains the tests
// from the ArrayTest class.
$suite = new PHPUnit_Framework_TestSuite('ArrayTest');

// Create a test result and attach a SimpleTestListener
// object as an observer to it.
$result = new PHPUnit_Framework_TestResult;
$result->addListener(new SimpleTestListener);

// Run the tests.

The code for SimpleTestListener is on the same page.