如何生成100%的Codeception路径覆盖报告?

huangapple go评论49阅读模式
英文:

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));
}

Codeception 报告

路径覆盖报告,点击蓝色 checkPaths

它显示了 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));
}

Codeception report

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.

huangapple
  • 本文由 发表于 2023年5月26日 00:11:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76334331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定