跳至内容

如果你想故意触发测试失败,默认方法是引发 "Failure" 异常。

这被认为是 "预期异常"。

这意味着故事失败将被突出显示,但不会显示堆栈跟踪。这使得故事失败消息更加清晰。

调试信息可以馈送到异常。

另请参阅

代码示例

engine.py

from hitchstory import BaseEngine, no_stacktrace_for, Failure
from code_that_does_things import raise_example_exception, output, ExampleException

class Engine(BaseEngine):
    def passing_step(self):
        pass

    def failing_step(self):
        raise_example_exception("Towel not located")

    @no_stacktrace_for(ExampleException)
    def failing_step_without_stacktrace(self):
        raise_example_exception("Expected exception")

    def raise_special_failure_exception(self):
        raise Failure("Special failure exception - no stacktrace printed!")

    def step_that_will_not_run(self):
        pass

    def on_failure(self, result):
        pass

    def not_executed_step(self):
        pass
example.story

Failing story:
  steps:
    - Raise special failure exception
    - Step that will not run
    - Step that will not run

带有代码

from hitchstory import StoryCollection
from engine import Engine
from pathlib import Path

story_collection = StoryCollection(Path(".").glob("*.story"), Engine())
story_collection.one().play()

将输出

RUNNING Failing story in /path/to/working/example.story ... FAILED in 0.1 seconds.

    Failing story:
      steps:
      - Raise special failure exception
      - Step that will not run
      - Step that will not run

hitchstory.exceptions.Failure

    Test failed.

Special failure exception - no stacktrace printed!

可执行规范

special-failure-exception.story storytests 自动生成的文档。