Behave – Data Types

Behave - Data Types

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.

Behave - Data Types

The continued output is as follows βˆ’

Behave - Data Types

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 βˆ’

Behave - Data Types

The output showsΒ 75.0Β andΒ 85.0 from the feature file.

Next Topic – Click Here

This Post Has One Comment

Leave a Reply