Files

25 lines
813 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""用例数据读取:fixtures/*.yml → namedtuple 行对象。
用例数据与代码分离,读取后按字段名访问,写错字段名立刻 AttributeError:
rows = load('login') # fixtures/login.yml
rows[0].username / rows[0].result
"""
from collections import namedtuple
import yaml
from core.path import get_path
def load(name):
"""读取 fixtures/<name>.yml,返回 namedtuple 行对象列表。"""
path = get_path('fixtures', '{0}.yml'.format(name))
with open(path, encoding='utf-8') as f:
doc = yaml.safe_load(f)
rows = doc.get('cases', doc) if isinstance(doc, dict) else doc
if not rows:
return []
Row = namedtuple('Row', list(rows[0].keys()))
return [Row(*[r.get(k) for k in Row._fields]) for r in rows]