跳到内容

默认情况下,每当故事按顺序播放时,它们都会在遇到第一个失败时停止。

但是,如果你的故事需要很长时间才能运行,你可能希望在第一个失败后继续运行。

代码示例

example1.story

A Create file:
  steps:
  - Create file
B Create file:
  steps:
  - Fail
example2.story

C Create file a third time:
  steps:
    - Create file

带有代码

from hitchstory import StoryCollection, BaseEngine
from pathlib import Path


class Engine(BaseEngine):
    def create_file(self, filename="step1.txt", content="example"):
        with open(filename, 'w') as handle:
            handle.write(content)

    def fail(self):
        raise Exception("Error")

停止失败是默认行为

StoryCollection(
    Path(".").glob("*.story"), Engine()
).ordered_by_name().play()

将输出

RUNNING A Create file in /path/to/working/example1.story ... SUCCESS in 0.1 seconds.
RUNNING B Create file in /path/to/working/example1.story ... FAILED in 0.1 seconds.

    B Create file:
      steps:
      - Fail


[1]: function 'fail'
  /path/to/working/examplepythoncode.py


        59 :
        60 :             def fail(self):
    --> 61 :                 raise Exception("Error")
        62 :



builtins.Exception
  Common base class for all non-exit exceptions.
Error

继续执行失败

StoryCollection(
    Path(".").glob("*.story"), Engine()
).ordered_by_name().continue_on_failure().play()

将输出

RUNNING A Create file in /path/to/working/example1.story ... SUCCESS in 0.1 seconds.
RUNNING B Create file in /path/to/working/example1.story ... FAILED in 0.1 seconds.

    B Create file:
      steps:
      - Fail


[1]: function 'fail'
  /path/to/working/examplepythoncode.py


        59 :
        60 :             def fail(self):
    --> 61 :                 raise Exception("Error")
        62 :



builtins.Exception
  Common base class for all non-exit exceptions.
Error
RUNNING C Create file a third time in /path/to/working/example2.story ... SUCCESS in 0.1 seconds.

可执行规范

fail-fast.story storytests 自动生成的文档。