English 中文(简体)
Behave - Data Types
  • 时间:2024-12-22

Behave - Data Types


Previous Page Next Page  

There are two types of Data Types in Behave, which are Predefined and User-defined. Let us first understand what are the predefined data types.

Pre-defined Data types

Behave utipses the parse module for the parsing parameters in the step definitions. Let us explore some of the parse types that have support for step definitions and do not need be registered pke user-defined data 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 Outpne: 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 is obtained after running the feature file and the command used is behave --no-capture -f plain.

Pre-defined Data types

The continued output is as follows −

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 also has the 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 feature titled payment process is as follows −


Feature − Payment Process
   Scenario Outpne: 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 "{}". 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.

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 behave --no-capture -f plain.

User-defined Data types

The continued output is as follows −

Float   0
 Values

The output shows 75.0 and 85.0 which have been converted to float values (with the help of user-defined conversion). These parameters are passed as the integer types from the feature file.

Advertisements