英文:
How to produce a 100% Codeception path coverage report?
问题
我正在评估路径覆盖率,并试图了解它的确切工作方式。以下是环境详细信息:
Yii2,php-code-coverage 9.2.26,使用 PHP 8.1.6,Codeception 和 PHPUnit 9.6.8
示例 PHP 函数:
public function checkPaths($a, $b) {
if($a && $b) {
echo " Both set! ". $a . " : ". $b;
}
}
Codeception 测试:
public function testCheckPaths()
{
$model = new checkPath();
$a = NULL; $b = NULL;
expect($model->checkPaths($a, $b));
$a = NULL; $b = 10;
expect($model->checkPaths($a, $b));
$a = 5; $b = NULL;
expect($model->checkPaths($a, $b));
$a = 5; $b = 10;
expect($model->checkPaths($a, $b));
}
它显示了 3/4 路径覆盖率。请告诉我如何在这种情况下实现 100% 的路径覆盖率。
我在测试中尝试了不同的情况组合,但无法实现 100% 的路径覆盖率。
英文:
I am evaluating Paths coverage and trying to understand how exactly it works. Following are environment details:
Yii2, php-code-coverage 9.2.26 using PHP 8.1.6, Codeception and PHPUnit 9.6.8
Sample php function:
public function checkPaths($a, $b) {
if($a && $b) {
echo " Both set! ". $a . " : ". $b;
}
}
Codeception test:
public function testCheckPaths()
{
$model = new checkPath();
$a = NULL; $b = NULL;
expect($model->checkPaths($a, $b));
$a = NULL; $b = 10;
expect($model->checkPaths($a, $b));
$a = 5; $b = NULL;
expect($model->checkPaths($a, $b));
$a = 5; $b = 10;
expect($model->checkPaths($a, $b));
}
Path coverage report showing on click of blue checkPaths
It shows 3 / 4 paths coverage. Please let me know how 100% path coverage can be achieved in this case.
I tried different case combinations in test but not able to achieve 100% path coverage.
答案1
得分: 1
以下是翻译好的部分:
这实际上是一个有趣的情况。虽然令人困惑,但这是正确的行为。
当您运行以下代码时:
$a = NULL;
$b = 10;
if ($a && $b) {
}
根本不会检查$b
,因为$a
会捷径条件。这使得$b
的值变得无关紧要,因此这意味着它与$a
和$b
都为NULL时的测试(和路径)相同,因此这两种情况都意味着相同的路径。
这意味着有四种可能路径中有一种是在逻辑上永远无法达到的。
英文:
This is actually an interesting one. And it is correct behaviour, although confusing.
When you run the code:
$a = NULL;
$b = 10;
if ($a && $b) {
}
There is never a check for $b
done at all, as the $a
shortcuts the condition. It makes the value of $b
irrelevant, and hence this means that it is the same test (and path) as when both $a
and $b
were NULL, and therefore these both mean the same path.
This means that there is one path out of four that you logically can never reach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论