实验性的
此功能处于 alpha 阶段。API 可能会在次要版本增量中更改。
使用 extra=,你可以使用在模板外部定义的附加函数和变量。
代码示例
example.story
Login:
about: Simple log in.
jiras: AZT-344, AZT-345
with:
username: AzureDiamond
password: hunter2
given:
url: /loginurl
steps:
- Fill form:
username: (( username ))
password: (( password ))
- Click: login
- Drag:
from item: left
to item: right
- Click:
item: right
double: yes
Log in on another url:
about: Alternate log in URL.
jiras: AZT-344, AZT-589
based on: login
given:
url: /alternativeloginurl
Log in as president:
about: For stories that involve Trump.
jiras: AZT-611
based on: login
with:
username: DonaldTrump
password: iamsosmrt
from hitchstory import BaseEngine, GivenDefinition, GivenProperty
from hitchstory import InfoDefinition, InfoProperty, validate
from strictyaml import Map, Int, Str, Bool, Optional, CommaSeparated
class Engine(BaseEngine):
given_definition = GivenDefinition(
url=GivenProperty(schema=Str()),
)
info_definition = InfoDefinition(
jiras=InfoProperty(schema=CommaSeparated(Str())),
)
def set_up(self):
print("visit {0}".format(self.given['url']))
def fill_form(self, **textboxes):
for name, text in sorted(textboxes.items()):
print("with {0}".format(name))
print("enter {0}".format(text))
def drag(self, from_item, to_item):
print(f"drag {from_item} to {to_item}")
@validate(double=Bool())
def click(self, item, double=False):
if double:
print(f"double clicked on {item}")
else:
print(f"clicked on {item}")
{% for story in story_list %}
{{ story.documentation() }}
{% endfor %}
story: |
# {{ name }}
URL : {{ WEBSITE }}/stories/{{ slug }}.html
{{ info.jiras.documentation() }}
{{ about }}
info:
jiras: |
{% for jira in jiras -%}
* {{ jira_url(jira) }}
{% endfor %}
带代码
from hitchstory import StoryCollection
from pathlib import Path
from engine import Engine
from path import Path
import jinja2
jenv = jinja2.Environment(
undefined=jinja2.StrictUndefined, loader=jinja2.BaseLoader
)
story_collection = StoryCollection(
Path(".").glob("*.story"), Engine()
).non_variations()
extra = {
"WEBSITE": "http://www.yourdocumentation.com/",
"jira_url": lambda jira: f"https://yourproject.jira.com/JIRAS/{jira}",
}
print(
jenv.from_string(Path("index.jinja2").read_text()).render(
story_list=story_collection.with_documentation(
Path("document.yaml").read_text(), extra=extra
).ordered_by_file()
)
)
将输出
# Login
URL : http://www.yourdocumentation.com//stories/login.html
* https://yourproject.jira.com/JIRAS/AZT-344
* https://yourproject.jira.com/JIRAS/AZT-345
Simple log in.
# Log in on another url
URL : http://www.yourdocumentation.com//stories/log-in-on-another-url.html
* https://yourproject.jira.com/JIRAS/AZT-344
* https://yourproject.jira.com/JIRAS/AZT-589
Alternate log in URL.
# Log in as president
URL : http://www.yourdocumentation.com//stories/log-in-as-president.html
* https://yourproject.jira.com/JIRAS/AZT-611
For stories that involve Trump.
可执行规范
从 documentation-extra-vars-and-functions.story storytests. 自动生成的文档。