Behave – Setup Table

Behave - Multiline Text

In this chapter, we will discuss Behave – Setup Table. A step can have a text and data table associated with it. We can add a data table with a step. It is recommended to have the table data indented and it is mandatory to have an equal column number for each line.

A column of data should be separated by the | symbol.

Feature File with Table (Login. feature)

The feature file is as mentioned below โˆ’

Feature โˆ’ User Information
Scenario โˆ’ Check login functionality
   Given Collection of credentials
      | username |password |
      | user1    | pwd1    |
      | user2    | pwd2    |
   Then user should be logged in

A table is accessible to the implementation Python code with the Behave Setup table attribute within the context variable (passed in the step function). A table is an instance of a Table. We can use the setup table to facilitate setting up the test.

Python code

The python code to access the table.(login_module.py) is as follows โˆ’

class Deprt(object):
   def __init__(self, username, ms=None):
      if not ms:
         ms = []
      self.username = username
      self.ms = ms
   def m_addition(self, usernane):
      assert usernane not in self.ms
      self.ms.append(usernane)
class LModel(object):
   def __init__(self):
      self.loginusrs = []f
      self.passwords = {}
   def usr_addition(self, username, password):
      assert username not in self.loginusrs
      if password not in self.passwords:
         self.passwords[password] = Deprt(password)
      self.passwords[password].m_addition(username)

Corresponding Step Implementation File(step_implg.py)

The file is as follows โˆ’

from behave import *
from features.steps.login_module import LModel
@given('Collection of credentials')
def step_impl(context):
   model = getattr(context, "model", None)
   if not model:
      context.model = LModel()
   #iterate rows of table
    for r in context.table:
      context.model.usr_addition(r["username"], password=r["password"])
@then('user should be logged in')
def step_impl(context):
   pass

Project setup

The project set up for the file in Python project is as follows

Behave - Setup Table

Output

The output obtained after running the feature file is given below and the command used is behaving –no-capture -f plain.

Behave - Setup Table

The output shows the step-up table printed.

Next Topic – Click Here

This Post Has One Comment

Leave a Reply