1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
|
@sequential
Feature: Parse data types in step parameters (type transformation)
As a test writer
I want write steps with parameter that should have floating-point data types
So that I can just use the step parameter with the correct type
behave uses the parse module (inverse of Python string.format).
Therefore, the following ``parse types`` for floats are already supported:
===== =========================================== ============= ================================
Type Characters Matched (regex class) Output Type Example(s)
===== =========================================== ============= ================================
% Percentage (converted to value/100.0) float 51.2%
f Fixed-point numbers float 1.23 -1.45
e Floating-point numbers with exponent float 1.1e-10 -12.3E+5
g General number format (either d, f or e) float 123 1.23 -1.45E+12
===== =========================================== ============= ================================
SEE ALSO:
* http://pypi.python.org/pypi/parse
* string.format: http://docs.python.org/library/string.html#format-string-syntax
@setup
Scenario: Feature Setup
Given a new working directory
And a file named "features/steps/float_param_steps.py" with:
"""
from behave import then, step
class NotMatched(object): pass
@step('a float param with "{value:g}"')
def step_float_param_with(context, value):
assert type(value) is float
context.value = value
@step('a float param with "{value}"')
def step_float_param_otherwise(context, value):
context.value = NotMatched
@step('a generic float param with "{value:g}"')
def step_generic_float_param_with(context, value):
step_float_param_with(context, value)
@step('a generic float param with "{value}"')
def step_generic_float_param_otherwise(context, value):
step_float_param_otherwise(context, value)
@step('a float with exponent param with "{value:e}"')
def step_float_with_exponent_param_with(context, value):
step_float_param_with(context, value)
@step('a float with exponent param with "{value}"')
def step_float_with_exponent_param_otherwise(context, value):
step_float_param_otherwise(context, value)
@step('a percentage param with "{value:%}"')
def step_percentage_param_with(context, value):
step_float_param_with(context, value)
@step('a percentage param with "{value}"')
def step_percentage_param_otherwise(context, value):
step_float_param_otherwise(context, value)
@step('a fixed-point number param with "{value:f}"')
def step_fixed_point_number_param_with(context, value):
step_float_param_with(context, value)
@step('a fixed-point number param with "{value}"')
def step_fixed_point_number_param_otherwise(context, value):
step_float_param_otherwise(context, value)
@then('the value should be {outcome} as float number')
def step_value_should_be_matched_as_float_number(context, outcome):
expected_type = float
if outcome == "matched":
assert type(context.value) is expected_type, \
"Unexpected type: %s" % type(context.value)
else:
assert context.value is NotMatched
@then('the value should be {outcome} as float number with "{expected:g}"')
def step_value_should_be_matched_as_float_number_with_expected(context, outcome, expected):
step_value_should_be_matched_as_float_number(context, outcome)
assert context.value == expected, \
"FAILED: value(%s) == expected(%s)" % (context.value, expected)
"""
Scenario: Float parameter values with type "%" (percentage)
Given a file named "features/example.float_param.with_percent.feature" with:
"""
Feature: Float parameter values with type "%" (percentage)
Scenario Outline: Good cases
Given a percentage param with "<Value>"
Then the value should be matched as float number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 0% | 0 | Zero |
| 20% | 0.2 | Positive number |
| 120% | 1.2 | Larger than 100% |
| 10.5% | 0.105 | Float number |
| -10% | -0.1 | Negative number |
| +10% | 0.1 | With plus sign |
Scenario Outline: Bad cases (not matched)
Given a percentage param with "<Value>"
Then the value should be <Outcome> as float number
Examples:
| Value | Outcome | Reason |
| 123 | not-matched | Percentage sign is missing |
| 1.23 | not-matched | Float number without percentage sign |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.float_param.with_percent.feature"
Then it should pass with:
"""
9 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Fixed-point parameter values with type "f"
Given a file named "features/example.float_param.with_type_f.feature" with:
"""
Feature: Float parameter values with type "f" (fixed-point number)
Scenario Outline: Good cases
Given a fixed-point number param with "<Value>"
Then the value should be matched as float number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 0.23 | 0.23 | |
| 1.23 | 1.23 | |
| 123.45 | 123.45 | |
| +1.23 | 1.23 | With plus sign |
| -1.23 | -1.23 | Negative float |
Scenario Outline: Bad cases (not matched)
Given a fixed-point number param with "<Value>"
Then the value should be <Outcome> as float number
Examples:
| Value | Outcome | Reason |
| 123 | not-matched | Integer number |
| 1.23E-7 | not-matched | Float number with exponent |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.float_param.with_type_f.feature"
Then it should pass with:
"""
8 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Float with exponent parameter values with type "e"
Given a file named "features/example.float_param.with_type_e.feature" with:
"""
Feature: Float parameter values with type "e" (float with exponents)
Scenario Outline: Good cases
Given a float with exponent param with "<Value>"
Then the value should be matched as float number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 1.0E-10 | 1E-10 | With mantisse, negative exponent |
| 1.23E+5 | 123E3 | Exponent with plus sign |
| 123.0E+3 | 1.23E5 | Exponent with plus sign |
| -1.23E5 | -123E3 | Negative number with mantisse and exponent |
| INF | +INF | Infinity (INF) |
| +INF | INF | |
| -INF | -INF | |
| +inf | INF | Lower-case special names |
Scenario Outline: Bad cases (not matched)
Given a float with exponent param with "<Value>"
Then the value should be <Outcome> as float number
Examples:
| Value | Outcome | Reason |
| 1E10 | not-matched | Without mantissa |
| 1.E10 | not-matched | Short mantissa |
| 123 | not-matched | Integer number |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.float_param.with_type_e.feature"
Then it should pass with:
"""
12 scenarios passed, 0 failed, 0 skipped
"""
Scenario: Generic float parameter values with type "g"
Given a file named "features/example.float_param.with_type_g.feature" with:
"""
Feature: Float parameter values with type "g" (generic float)
Scenario Outline: Good cases
Given a generic float param with "<Value>"
Then the value should be matched as float number with "<Expected Value>"
Examples:
| Value | Expected Value | Case |
| 1 | 1.0 | Integer number format |
| 1E10 | 1.0E10 | Float with exponent and shortened mantissa |
| 1.23E5 | 1.23E5 | Float with exponent and mantissa |
| 1.23e5 | 1.23E5 | Float with exponent and mantissa (lower-case) |
| 1.0E-10 | 1E-10 | With mantisse, negative exponent |
| 1.23E+5 | 123E3 | Exponent with plus sign |
| 123.0E+3 | 1.23E5 | Exponent with plus sign |
| -1.23E5 | -123E3 | Negative number with mantisse and exponent |
Scenario Outline: Bad cases (not matched)
Given a generic float param with "<Value>"
Then the value should be <Outcome> as float number
Examples:
| Value | Outcome | Reason |
| 0b101 | not-matched | Binary number |
| 0o17 | not-matched | Octal number |
| 0x1A | not-matched | Hexadecimal number |
| 1.E10 | not-matched | Short mantissa |
| Alice | not-matched | Not a number |
"""
When I run "behave -f plain features/example.float_param.with_type_g.feature"
Then it should pass with:
"""
13 scenarios passed, 0 failed, 0 skipped
"""
|