[英]PHPUnit code coverage show 0% coverage
對於我的庫( 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>
在上面的源代碼中,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%,我的代碼中唯一奇怪的是像這樣縮進的深層嵌套數組:
$array['key']
['foo']
['bar']
['fooz']
['bazz'] = "some_value";
在使它成為一個像它一樣工作之后
$array['key']['foo']['bar']['fooz']['bazz'] = "some_value";
這是一個奇怪的問題,有一個奇怪的漫游
希望能幫助到你
您應該檢查 filter->whitelist 設置是否確實包含您要按代碼覆蓋率分析的源文件。
我想再添加一個可能的原因,說明一切都配置正確,但代碼覆蓋率僅為0% 。 當您將所有待測代碼調用放在 PHPUnit setUp()
和setUpBeforeClass()
方法中時,就會發生這種情況。 在實際測試方法中,您仍然可以檢查生成的輸出,但不會影響覆蓋率。
此外,如果您的有效 PHPUnit 測試方法不包含任何斷言(並且您被迫使用 PHPUnit 的@doesNotPerformAssertions
注釋),則此類測試也不會生成覆蓋率。
換句話說,如果您關心代碼覆蓋率,請將事情移至帶有斷言的 PHPUnit 測試。
由於這是我找到的第一個答案,因此我將為那些最終遇到類似情況的人留下此評論。 我的情況是forceCoversAnnotation="true"
。 如果您將其從 phpunit.xml 中刪除,代碼覆蓋率將按預期工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.