跳到内容

对于您不希望在错误消息中输出整个堆栈跟踪的常见和预期异常,您可以将“@no_stacktrace_for”装饰器应用于步骤。

另请参阅

代码示例

example.story

Failing story:
  steps:
    - Failing step without stacktrace
engine.py

from hitchstory import BaseEngine, no_stacktrace_for, Failure, strings_match
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

带代码

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:
      - Failing step without stacktrace


code_that_does_things.ExampleException

    This is a demonstration exception docstring.

    It spreads across multiple lines.

Expected exception

可执行规范

expected-exceptions.story storytests 自动生成的文档。