运行带有示例的生菜场景时,创建的步骤未被识别

1 投票
1 回答
3689 浏览
提问于 2025-04-17 17:29

让我给你说说我有多困惑。

如果我只是用一个包含变量的lettuce特性文件,一切都能正常工作。比如说,如果我创建以下这个特性文件:

Feature: File Finder
    I need to just look for the presence of certain files on a system

Scenario Outline:  Verify the presence (or absence) of a file on a system
    Given I log into a system at "172.16.100.23" as user "cadmin" with password "cadmin"
    Then I can look for "/var/log/nginx.log" and know that it should "not" be there

然后我用lettuce来运行它,它会告诉我需要创建以下步骤:

You can implement step definitions for undefined steps with these snippets:

# -*- coding: utf-8 -*-
from lettuce import step

@step(u'Given I log into a system at "([^"]*)" as user "([^"]*)" with password "([^"]*)"')
def given_i_log_into_a_system_at_group1_as_user_group2_with_password_group2(step, group1, group2, group3):
    assert False, 'This step must be implemented'
@step(u'Then I can look for "([^"]*)" and know that it should "([^"]*)" be there')
def then_i_can_look_for_group1_and_know_that_it_should_group2_be_there(step, group1, group2):
    assert False, 'This step must be implemented'

如果我把那个头部("from lettuce import step")和那些步骤粘贴到filefinder.py文件夹里,并把"assert False"改成"assert True",只是为了让测试通过,我就能顺利通过测试:

Feature: File Finder
  I need to just look for the presence of certain files on a system

Scenario: Verify the presence (or absence) of a file on a system
  Given I log into a system at "172.16.100.23" as user "cadmin" with password "cadmin"
  Then I can look for "/var/log/nginx.log" and know that it should "not" be there

1 feature (1 passed)
1 scenario (1 passed)
2 steps (2 passed)

现在,我想在里面加一个示例表格。我只需把Then I can ask <manager> for <item>作为我的第三步,并添加以下示例表格:

Examples:
    | manager   | item            |
    | "bob"     | "raise"         |
    | "suzy"    | "more switches" |
    | "bill"    | "more coffee"   |

当我用lettuce运行这个时,它告诉我:

You can implement step definitions for undefined steps with these snippets:

# -*- coding: utf-8 -*-
from lettuce import step

@step(u'Then I can ask <manager> for <item>')
def then_i_can_ask_manager_for_item(step):
    assert False, 'This step must be implemented'

所以,我把这个加到我的filefinder.py文件里,并把"assert False"改成"assert True",只是为了让它通过,看看控制台上显示的绿色。如果我用lettuce运行这个,它给我的回应和之前完全一样,似乎不识别占位符<manager><item>作为有效步骤。我想这就是我无法创建它要求的步骤的唯一情况——当我使用这里描述的占位符时:http://lettuce.it/tutorial/scenario-outlines.html。这个例子显示“场景大纲:阶乘 [0-4]”很奇怪,因为我不知道[0-4]是否必要。在我的测试中似乎没有任何影响,尽管我没有成功的示例测试,所以我可能完全错了。

我需要弄清楚为什么lettuce看不到那些带有"<placeholder>"语法的步骤。

有没有人能帮我解答一下这个问题?

1 个回答

4

我发现了这个页面:http://lettuce.it/intro/wtf.html,名字很有意思,叫做wtf!

我需要做的事情是给<placeholders>加上引号,并且在示例表格中去掉引号。

现在,我的特性文件看起来是这样的:

Feature: File Finder
    I need to just look for the presence of certain files on a system

Scenario Outline:  Verify the presence of a file on a system [0-3]
    Given I log into a system at "172.16.100.23" as user "cadmin" with password "cadmin"
    Then I can look for "/var/log/nginx.log" and know that it should "not" be there
    Then I can ask "<manager>" for "<item>"

Examples:
    | manager | item          |
    | bob     | raise         |       
    | suzy    | more switches |
    | bill    | more coffee   |

而且在我的控制台上显示了各种绿色的通过标记:

Feature: File Finder
  I need to just look for the presence of certain files on a system

Scenario Outline: Verify the presence of a file on a system
  Given I log into a system at "172.16.100.23" as user "cadmin" with password "cadmin"
  Then I can look for "/var/log/nginx.log" and know that it should "not" be there
  Then I can ask "<manager>" for "<item>"

Examples:
  | manager | item          |
  | bob     | raise         |
  | suzy    | more switches |
  | bill    | more coffee   |

1 feature (1 passed)
3 scenarios (3 passed)
9 steps (9 passed)

总结一下……"[0-4]"这个部分是不必要的,当使用示例时,确保使用"Scenario Outline"而不是"Scenario"。

撰写回答