为了加快故事套件的原型设计和开发,在前提条件、参数和步骤参数中指定的 YAML 数据结构无需预先严格定义。
所有未经验证器解析的故事数据都解析为字典、列表或字符串。
尽管如此,仍建议你尽快应用验证器。在此处了解有关此方面的更多信息.
代码示例
example.story
Create files:
given:
files created:
preconditionfile.txt:
some text
steps:
- Create file:
details:
file name: step1.txt
content: some other text
from hitchstory import BaseEngine, GivenDefinition, GivenProperty
class Engine(BaseEngine):
given_definition = GivenDefinition(
files_created=GivenProperty(),
)
def set_up(self):
for filename, contents in self.given['files_created'].items():
with open(filename, 'w') as handle:
handle.write(contents)
def create_file(self, details):
with open(details['file name'], 'w') as handle:
handle.write(details['content'])
带有代码
from hitchstory import StoryCollection
from pathlib import Path
from engine import Engine
StoryCollection(Path(".").glob("*.story"), Engine()).named("Create files").play()
将输出
RUNNING Create files in /path/to/working/example.story ... SUCCESS in 0.1 seconds.
文件 preconditionfile.txt 现在应该包含
some text
文件 step1.txt 现在应该包含
some other text
可执行规范
从 gradual-typing.story storytests 自动生成的文档。