跳到内容

子故事可以基于父故事。

如果你在子故事中更改了一个先决条件,那么在运行它时,步骤和其他先决条件将保持不变。

在以下示例中,给定的 url 从 /loginurl 更改为 /alternativeloginurl,浏览器保持为 firefox。

代码示例

example.story

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

Log in on alternate url:
  based on: login
  given:
    url: /alternativeloginurl
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=Str()),
    )

    def set_up(self):
        print("use browser {0}".format(self.given["browser"]))
        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
visit /loginurl
with password
enter password
with username
enter hello
clicked on login
SUCCESS in 0.1 seconds.

子级

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

将输出

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

可执行规范

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