跳到内容

这些示例展示了如何构建从程序输出重写其给定前提条件的故事。

这对于在外部世界发生变化时自动更新给定前提条件很有用。 例如,如果正在模拟的 REST API 服务开始返回不同的数据,则可以在重写模式下运行故事以更新模拟。

执行此重写的命令是

self.current_step.rewrite("argument").to("new output")

请注意,如果存在故事继承层次结构,则只有子故事的给定前提条件将被更新。

代码示例

example1.story

Basic:
  given:
    mock api:
      request: |
        {"greeting": "hello"}
      response: |
        {"greeting": "hi"}
  steps:
    - Call API
example2.story

Overridden response:
  based on: basic
  given:
    mock api:
      response: |
        {"greeting": "bonjour"}
example3.story

Overridden request:
  based on: basic
  given:
    mock api:
      request: |
        {"greeting": "hi there"}
example4.story

Story with variations:
  steps:
  - Call API

  variations:
    French:
      given:
        mock api:
          response: |
            {"greeting": "bonjour"}

    Chinese:
      given:
        mock api:
          request: |
            {"greeting": "Ni hao"}
engine.py

from hitchstory import BaseEngine, GivenDefinition, GivenProperty
from strictyaml import Map, Str

class Engine(BaseEngine):
    given_definition = GivenDefinition(
        mock_api=GivenProperty(
            schema=Map({"request": Str(), "response": Str()}),
            inherit_via=GivenProperty.OVERRIDE,
        ),
    )

    def __init__(self, rewrite=True):
        self._rewrite = rewrite

    def call_api(self):
        if self._rewrite:
            self.given.rewrite("Mock API", "response").to("""{"greeting": "bye"}\n""")

带有代码

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

简单

StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).named("Basic").play()

将输出

RUNNING Basic in /path/to/working/example1.story ... SUCCESS in 0.1 seconds.

文件 example1.story 现在应该包含

Basic:
  given:
    mock api:
      request: |
        {"greeting": "hello"}
      response: |
        {"greeting": "bye"}
  steps:
  - Call API

覆盖的响应

StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).named("Overridden response").play()

将输出

RUNNING Overridden response in /path/to/working/example2.story ... SUCCESS in 0.1 seconds.

文件 example2.story 现在应该包含

Overridden response:
  based on: basic
  given:
    mock api:
      response: |
        {"greeting": "bye"}

覆盖的请求

StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).named("Overridden request").play()

将输出

RUNNING Overridden request in /path/to/working/example3.story ... SUCCESS in 0.1 seconds.

文件 example3.story 现在应该包含

Overridden request:
  based on: basic
  given:
    mock api:
      request: |
        {"greeting": "hi there"}
      response: |
        {"greeting": "bye"}

带有变体的故事

StoryCollection(Path(".").glob("*.story"), Engine(rewrite=True)).named("Story with variations/French").play()

将输出

RUNNING Story with variations/French in /path/to/working/example4.story ... SUCCESS in 0.1 seconds.

文件 example4.story 现在应该包含

Story with variations:
  steps:
  - Call API

  variations:
    French:
      given:
        mock api:
          response: |
            {"greeting": "bye"}

    Chinese:
      given:
        mock api:
          request: |
            {"greeting": "Ni hao"}

可执行规范

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