There are two types of Behave Data Types, which are Predefined and User-defined. Let us first understand what are the predefined data types.
Pre-defined Data types
Behave utilizes the parse module for the parsing parameters in the step definitions. Let us explore some of the parse types.
- w (of str type) β Underscore & letters.
- W (of str type) β Underscore & non-letters.
- s (of str type) β Whitespace.
- S (of str type) β Non – Whitespace.
- d (of int type) β Digits.
- D (of str type) β Non – Digits.
- n (of int type) β Numbers having thousands separators.
- % (of float type) β Percentage. (translated to value/100.0)
- f (of float type) β Fixed β point numbers.
- e (of float type) β Floating β point numbers along with exponent.
- g (of float type) β Number format.
- b (of int type) β Numbers in binary.
- (of int type) β Numbers in octal.
- x (of int type) β Numbers in hexadecimal.
- ti (of datetime type) β Time in ISO 8601 date/time format.
- te (of datetime type) β Time in RFC 2822 email data/time format.
- tg (of datetime type) β Time in Global data/time format.
- ta (of datetime type) β Time in US data/time format.
- tc (of datetime type) β ctime() data/time format.
- th (of datetime type) β Time in HTTP log data/time format.
- tt (of time type)
In the step implementation, we shall pass the parameter: data type enclosed in “{}”.
Feature File with % data type
The feature file with % data type is as follows β
Feature β Payment Process Scenario Outline: Credit card transaction Given user is on credit card payment screen When user makes a payment of "<p>" percent of total Examples: Amounts | p | |80% | |90% |
Corresponding Step Implementation File
The file is as follows β
from behave import * @given('user is on credit card payment screen') def credit_card_pay(context): print('User is on credit card payment screen') #passing parameter in % datatype enclosed in {} @when('user makes a payment of "{p:%}" percent of total') def step_impl(context, p): print('Number is: ') print(p)
Output
The output used is behaving –no-capture -f plain.
The continued output is as follows β
The output shows 0.8 and 0.9 which is obtained from the % data type to represent 80% and 90% values passed from the feature file.
User-defined Data types
Behave Data Types also have user-defined data types. The method register_type is used to register a user-defined type that can be parsed for any type conversion at the time of matching the step.
Feature File
The feature file for the feature titled payment process is as follows β
Feature β Payment Process Scenario Outline: Credit card transaction Given user is on credit card payment screen When user makes a payment of "<amount>" of total Examples: Amounts |amount | |75 | |85 |
In the step implementation, we shall pass the parameter: user-defined datatype enclosed in “{}”.
Corresponding Step Implementation File
The file is as follows β
from behave import * from behave import register_type #convert parsed text to float def parse_percent(t): return float(t) #register user-defined type register_type(Float=parse_percent) @given('user is on credit card payment screen') def credit_card_pay(context): print('User is on credit card payment screen') @when('user makes a payment of "{amount:Float}" of total') def step_impl(context, amount): print('Number is: ') print(amount)
Output
The output is obtained after running the feature file and the command used is behaving –no-capture -f plain.
The continued output is as follows β
The output showsΒ 75.0Β andΒ 85.0 from the feature file.
Next Topic – Click Here
Pingback: Behave - Background - Adglob Infosystem Pvt Ltd