2016-12-06 60 views
0

我的行为步骤或多或少都有变化。 我希望能够提出建议。未定义步骤时,如何获得有用的错误?

例如:

@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字符串就没关系。

谢谢!

回答

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() 

在此之后检查,你既可以调用另一个步骤中,有相当多的方法,你可以处理的事情过去,这一点,所以我会离开这个给你。