【问题标题】:Writing Codeception CLI test; want to throw exception but not have it fail the test编写 Codeception CLI 测试;想抛出异常但不让它通过测试
【发布时间】:2014-11-19 14:47:01
【问题描述】:

我分叉了 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'); 更新测试/cli/GenerateSuiteCept.php 时,它失败了,因为我首先在 src/Codeception/Command/GenerateSuite.php 中抛出了异常。

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

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

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

【问题讨论】:

    标签: codeception


    【解决方案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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      • 2019-05-26
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多