Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to run phpunit from code?

Tags:

php

phpunit

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()
like image 584
Hugo Mota Avatar asked Feb 03 '13 22:02

Hugo Mota


People also ask

How do I run a single PHPUnit test?

To Run a single PHPUnit Test Case on a method within the file: Open your PHPUnit Test Case file in the editor. Place your cursor on the method you wish to test, right-click and select Run As | PHP Unit Test.

How do I run PHPUnit on Windows?

Open file Explorer and type "C:\windows\system32", then find cmd.exe and right click and select "Run as administrator". Start the Command Prompt as Administrator by clicking Start, All programs and Accessories, then right-click on Command Prompt link and selecting "Run as Administrator" from the context menu.


1 Answers

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.

like image 117
Lim H. Avatar answered Oct 03 '22 23:10

Lim H.