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

2024-04-26 00:44:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我的行为步骤或多或少是一组变化。 我想给你一些建议。你知道吗

示例:

@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目录中的功能,但是只使用doc字符串就可以了。你知道吗

谢谢!你知道吗


Tags: in示例cookiedefstepwith步骤建议
1条回答
网友
1楼 · 发布于 2024-04-26 00:44:42

这实际上是不可能的,除非可能通过修补行为本身。不过,也许你可以这样做:

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

在这个检查之后,你可以调用另一个步骤,有很多方法可以处理超过这一点的事情,所以我将把这个留给你。你知道吗

相关问题 更多 >