我想使用PHPUnit的本地安装(通过composer)来运行我的测试并在屏幕上显示它(例如acessing/admin/tests).但是,我在文档中找到的运行测试的唯一方法是使用命令行工具.
贝娄是我正在寻找的一个假设的例子:
$session = new PHPUnit_TestSession('path/to/folder');
$results = $session->runAll();
echo $results->failuresCount();
// other hipotetical $result->methods...
// maybe $results->dump()
Run Code Online (Sandbox Code Playgroud)
这可能是一种矫枉过正,但你可以享受一下:https://github.com/NSinopoli/VisualPHPUnit :)
编辑以下是使用TextUI_TestRunner对PHPUnit的初步使用
// 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);
Run Code Online (Sandbox Code Playgroud)
该YourTestCase班是的子类PHPUnit_Framework_TestCase,你可以在官方网站了解更多关于如何写:http://www.phpunit.de/manual/3.2/en/writing-tests-for-phpunit.html
但是,我还建议您获取本书的副本:http://www.amazon.com/Advanced-PHP-Programming-George-Schlossnagle/dp/0672325616作者教您很多很酷的技巧,包括自动加载测试等