编写Codeception CLI测试;想要抛出异常,但没有让它失败

时间:2014-11-19 14:47:01

标签: codeception

我分叉了Codeception / Codeception并添加了一个抛出异常的Command模块检查。 在测试时,此异常导致测试失败,但我正在运行测试以强制异常和失败,以确保它捕获它。

这是对src / Codeception / Command / GenerateSuite.php

的编辑
    if ($this->containsInvalidCharacters ($suite)) {
        throw new \Exception("Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).");
    }

当我运行命令$I->executeCommand('generate:suite invalid-suite');时更新tests / cli / GenerateSuiteCept.php时,它失败了,因为我首先在src / Codeception / Command / GenerateSuite.php中抛出异常。

这是test / cli / GenerateSuiteCept.php的补充:

$I->executeCommand('generate:suite invalid-dash-suite');

当我运行它时,它说它失败了。但是,我希望它能够通过,因为我故意添加破折号来验证它是否捕获了无效字符。最好的方法是什么?我对这些测试中的try / catch块保持警惕。我不确定这是否是最佳方式。

1 个答案:

答案 0 :(得分:1)

在查看其他Codeception测试之后,我发现这比抛出异常更好:

if ($this->containsInvalidCharacters ($suite)) {
    $output->writeln("<error>Suite name '{$suite}' contains invalid characters. ([A-Za-z0-9_]).</error>");
    return;
}

所以我将测试修改为:

$I->expect ('suite is not created due to dashes');
$I->executeCommand('generate:suite invalid-dash-suite');
$I->seeInShellOutput('contains invalid characters');
相关问题