跳到内容

实验性

此功能处于 alpha 阶段。API 可能会在次要版本增量中更改。

带参数的故事用于描述本质上相同的故事,但一个或多个变量可以变化。

一个常见的例子是用户使用浏览器登录的故事,可以使用多种不同的浏览器完成。

参数可以在前提条件和步骤中使用,方法是在参数名称周围添加 (( 括号 ))。

代码示例

example.story

Click magic button:
  with:
    browser:
      name: firefox
      version: 37
  given:
    browser: (( browser ))
  steps:
  - Click on button
  - Save screenshot:
      for browser: (( browser ))

  variations:
    with chrome:
      with:
        browser:
          name: chrome
          version: 153
engine.py

from hitchstory import BaseEngine, GivenDefinition, GivenProperty, validate
from strictyaml import Map, Seq, Int, Str, Optional
from code_that_does_things import *

class Engine(BaseEngine):
    given_definition=GivenDefinition(
        browser=GivenProperty(
            schema=Map({"name": Str(), "version": Int()}),
            inherit_via=GivenProperty.OVERRIDE,
        ),
    )

    def set_up(self):
        print(self.given['browser']['name'])
        print(self.given['browser']['version'])

    def click_on_button(self):
        print("clicked!")

    @validate(for_browser=Map({"name": Str(), "version": Int()}))
    def save_screenshot(self, for_browser):
        print('save screenshot:')
        print("screenshot-{0}-{1}.png".format(
            for_browser['name'],
            for_browser['version']
        ))

使用代码

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

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

默认值

story_collection.named("Click magic button").play()

将输出

RUNNING Click magic button in /path/to/working/example.story ... firefox
37
clicked!
save screenshot:
screenshot-firefox-37.png
SUCCESS in 0.1 seconds.

变体

story_collection.named("Click magic button/with chrome").play()

将输出

RUNNING Click magic button/with chrome in /path/to/working/example.story ... chrome
153
clicked!
save screenshot:
screenshot-chrome-153.png
SUCCESS in 0.1 seconds.

使用代码指定参数

story_collection.with_params(browser={"name": "ie", "version": "6"}).named("Click magic button").play()

将输出

RUNNING Click magic button in /path/to/working/example.story ... ie
6
clicked!
save screenshot:
screenshot-ie-6.png
SUCCESS in 0.1 seconds.

可执行规范

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