为什么Python Behave在我的场景大纲中给出了NotImplementedErrors?

2024-10-02 10:18:21 发布

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

我对Python和自动测试是完全陌生的,因此作为概念证明,我正在测试一个简单的方法,该方法可以将幼童军巢穴名称的某些拼写错误更正为正确的巢穴名称

我的功能文件:

Feature: denConverter method

  Scenario: Invalid Names
     Given the den name xyz
     When the denConverter method is run
     Then denConverter should return None

  Scenario Outline: Valid Names
     Given the den name <initialName>
     When the denConverter method is run
     Then denConverter should return <endName>

Examples: Possible Entries
| initialName   | endName   |
| Wolf          | Wolf      |
| Wolves        | Wolf      |
| Lion          | Lion      |
| Tiger         | Tiger     |
| Bear          | Bear      |
| Webelos       | Webelos   |
| AoL           | AoL       |
| Arrowoflight  | AoL       |
| Arrowoflights | AoL       |
| Bears         | Bear      |

我的步骤文件:

from behave import *
from DenScheduler import denConverter

class DenName:
    def __init__(self):
        input_name = ""
        output_name = ""

@given('the den name "{initialName}"')
def step_impl(context, initialName):
    context.den_name = DenName()
    context.den_name.input_name = initialName
    pass

@given('the den name xyz')
def step_impl(context):
    context.den_name = DenName()
    context.den_name.input_name = "xyz"
    pass

@when('the denConverter method is run')
def step_impl(context):
    context.den_name.output_name = denConverter(context.den_name.input_name)
    assert True is not False

@then('denConverter should return "{endName}"')
def step_impl(context, endName):
    assert context.den_name.output_name == endName

@then('denConverter should return None')
def step_impl(context):
    assert context.den_name.output_name == None

当我运行behave时,场景大纲“有效名称”中的所有测试都会失败,如下所示:

 Scenario Outline: Valid Names -- @1.1 Possible Entries  # features/denConverter.feature:15
    Given the den name Wolf                               # None
    When the denConverter method is run                   # None
    Then denConverter should return Wolf                  # None

@given(u'the den name Wolf')
def step_impl(context):
    raise NotImplementedError(u'STEP: Given the den name Wolf')


@then(u'denConverter should return Wolf')
def step_impl(context):
    raise NotImplementedError(u'STEP: Then denConverter should return Wolf')

我肯定我错过了一些明显的东西,但我已经在这上面呆了好几个小时,试图找出哪里出了问题。在这一点上,我注意到一些示例使用@given(u'given statement'),而其他示例使用@given('given statement'),没有“u”,我不确定“u”是什么意思。我试过用和不用“u”


Tags: thenamenonereturnisdefstepcontext
1条回答
网友
1楼 · 发布于 2024-10-02 10:18:21

解决这个问题有两种选择。 通过更新小黄瓜并用“”括起您的参数,如:

Given the den name "xyz"

或者从decorator中删除相同的内容,如:

@given('the den name {initialName}')

一般来说,小黄瓜步骤名称应该与修饰字符串中的名称匹配。因此,下面的两个问题结合在一起:

@given('the den name "{initialName}"')
Given the den name "xyz"

或以下两项:

@given('the den name {initialName}')
Given the den name xyz

通常,我们在注释字符“”中定义参数,默认情况下,它们在方法中被识别为字符串。对于这类特性的读者来说,理解这些参数的边界在哪里也更为清楚。 如果输入以下数字,则带“”符号的参数可以识别为数字:

Given the den name 100

这在处理一些数字时很有用

相关问题 更多 >

    热门问题