Debian Codegraph @

features/step_param.custom_types.feature

raw
1 @sequential
2 @todo
3 Feature: Parse custom data types in step parameters (type transformation)
4
5 As a test writer
6 I want to provide own type parsers (and matcher)
7 to simplify writing step definitions.
8
9 . WORKS ONLY WITH MATCHERS:
10 . * parse
11 . * cfparse (parse with cardinality field extension)
12
13
14 @setup
15 Scenario: Feature Setup
16 Given a new working directory
17 And a file named "features/steps/type_converter_steps.py" with:
18 """
19 from behave import then, step, register_type
20 import parse
21
22 @parse.with_pattern(r"\d+") # -- ONLY FOR: Zero and positive integers.
23 def parse_number(text):
24 return int(text)
25
26 register_type(Number=parse_number)
27 class NotMatched(object): pass
28
29 @step('a param with "Number:{value:Number}"')
30 def step_param_with_number_value(context, value):
31 assert isinstance(value, int)
32 context.value = value
33
34 @step('a param with "{type:w}:{value}"')
35 def step_param_with_unknown_type_value(context, type, value):
36 context.value = NotMatched
37
38 @then('the param should be "{outcome}"')
39 def step_value_should_be_matched_or_not_matched(context, outcome):
40 if outcome == "matched":
41 assert isinstance(context.value, int), \
42 "Unexpected type: %s" % type(context.value)
43 assert context.value is not NotMatched
44 elif outcome == "not matched":
45 assert context.value is NotMatched
46 else:
47 raise ValueError("INVALID: outcome=%s (use: matched, not matched)" % outcome)
48 """
49 And a file named "features/steps/common_steps.py" with:
50 """
51 from behave4cmd0 import passing_steps
52 """
53 And a file named "behave.ini" with:
54 """
55 [behave]
56 show_skipped = false
57 show_timings = false
58 """
59
60
61 Scenario: Use own custom type for numbers
62 Given a file named "features/example.number_type.feature" with:
63 """
64 Feature: Simple Custom Type for Numbers
65
66 Scenario Outline: Numbers
67 Given a param with "Number:<value>"
68 Then the param should be "<outcome>"
69
70 Examples: Good cases for type=Number
71 | value | outcome | Case |
72 | 0 | matched | Zero |
73 | 1 | matched | One |
74 | 10 | matched | Positive number with 2 digits. |
75 | 42 | matched | Another positive number. |
76
77 Examples: Bad cases for type=Number
78 | value | outcome | Case |
79 | +1 | not matched | Positive number with plus sign. |
80 | -1 | not matched | Negative number. |
81 | 1.234 | not matched | BAD: Floating-point number. |
82 | ABC | not matched | BAD: Not a number. |
83 """
84 When I run "behave -f plain features/example.number_type.feature"
85 Then it should pass with:
86 """
87 8 scenarios passed, 0 failed, 0 skipped
88 """
89
90
91 Scenario: Type conversion fails
92 Given a file named "features/steps/bad_type_converter_steps.py" with:
93 """
94 from behave import step, register_type
95 import parse
96
97 @parse.with_pattern(r".*") # -- NOTE: Wildcard pattern, accepts anything.
98 def parse_fails(text):
99 raise ValueError(text)
100
101 register_type(BadType=parse_fails)
102
103 @step('a param with "BadType:{value:BadType}"')
104 def step_param_with_badtype_value(context, value):
105 assert False, "SHOULD_NEVER_COME_HERE: BadType converter raises error."
106 """
107 And a file named "features/example.type_conversion_fails.feature" with:
108 """
109 Feature: Type Conversion Fails
110 Scenario: BadType raises ValueError during type conversion
111 Given a param with "BadType:BAD_VALUE"
112
113 Scenario: Ensure other scenarios are executed
114 Then another step passes
115 """
116 When I run "behave -f plain features/example.type_conversion_fails.feature"
117 Then it should fail with:
118 """
119 1 scenario passed, 1 failed, 0 skipped
120 1 step passed, 1 failed, 0 skipped, 0 undefined
121 """
122 And the command output should contain:
123 """
124 Scenario: BadType raises ValueError during type conversion
125 Given a param with "BadType:BAD_VALUE" ... failed
126 Traceback (most recent call last):
127 """
128 And the command output should contain:
129 """
130 File "features/steps/bad_type_converter_steps.py", line 6, in parse_fails
131 raise ValueError(text)
132 """
133 And the command output should contain "ValueError: BAD_VALUE"
134 And the command output should contain "StepParseError: BAD_VALUE"