将@符号放入场景中 - 清新

时间:2014-01-21 13:35:48

标签: python escaping tdd cucumber bdd

我正在尝试使用Freshen运行以下方案:

  Scenario Outline: Retrieve a user using some identifier
    Given I have the account number <account_number>
    And I get the user from their <identifier> which is <identifier_value>
    Then the users <field_name> should be <field_value>

  Examples:
   | account_number | identifier | identifier_value        | field_name | field_value     | 
   | 57             | id         | 622                     | Username   | testuser01      |
   | 57             | username   | testuser01              | Email      | argy@bargy.com  |

通过以下步骤:

@Given("I have the account number (\d+)")
def set_account_num(account_number):
    scc.account_number = int(account_number)

@Given("I get the user from their (\w+) which is (\w+)")
def get_user_from_id(identifier, identifier_value):

    scc.user = user_operations.get_user(scc.account_number, str(identifier_value), str(identifier), scc.headers)

@Then("the users (\w+) should be (\w+)")
def check_result(field_name, field_value):
    assert_equal(str(field_value), str(scc.user[str(field_name)]))

我遇到以下失败:

======================================================================
FAIL: user: Retrieve a user using some identifier
----------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\Front_End\features\
teps.py", line 83, in check_result
    assert_equal(str(field_value), str(scc.user[str(field_name)]))
AssertionError: 'argy' != 'argy@bargy.com'

----------------------------------------------------------------------

问题似乎是field_value中的字符串正在被"argy"而不是"argy@bargy.com"读入。有人知道如何包含或转义@符号吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

用于注释步骤的字符串(例如"the users (\w+) should be (\w+)")是正则表达式。他们习惯于将步骤定义与Gherkin文件中的文本进行匹配,并解析文本中的参数。

在这种情况下,(\w+)正在寻找一个或多个&#34;字&#34;字符,不包括@。实际上,@ isn是您唯一的问题,\w也不会与.com中的点匹配。

您有两个选项,要么将(\w+)更改为与电子邮件匹配的正则表达式,要么只在参数周围加上引号,并使用(.*)来匹配这些引号内的任何内容。

使用正则表达式匹配电子邮件地址比您想象的要难(请参阅http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html),所以只需使用引号:

Then the users "<field_name>" should be "<field_value>"

@Then('the users "(.*)" should be "(.*)"')