- behave 提供3中step匹配模式
- 'parse'
- 'cfparse' 基于parse的扩展, 支持cardinality field syntax?
- 're' 支持在step中定义正则表达式
'parse' 是默认的step mathcer, 他被使用最多, 有以下特点
- 上手容易, 易读性好, 好理解
- 支持预定义的数据类型和用户自定义类型
- 可以在自定义数据类型中使用re, 在step_impl中隐藏了re, 可读性好
'cfparse' 是parse的扩展, 设计初衷是替代parse, 它有以下特点
- 继承parse, 支持 the cardinality field syntax
- 自动创建缺少的类型转换函数for fields with cardinality field part
- 基于parse_type
're'有以下特点
- addresses some cases that cannot be solved otherwise (currently)
- is backward compatible to cucumber (uses regular expressions)
- is less ambiguous compared to the “parse” matcher (currently)
定义step matcher的两种方法
- 在environment.py中定义默认的matcher
# -- FILE: features/environment.py from behave import use_step_matcher # -- SELECT DEFAULT STEP MATCHER: Use "re" matcher as default. # use_step_matcher("parse") # use_step_matcher("cfparse") use_step_matcher("re")
- 在step definition文件中切换step matcher, 同样使用use_step_matcher("re")
从切换step matcher行后的所有step_impl都使用你切换的step matcher, 除非你再次切换
正则匹配
# 简单的group捕获, 并赋值给P<test>
# -- SIMPLE GROUP: foo @when(u'I try to match "(?P<foo>foo)"') def step_when_I_try_to_match_foo(context, foo): context.foo = foo # -- SIMPLE GROUP: anything else @when(u'I try to match "(?P<anything>.*)"') def step_when_I_try_to_match_anything_else(context, anything): context.anything = anything
# 可选的的group: (?P<an_>an )?
@when(u'I try to match (?P<an_>an )?optional "(?P<foo>foo)"') def step_when_I_try_to_match_an_optional_foo(context, an_, foo): context.foo = foo context.an_ = an_
# 套嵌的正则
@when(u'I try to match nested "(?P<foo>foo(?P<bar>bar)?)"') def step_when_I_try_to_match_nested_foobar(context, foo, bar): context.foo = foo context.bar = bar