跳至内容
My story:
  ...
  variations:
    Substory:
    ...

有些故事非常相似,只是改变了几个项目。你可以在同一个故事中创建子故事,以便枚举特定故事下所有可能的 precondition 和步骤排列。

变体只是在同一个 YAML 文件中定义的继承故事。

一个故事可以有任意多个变体,但变体不能有变体。

代码示例

example.story

Create files:
  given:
    content: dog
    hierarchical content:
      x: 1
      y:
        - 42
  steps:
    - Do thing with precondition
    - Do other thing: dog
    - Do yet another thing
    - Do a fourth thing:
        animals:
          pond animal: frog
  variations:
    cat:
      about: create a cat file
      given:
        content: cat

带有代码

from hitchstory import StoryCollection, BaseEngine, GivenDefinition, GivenProperty, validate
from strictyaml import Map, Seq, Int, Str, Optional
from pathlib import Path
from ensure import Ensure
from path import Path


class Engine(BaseEngine):
    given_definition=GivenDefinition(
        content=GivenProperty(schema=Str()),
        hierarchical_content=GivenProperty(
            schema=Map({"x": Int(), "y": Seq(Str())}),
            inherit_via=GivenProperty.OVERRIDE,
        ),
    )

    def do_other_thing(self, parameter):
        assert type(parameter) is str
        print(parameter)

    def do_thing_with_precondition(self):
        assert type(self.given['content']) is str
        print(self.given['content'])

    def do_yet_another_thing(self):
        assert type(self.given['hierarchical_content']['y'][0]) is str
        print(self.given['hierarchical_content']['y'][0])

    @validate(animals=Map({"pond animal": Str()}))
    def do_a_fourth_thing(self, animals=None):
        assert type(animals['pond animal']) is str
        print(animals['pond animal'])

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

播放

story_collection.shortcut("cat").play().report()

将输出

RUNNING Create files/cat in /path/to/working/example.story ... cat
dog
42
frog
SUCCESS in 0.1 seconds.

可以从集合中选择非变体

Ensure([
    story.name for story in story_collection.non_variations().ordered_by_name()
]).equals(
    ["Create files", ]
)

可以从故事对象直接获取变体

Ensure([
    story.name for story in story_collection.named("Create files").variations
]).equals(
    ["Create files/cat"],
)

也可以仅选择子故事

Ensure([
    story.name for story in story_collection.only_uninherited().ordered_by_name()
]).equals(
    ["Create files/cat"],
)

生成文档

docstory.yml

story: |
  # {{ name }}

  {% for variation in variations %}
  {{ variation.documentation() }}
  {% endfor %}
given:
  content: '{{ content }}'
  hierarchical_content: '{{ hierarchical_content["x"] }}'
variation: |
  ## {{ name }}
  ## {{ full_name }}

  {{ about }}

  {% for name, property in given.child.items() %}
  {{ property.documentation() }}
  {% endfor %}
print(
    story_collection.with_documentation(
        Path("docstory.yml").read_text()
    ).named("Create files").documentation()
)

将输出

# Create files


## cat
## Create files/cat

create a cat file


cat

可执行规范

variations.story storytests 自动生成的文档。