How to run phpunit from code?
I want to use my local PHPUnit installation (via composer) to run my tests and display it on the screen (like acessing / admin / tests). But the only way to run tests that I found in the documentation was with a command line tool.
Bellow is a 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()
source to share
It might be overkill, but you're ready for a cure: https://github.com/NSinopoli/VisualPHPUnit :)
EDIT Here is a rudimentary PHPUnit usage with 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 class YourTestCase
is a subclass PHPUnit_Framework_TestCase
, which you can read more about how to write on the official site: http://www.phpunit.de/manual/3.2/en/writing-tests-for-phpunit.html
However, I would also recommend grabbing a copy of this book: http://www.amazon.com/Advanced-PHP-Programming-George- Schlossnagle / dp / 0672325616 The author teaches you some great tricks, including autoload tests, etc.
source to share