跳至内容

虽然你可以在故事步骤中使用 assert expected == actual 来匹配两个字符串,但如果你使用 strings_match(expected, actual),那么当它失败时

  • 它将显示实际字符串、预期字符串 *以及差异*。
  • 它将引发 Failure 异常并避免将完整的堆栈跟踪污染错误消息。

下面展示了一个示例

代码示例

example.story

Failing story:
  steps:
    - Pass because strings match
    - Fail because strings don't match
engine.py

from hitchstory import BaseEngine, strings_match

class Engine(BaseEngine):
    def pass_because_strings_match(self):
        strings_match("hello", "hello")

    def fail_because_strings_dont_match(self):
        strings_match("hello", "goodbye")

带代码

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

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

将输出

RUNNING Failing story in /path/to/working/example.story ... FAILED in 0.1 seconds.

      steps:
      - Pass because strings match
      - Fail because strings don't match


hitchstory.exceptions.Failure

    Test failed.

ACTUAL:
goodbye

EXPECTED:
hello

DIFF:
- hello+ goodbye

可执行规范

matching-strings.story storytests 自动生成的文档。