PHPUnit测试带覆盖的命令行脚本

时间:2017-10-18 19:30:22

标签: php command-line phpunit code-coverage

我正在开发一个使用PHP Class的命令行工具。 我为我的课程做了覆盖测试。

现在我想测试我在命令行中使用的PHP脚本。

我找到了如何使用以下线程触发命令行: How do I test a command-line program with PHPUnit?

我想知道如何覆盖命令行脚本中执行的行。

我试着像以下那样进行测试:

class CommandTest extends \PHPUnit_Framework_TestCase
{
    protected static $command = './src/Command.php ';
    protected static $testWorkingDir = 'tests/Command';

    public function testCLIInstall()
    {
        $command = self::$command . ' --help';
        $output = `$command`;
    }
}

执行成功,但文件'Command.php'中没有任何内容。

首先,有可能吗? 然后,如果是,我该如何处理封面命令行脚本?

非常感谢所有人。

最诚挚的问候,

Neoblaster。

更新:我在GitHub上打开了一个问题:https://github.com/sebastianbergmann/phpunit/issues/2817

1 个答案:

答案 0 :(得分:0)

当你开始测试时,你有一个PHP解释器实例,它正在处理测试的脚本。

您的测试脚本调用命令行,该命令行调用php解释器的第二个实例。正确?

现在你有两个解释器正在运行,它们完全相互分离,并且没有任何通道可以知道其他解释器现在正在做什么。

因此,测试中的xdebug不知道命令脚本中使用了哪些代码行,哪些不是。

我认为最适合您的解决方案是分离您的命令类:

//Command.php
class Command
{
}

和你的命令的索引脚本:

//command_index.php
(new Command($argv))->run();

因此,您可以在测试套件中测试命令的类,并从coverage中排除command_index.php。

相关问题