如何在未定义的步骤中获得有用的错误?

时间:2016-12-06 07:29:00

标签: python gherkin python-behave

我的行为步骤或多或少是有变化的群体。 我希望能够提出建议。

示例:

@given('a cookie with sprinkles')
def cookie_with_sprinkles():
   """Given a cookie with sprinkles"""
   ...
@given('a cookie with icing')
   ...
@given('a cookie in wrapping')
   ...

通过测试步骤

Given a cookie with icing

我想表达像

这样的话
Undefined step 'Given a cookie with icing'
Steps available with 'a cookie' are:
  Given a cookie with sprinkles
  Given a cookie with icing
  Given a cookie in wrapping

我希望在某个地方硬编码模式 '一个cookie'和映射到实现cookie步骤的函数。 我想重新使用--steps-catalog中的功能,但只需使用doc字符串即可。

谢谢!

1 个答案:

答案 0 :(得分:0)

除非可能通过修补Behave本身,否则这不可能实现。虽然,也许你可以这样做:

@given('a cookie with {what}')
def cookie_with_something(context, what):
    """defines all my cookie steps"""

    # here we define the available choices
    choices = ['sprinkles', 'icing', 'wrapping']

    # and if our step selected an invalid choice, we throw
    # an exception and list the available choices
    # you may as well just exit the step without raising
    # an exception, up to you
    if what not in choices:

        print("Undefined step 'Given a cookie with %s" % what)
        print("Steps available with 'a cookie' are:")

        for choice in choices:

            print("    Given a cookie with %s" % choice)

        raise AssertionError()

经过这次检查后,您可以拨打另一个步骤,有很多方法可以处理这一点,所以我会把这个留给您。