与其他一些集成测试框架不同,HitchStory 可以严格验证所有测试,并在运行测试之前,如果它们不符合模式,则会失败。
如果没有验证器,所有步骤/给定数据将被解析为 dict、list 和 string,如这里所示.
为了限制允许的 YAML 类型,或者将字符串解析为其他类型(例如整数),可以在步骤方法上使用 **验证器** 装饰器,并在 GivenProperty 对象上使用 **模式** 参数。
验证器装饰器和模式参数接受所有有效的 StrictYAML 验证器.
代码示例
example.story
Create files:
given:
x: 1
steps:
- Add product:
name: Towel
versions:
- 2.6
- 2.3.4
quantity: 2
options:
tagline: Hoopy
nametag: Ford Prefect
- Put back items: 1
- Add generic product:
name: Towel
versions:
- 2.6
- 2.3.4
quantity: 2
options:
tagline: Hoopy
nametag: Ford Prefect
from hitchstory import BaseEngine, validate, GivenDefinition, GivenProperty
from strictyaml import Seq, Str, Int, Map
from code_that_does_things import append
class Engine(BaseEngine):
given_definition = GivenDefinition(
x=GivenProperty(schema=Int()),
)
def set_up(self):
pass
@validate(
versions=Seq(Str()),
quantity=Int(),
options=Map({
'tagline': Str(), 'nametag': Str()
}),
)
def add_product(
self,
quantity,
name=None,
versions=None,
options=None
):
assert type(quantity) is int, "quantity is of type {0}".format(type(quantity))
assert type(versions[0]) is str
assert type(options['tagline']) is str
append(options['nametag'])
@validate(number_of_items=Int())
def put_back_items(self, number_of_items):
assert type(number_of_items) is int
append("Items put back: " + str(number_of_items))
@validate(kwargs=Map({
"name": Str(),
"quantity": Int(),
"versions": Seq(Str()),
"options": Map({
'tagline': Str(), 'nametag': Str()
})
}))
def add_generic_product(self, **kwargs):
assert type(kwargs['quantity']) is int, "quantity is of type {0}".format(type(kwargs['quantity']))
assert type(kwargs['versions'][0]) is str
assert type(kwargs['options']['tagline']) is str
def tear_down(self):
pass
使用代码
from hitchstory import StoryCollection
from pathlib import Path
from engine import Engine
StoryCollection(Path(".").glob("*.story"), Engine()).ordered_by_name().play()
将输出
RUNNING Create files in /path/to/working/example.story ... SUCCESS in 0.1 seconds.
代码将输出
Ford Prefect
Items put back: 1
可执行规范
从 strong-typing.story storytests 自动生成的文档。