对于我的某个库(source),我已将PHPunit配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="PHPReboot Stopwatch Test Suite">
<directory>tests/Phpreboot/Stopwatch</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/Phpreboot/Stopwatch</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./log/codeCoverage" charset="UTF-8"
yui="true" highlight="true"
lowUpperBound="50" highLowerBound="80"/>
<log type="testdox-html" target="./log/testdox.html" />
</logging>
</phpunit>
在上面的source中,phpunit.xml.dist只是复制为phpunit.xml。
我的代码覆盖率报告正在生成,但报告显示0%的代码覆盖率。但是根据测试(检查源代码中的代码),我确定它必须大于0%。
有人可以建议我的配置出错吗?
首次评论后修改
测试用例:https://github.com/phpreboot/stopwatch/blob/master/tests/Phpreboot/Stopwatch/StopWatchTest.php
<?php
namespace Phpunit\Stopwatch;
use Phpreboot\Stopwatch\StopWatch;
use Phpreboot\Stopwatch\Timer;
/**
* Class StopWatchTest
* @package Phpunit\Stopwatch
* @group Phpreboot
* @group Phpreboot_Stopwatch
* @group Phpreboot_Stopwatch_StopWatch
*/
class StopWatchTest extends \PHPUnit_Framework_TestCase
{
/** @var StopWatch $stopWatch */
private $stopWatch;
public function setUp()
{
$this->stopWatch = new StopWatch();
}
public function tearDown()
{
$this->stopWatch = null;
}
/* ******************/
/* Constructor test */
/* ******************/
/**
* @group Phpreboot_Stopwatch_StopWatch_constructor
*/
public function testStopWatchHaveDefaultWatch()
{
/** @var Timer $defaultWatch */
$defaultWatch = $this->stopWatch->getWatch();
$this->assertNotNull($defaultWatch, "No watch available");
$this->assertInstanceOf('Phpreboot\Stopwatch\Timer', $defaultWatch, "Not an instance of Watch");
$name = $defaultWatch->getName();
$this->assertEquals(StopWatch::STOPWATCH_DEFAULT_NAME, $name, "Default name of StopWatch is not set correctly");
}
/* ***************/
/* addWatch Test */
/* ***************/
/**
* @group Phpreboot_Stopwatch_StopWatch_addWatch
*/
public function testWatchCanBeAdded()
{
$this->assertEquals(1, $this->stopWatch->getWatchCount(), "Stopwatch doesn't initialized with default watch.");
$this->stopWatch->addWatch('testWatch');
$this->assertEquals(2, $this->stopWatch->getWatchCount(), "Stopwatch could not be added");
}
/**
* @group Phpreboot_Stopwatch_StopWatch_addWatch
*/
public function testWatchCanNotBeAddedWithDuplicateName()
{
$this->assertEquals(1, $this->stopWatch->getWatchCount(), "Stopwatch doesn't initialized with default watch.");
$this->assertFalse($this->stopWatch->addWatch(StopWatch::STOPWATCH_DEFAULT_NAME), "Watch with default name was duplicated.");
$this->assertEquals(1, $this->stopWatch->getWatchCount(), "Watch with default name was duplicated.");
$this->assertTrue($this->stopWatch->addWatch('testWatch'), "New watch couldn't be added.");
$this->assertEquals(2, $this->stopWatch->getWatchCount(), "New watch couldn't be added.");
$this->assertFalse($this->stopWatch->addWatch('testWatch'), "New watch with duplicate name was added.");
$this->assertEquals(2, $this->stopWatch->getWatchCount(), "New watch with duplicate name was added.");
}
/* ********************/
/* getWatchCount Test */
/* ********************/
/**
* @group Phpreboot_Stopwatch_StopWatch_getWatchCount
*/
public function testWatchCountIsCorrect()
{
$totalWatch = $this->stopWatch->getWatchCount();
$this->assertEquals(1, $totalWatch, "Watch count is not correct");
}
/* ***************/
/* getWatch Test */
/* ***************/
/**
* @group Phpreboot_Stopwatch_StopWatch_getWatch
*/
public function testDefaultWatchCouldBeReturned()
{
$watch = $this->stopWatch->getWatch();
$this->assertInstanceOf('Phpreboot\Stopwatch\Timer', $watch, "Default watch is not an instance of Watch.");
$this->assertEquals(StopWatch::STOPWATCH_DEFAULT_NAME, $watch->getName(), "Name of default was was not correctly set.");
}
/**
* @group Phpreboot_Stopwatch_StopWatch_getWatch
*/
public function testWatchCouldBeReturned()
{
$this->stopWatch->addWatch('newWatch');
$newWatch = $this->stopWatch->getWatch("newWatch");
$this->assertInstanceOf('Phpreboot\Stopwatch\Timer', $newWatch, "New watch is not an instance of Watch.");
}
}
来源:https://github.com/phpreboot/stopwatch/blob/master/src/Phpreboot/Stopwatch/StopWatch.php
/*
* This file is part of the PHPReboot/Stopwatch package.
*
* (c) Kapil Sharma <kapil@phpreboot.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Phpreboot\Stopwatch;
use Phpreboot\Stopwatch\Timer;
class StopWatch
{
const STOPWATCH_DEFAULT_NAME = "default_watch_R@nd0m_n@m3";
private $timers;
/**
* Constructor to create new StopWatch instance with default watch.
*/
public function __construct()
{
$this->timers = array();
$this->addWatch(self::STOPWATCH_DEFAULT_NAME);
}
public function start($name = self::STOPWATCH_DEFAULT_NAME)
{
if (!$this->isWatchExist($name)) {
return false;
}
return $this->getWatch($name)->start();
}
public function pause($name = self::STOPWATCH_DEFAULT_NAME)
{
if (!$this->isWatchExist($name)) {
return false;
}
return $this->getWatch($name)->pause();
}
public function stop($name = self::STOPWATCH_DEFAULT_NAME)
{
if (!$this->isWatchExist($name)) {
return false;
}
return $this->getWatch($name)->stop();
}
public function getTime($name = self::STOPWATCH_DEFAULT_NAME)
{
if (!$this->isWatchExist($name)) {
return -1;
}
return $this->getWatch($name)->getTime();
}
public function isWatchExist($name)
{
return array_key_exists($name, $this->timers);
}
/**
* Add a new watch to the StopWatch.
*
* @param string $name Name of watch to be added.
* @return bool True if watch added successfully, false otherwise.
*/
public function addWatch($name)
{
if (array_key_exists($name, $this->timers)) {
return false;
}
$watch = new Timer($name);
$this->timers[$name] = $watch;
return true;
}
public function addWatches(array $watches)
{
$isWatchAdded = false;
if (empty($watches)) {
return $isWatchAdded;
}
foreach ($watches as $watch) {
$this->addWatch($watch);
$isWatchAdded = true;
}
return $isWatchAdded;
}
/**
* Get a watch by name of watch.
*
* @param string $name Name of watch
* @throws \InvalidArgumentException In case watch with name '$name' does not exist.
* @return Timer A watch instance with name '$name'.
*/
public function getWatch($name = self::STOPWATCH_DEFAULT_NAME)
{
if (!array_key_exists($name, $this->timers)) {
throw new \InvalidArgumentException('Watch ' . $name . ' does not exist.');
}
return $this->timers[$name];
}
public function getWatchCount()
{
return count($this->timers);
}
}
编辑2:XDebug设置
php --info | grep xdebug
/etc/php5/cli/conf.d/20-xdebug.ini,
xdebug
xdebug support => enabled
xdebug.auto_trace => Off => Off
xdebug.cli_color => 0 => 0
xdebug.collect_assignments => Off => Off
xdebug.collect_includes => On => On
xdebug.collect_params => 0 => 0
xdebug.collect_return => Off => Off
xdebug.collect_vars => Off => Off
xdebug.coverage_enable => On => On
xdebug.default_enable => On => On
xdebug.dump.COOKIE => no value => no value
xdebug.dump.ENV => no value => no value
xdebug.dump.FILES => no value => no value
xdebug.dump.GET => no value => no value
xdebug.dump.POST => no value => no value
xdebug.dump.REQUEST => no value => no value
xdebug.dump.SERVER => no value => no value
xdebug.dump.SESSION => no value => no value
xdebug.dump_globals => On => On
xdebug.dump_once => On => On
xdebug.dump_undefined => Off => Off
xdebug.extended_info => On => On
xdebug.file_link_format => no value => no value
xdebug.idekey => no value => no value
xdebug.max_nesting_level => 250 => 250
xdebug.overload_var_dump => On => On
xdebug.profiler_aggregate => Off => Off
xdebug.profiler_append => Off => Off
xdebug.profiler_enable => Off => Off
xdebug.profiler_enable_trigger => Off => Off
xdebug.profiler_output_dir => /tmp => /tmp
xdebug.profiler_output_name => cachegrind.out.%p => cachegrind.out.%p
xdebug.remote_autostart => Off => Off
xdebug.remote_connect_back => On => On
xdebug.remote_cookie_expire_time => 3600 => 3600
xdebug.remote_enable => On => On
xdebug.remote_handler => dbgp => dbgp
xdebug.remote_host => localhost => localhost
xdebug.remote_log => no value => no value
xdebug.remote_mode => req => req
xdebug.remote_port => 9000 => 9000
xdebug.scream => Off => Off
xdebug.show_exception_trace => Off => Off
xdebug.show_local_vars => Off => Off
xdebug.show_mem_delta => Off => Off
xdebug.trace_enable_trigger => Off => Off
xdebug.trace_format => 0 => 0
xdebug.trace_options => 0 => 0
xdebug.trace_output_dir => /tmp => /tmp
xdebug.trace_output_name => trace.%c => trace.%c
xdebug.var_display_max_children => 128 => 128
xdebug.var_display_max_data => 512 => 512
xdebug.var_display_max_depth => 3 => 3
答案 0 :(得分:3)
在xdebug的每个测试的注释中告诉PHPUnit该测试中涉及的代码是什么。
可以在测试代码中使用@covers注释来指定哪个 测试方法想要测试的方法:
/** * @covers BankAccount::getBalance */ public function testBalanceIsInitiallyZero() { $this->assertEquals(0, $this->ba->getBalance()); }
如果提供,则仅指定的代码覆盖率信息 方法将被考虑。
此外,请确保您已安装{{3}},因为代码覆盖率报告可以使用。
答案 1 :(得分:2)
我有类似的问题。在我的情况下,我确信该方法正在测试,因为覆盖率报告显示所有行都是绿色的(意味着测试正在通过它们)但是某些方法的摘要显示为0%,我的代码中唯一奇怪的是深嵌套数组缩进,如下所示:
$array['key']
['foo']
['bar']
['fooz']
['bazz'] = "some_value";
制作单曲之后
$array['key']['foo']['bar']['fooz']['bazz'] = "some_value";
奇怪的是,这是一个奇怪的问题
希望有所帮助
答案 2 :(得分:1)
您应该检查filter-&gt;白名单设置是否包含您要通过代码覆盖率分析的源文件。
答案 3 :(得分:1)
由于这是我找到的第一个答案,因此对于那些遇到类似情况的人,我将保留此评论。我的情况是forceCoversAnnotation="true"
。如果将其从phpunit.xml中删除,代码覆盖范围将按预期工作。
答案 4 :(得分:0)
我想添加所有可能正确配置所有其他原因的可能原因,但是代码覆盖率仅为 0%。当您将所有待测代码调用放入PHPUnit setUp()
和setUpBeforeClass()
方法中时,就会发生这种情况。在实际的测试方法中,您仍然可以检查生成的输出,但这不会影响覆盖范围。
此外,如果有效的PHPUnit测试方法不包含任何断言(并且PHPUnit强制您使用@doesNotPerformAssertions
注释),则也不会为此类测试生成覆盖率。
换句话说,如果您关心代码覆盖率,请使用断言将内容移至PHPUnit测试。