我的方法是结合@covers http://www.phpunit.de/manual/current/en/appendixes.annotations.html#appendixes.annotations.covers标签和一个自定义结果打印机.
无论如何,您应该使用 @covers 标签来生成更有意义的代码覆盖率,特别是在更大的测试套件中,确保只有那些测试是很重要的supposed测试一个方法是否真正为其生成覆盖率。
我知道您的问题与覆盖范围无关,但我们稍后会讨论这个问题。也许仅使用该注释就足够了,因为无论您是否运行所有集成测试等,没有专用测试的每种方法都将显示 0% 覆盖率。
自定义结果监听器,用于收集您想要的信息
实现肯定可以调整,我只是想制作一些可以很好地展示概念的东西,并希望给你一些你可以适应的东西。
该代码是 alpha,因为我仅为该问题编写了它,但它适用于当前的 phpunit,我想我粘贴了您需要的所有内容。
结果:
--- myClass ---
-- myClass::a --
ok - myClassTest::testAone
fail - myClassTest::testAtwoFails
-- myClass::b --
ok - myClassTest::testB
-- myClass::untested --
!! Method untested !!
我希望这符合您想要的输出。可以在下面的代码中轻松更改格式。
它会为您要测试的每一门课程打印这条信息(空的就足够了!)
如果测试 @covers 多种方法,它将显示其中的每一种方法
被测类
<?php
class myClass {
public function a() {
return 1;
}
public function b() {
return 2;
}
public function untested() {
return 3;
}
}
测试用例
<?php
require_once("myClass.php");
class myClassTest extends PHPUnit_Framework_TestCase {
/**
* @covers a
*/
public function testAone() {
$sut = new myClass();
$this->assertSame(1, $sut->a());
}
/**
* @covers a
*/
public function testAtwoFails() {
$sut = new myClass();
$this->assertSame("error", $sut->a());
}
/**
* @covers b
*/
public function testB() {
$sut = new myClass();
$this->assertSame(2, $sut->b());
}
}
你需要在phpunit.xml中注册测试监听器!
<phpunit>
<listeners>
<listener class="ResultPrinterListener" file="./ResultPrinterListener.php"></listener>
</listeners>
</phpunit>
结果打印机
<?php
class ResultPrinterListener implements PHPUnit_Framework_TestListener {
protected $suites = array();
public function addError(PHPUnit_Framework_Test $test, Exception $e, $time) {}
public function addFailure(PHPUnit_Framework_Test $test, PHPUnit_Framework_AssertionFailedError $e, $time) {}
public function addIncompleteTest(PHPUnit_Framework_Test $test, Exception $e, $time) {}
public function addSkippedTest(PHPUnit_Framework_Test $test, Exception $e, $time) {}
public function startTest(PHPUnit_Framework_Test $test) {}
public function endTest(PHPUnit_Framework_Test $test, $time) {}
public function startTestSuite(PHPUnit_Framework_TestSuite $suite) {}
public function endTestSuite(PHPUnit_Framework_TestSuite $suite) {
$this->suites[] = $suite;
}
public function __destruct() {
$tests = array();
foreach($this->suites as $suite) {
foreach($suite->tests() as $test) {
if(!$test instanceOf PHPUnit_Framework_TestCase) {
continue;
}
$testClass = get_class($test);
$classUnderTest = substr($testClass, 0, -4); // just cutting the "Test" for now
/**
* Create an array structue
* array[ClassUnderTests][methodUnderTest][arrayOfTestMethodsThatTestThatMethod]
* Every method for a class you have at least one test for will show up here for now
*/
if(!isset($tests[$classUnderTest])) {
if(!class_exists($classUnderTest)) {
echo "\nCan't find matching class '$classUnderTest' for test class $testClass!\n";
}
$class = new ReflectionClass($classUnderTest);
foreach($class->getMethods() as $method) {
$tests[$classUnderTest][$method->getName()] = array();
}
}
$annotations = $test->getAnnotations();
if(!isset($annotations["method"]["covers"])) {
continue;
}
foreach($annotations["method"]["covers"] as $functionUnderTest) {
$statusLine = "";
$status = $test->getStatus();
if($status == PHPUnit_Runner_BaseTestRunner::STATUS_FAILURE) {
$statusLine .= "fail - ";
} else if($status == PHPUnit_Runner_BaseTestRunner::STATUS_SKIPPED) {
$statusLine .= "skip - ";
} else if($status == PHPUnit_Runner_BaseTestRunner::STATUS_INCOMPLETE) {
$statusLine .= "inc - ";
} else {
$statusLine .= "ok - ";
}
$statusLine .= $testClass."::".$test->getName();
$tests[$classUnderTest][$functionUnderTest][] = $statusLine;
}
}
}
foreach($tests as $classUnderTest => $methods) {
echo "\n\n --- $classUnderTest --- \n\n";
foreach($methods as $method => $testCaseStrings) {
echo "-- $classUnderTest::$method -- \n";
if($testCaseStrings == array()) {
echo " !! Method untested !!\n";
continue;
}
foreach($testCaseStrings as $testCaseString) {
echo " $testCaseString\n";
}
echo "\n";
}
}
}
}