跳到内容

子故事可以基于父故事。 给定的前提条件将被覆盖

在此示例中,父故事的浏览器类型为 firefox,它被覆盖为 chrome。

如果使用 inherit_via=GivenProperty.OVERRIDE,则子故事中以映射形式出现的部分给定前提条件将覆盖父故事中的给定前提条件。

代码示例

example.story

Login:
  given:
    url: /loginurl
    browser:
      type: firefox
      size: 1024x768
  steps:
  - Fill form:
      username: hello
      password: password
  - Click: login

Log in using chrome:
  based on: login
  given:
    browser:
      type: chrome
engine.py

from hitchstory import BaseEngine, GivenDefinition, GivenProperty
from strictyaml import Map, Int, Str, MapPattern, Optional


class Engine(BaseEngine):
    given_definition = GivenDefinition(
        url=GivenProperty(schema=Str()),
        browser=GivenProperty(
            schema=Map({"type": Str(), "size": Str()}),
            inherit_via=GivenProperty.OVERRIDE,
        ),
    )

    def set_up(self):
        print("use browser {0}".format(self.given["browser"]["type"]))
        print("browser size {0}".format(self.given["browser"]["size"]))
        print("visit {0}".format(self.given['url']))

    def fill_form(self, **textboxes):
        for name, text in sorted(textboxes.items()):
            print("with {0}".format(name))
            print("enter {0}".format(text))

    def click(self, item):
        print("clicked on {0}".format(item))

带代码

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

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

父级

collection.named("Login").play()

将输出

RUNNING Login in /path/to/working/example.story ... use browser firefox
browser size 1024x768
visit /loginurl
with password
enter password
with username
enter hello
clicked on login
SUCCESS in 0.1 seconds.

子级

collection.named("Log in using chrome").play()

将输出

RUNNING Log in using chrome in /path/to/working/example.story ... use browser chrome
browser size 1024x768
visit /loginurl
with password
enter password
with username
enter hello
clicked on login
SUCCESS in 0.1 seconds.

可执行规范

inheritance-given-mapping.story storytests 自动生成的文档。