English 中文(简体)
Behave - Setup Table
  • 时间:2024-11-03

Behave - Setup Table


Previous Page Next Page  

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 pne.

A column 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 functionapty
   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 .table attribute within the context variable (passed in the step function). A table is an instance of Table. We can use the set up table to faciptate setting up the test.

Python code

The python code to access 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

Project Setup

Output

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

Setup Table

The output shows the step up table printed.

Advertisements