擅长:python、mysql、java
<p>您可以定义一个修饰符,为您“解包”上下文并将“unpacked”值作为参数传递:</p>
<p><strong>环境.py</strong></p>
<pre><code>def before_feature(context, feature):
context.spam = 'spam'
def after_feature(context, feature):
del context.spam
</code></pre>
<p><strong>测试功能</strong></p>
^{pr2}$
<p><strong>步骤.py</strong></p>
<pre><code>def add_context_attrs(func):
@functools.wraps(func) # wrap it neatly
def wrapper(context, *args, **kwargs): # accept arbitrary args/kwargs
kwargs['spam'] = context.spam # unpack 'spam' and add it to the kwargs
return func(context, *args, **kwargs) # call the wrapped function
return wrapper
@step('spam should be "{val}"')
@add_context_attrs
def assert_spam(context, val, spam):
assert spam == val
</code></pre>