编写Codeception-CLI测试;想抛出异常,但不想让它通过测试

编写Codeception-CLI测试;想抛出异常,但不想让它通过测试,codeception,Codeception,我分叉了Codeception/Codeception,并添加了一个引发异常的命令模块检查。 在测试时,这个异常会导致测试失败,但我正在运行一个测试来强制异常和失败,以确保它捕获它 下面是对src/Codeception/Command/GenerateSuite.php的编辑 if ($this->containsInvalidCharacters ($suite)) { throw new \Exception("Suite name '{$suite}' co

我分叉了Codeception/Codeception,并添加了一个引发异常的命令模块检查。 在测试时,这个异常会导致测试失败,但我正在运行一个测试来强制异常和失败,以确保它捕获它

下面是对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中抛出异常

下面是对tests/cli/GenerateSuiteCept.php的补充:

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

当我运行它时,它说它失败了。但是,我希望它通过,因为我故意添加破折号来验证它是否捕获了无效字符。最好的方法是什么?我担心在这些测试中会出现try/catch阻塞。我不确定这是否是最好的方法。

在查看其他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');