Hugo Mota
Hugo Mota

Reputation: 11567

how to run phpunit from code?

I want to use a local installation of PHPUnit (via composer) to run my tests and display it on screen (acessing /admin/tests for instance). But the only way to run tests I found in the documentation was the command line tool.

Bellow is an hypothetical example of what I'm looking for:

$session = new PHPUnit_TestSession('path/to/folder');
$results = $session->runAll();
echo $results->failuresCount();
// other hipotetical $result->methods...
// maybe $results->dump()

Upvotes: 6

Views: 970

Answers (1)

Lim H.
Lim H.

Reputation: 10050

This may be an overkill but you are in for a treat: https://github.com/NSinopoli/VisualPHPUnit :)

EDIT Here is a rudimentary use of PHPUnit using the TextUI_TestRunner

// make sure you have PHPUnit on your path
require_once "PHPUnit/Framework/TestSuite.php";
require_once "PHPUnit/TextUI/TestRunner.php";

$suite = new PHPUnit_Framework_TestSuite();
$suite->addTestSuite('YourTestCase');

// run the test suite with TextUI_TestRunner
PHPUnit_TextUI_TestRunner::run($suite);

The YourTestCase class is a subclass of PHPUnit_Framework_TestCase, which you can read more on how to write at the official website: http://www.phpunit.de/manual/3.2/en/writing-tests-for-phpunit.html

However, I'd also recommend getting a copy of this book: http://www.amazon.com/Advanced-PHP-Programming-George-Schlossnagle/dp/0672325616 The author teaches you quite a few cool tricks, including autoloading tests, etc.

Upvotes: 6

Related Questions