跳到内容
Parent story:
  ...

Child story:
  based on: Parent story
  ...

故事继承允许你使用父故事作为子故事的模板,并且

  • 更改给定前提条件。
  • 添加新的步骤。

代码示例

example.story

Login:
  about: Simple log in.
  with:
    username: AzureDiamond
    password: hunter2
  given:
    url: /loginurl
    files:
      a.txt: a
      b.txt: b
  steps:
  - Fill form:
      username: (( username ))
      password: (( password ))
  - Click: login


Log in on another url:
  about: Alternate log in URL.
  based on: login
  given:
    url: /alternativeloginurl

Log in as president:
  about: For stories that involve Trump.
  based on: login
  with:
    username: DonaldTrump
    password: iamsosmrt
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()),
        files=GivenProperty(
            schema=MapPattern(Str(), Str()),
            inherit_via=GivenProperty.REPLACE,
        ),
    )

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

        for filename, content in self.given.get("files", {}).items():
            print("{}: {}".format(filename, content))

    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 engine import Engine
from hitchstory import StoryCollection
from pathlib import Path
from ensure import Ensure

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

原始故事

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

将输出

RUNNING Login in /path/to/working/example.story ... visit /loginurl
a.txt: a
b.txt: b
with password
enter (( password ))
with username
enter (( username ))
clicked on login
SUCCESS in 0.1 seconds.

重写给定

collection.named("Log in on another url").play()

将输出

RUNNING Log in on another url in /path/to/working/example.story ... visit /alternativeloginurl
a.txt: a
b.txt: b
with password
enter (( password ))
with username
enter (( username ))
clicked on login
SUCCESS in 0.1 seconds.

重写参数

collection.named("Log in as president").play()

将输出

RUNNING Log in as president in /path/to/working/example.story ... visit /loginurl
a.txt: a
b.txt: b
with password
enter (( password ))
with username
enter (( username ))
clicked on login
SUCCESS in 0.1 seconds.

只有子项

print('\n'.join([
    story.name for story in collection.only_uninherited().ordered_by_file()
]))

将输出

Log in on another url
Log in as president

可执行规范

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