Behave 简明教程

Behave - Regular Expressions

让我们对正则表达式的语法进行总体了解 −

Let us have an overall view of the syntax of regular expressions −

  1. Dot (.) − Equivalent to any character.

  2. Caret (^) − Equivalent to beginning of string. (^…)

  3. Dollar Sign ($) − Equivalent to end of string. (…$)

  4. | − Expression x| y, matches x or y.

  5. \ − Escape character.

  6. \. − Matches dot. (.)

  7. \\ − Matches backslash. (\)

  8. […] − Declares a set of characters. ([A-Za-z])

  9. \d − Matches digit. ([0-9])

  10. \D − Matches non-digit.

  11. \s − Matches whitespace character.

  12. \S − Matches non - whitespace character.

  13. \w − Matches alphanumeric.

  14. \W − Matches non-alphanumeric.

  15. (…) − Group a pattern of regular expression.

  16. \number − Matches text of previous group by index. (\1)

  17. (? P<name>…) − Matches pattern and stores it in the name parameter.

  18. (?P=name) − Matches all text which was matched by the previous group name.

  19. (?:…) − Matches a pattern, however cannot capture text.

  20. (?#…​) − Comment (not considered). Narrates details of pattern.

如果一个字符、字符集或组需要重复多次,有必要提供正则表达式的模式基数。

In case a character, character set or group needs to repeat multiple times, it is mandatory to provide the cardinality of the pattern of regular expression.

  1. ? : Pattern having cardinality 0…​ 1:not mandatory(question mark)

  2. - : Pattern having cardinality 0 or more, 0..( asterisk)

  3. + - : Pattern having cardinality 1 or more, 1..(plus)

  4. {n}: Matches a pattern for n repetitions.

  5. {a ,b}: Matches from a to b for a pattern repetitions.

  6. [A-Za-z]+ : Matches multiple alphabetical characters.

功能文件中可能会有包含几乎相似短语的步骤。Behave 具有解析能力。为此使用了 use_step_parser 方法,我们必须将解析器类型作为参数传递给该方法。

There are maybe steps in the feature file having almost the similar phrases. Behave has the parsing ability. The method use_step_parser is used for this and we have to pass the parser type as a parameter to that method.

对于正则表达式匹配器,我们必须传递参数 re。参数 (? P<name>…​) 用来从步骤定义获取参数。

For regular expression matchers, we have to pass the parameter re. The parameter (? P<name>…​) is utilised to obtain parameters from the step definition.

Feature File (almost similar steps)

Feature File (almost similar steps)

类似步骤的功能文件如下所示 −

The feature file for similar steps is as follows −

Feature − Payment Process
Scenario − Check Debit transactions
      Given user is on "debit" screen
   Scenario − Check Credit transactions
      Given user is on "credit" screen

Corresponding Step Implementation File

Corresponding Step Implementation File

步骤实现文件如下所示 −

The step implementation file is as follows −

from behave import *
#define parser type
use_step_matcher("re")
#regular expression parsing
@given('user is on "(?P<payment>.*)" screen')
def step_impl(context, payment):
   print("Screen type: ")
   print(payment)

Output

运行功能文件后获得的输出如下。在此,我们使用了命令 behave --no-capture -f plain

The output obtained after running the feature file is as follows. Here, we have used the command behave --no-capture -f plain.

regular expressions

输出显示了借方和贷方。这两个值在功能文件中以几乎相似的步骤传递。在步骤执行中,我们使用正则表达式解析了两个步骤。

The output shows the debit and credit. These two values have been passed with almost the similar steps in the feature file. In step implementation, we have parsed both the steps with regular expression.