- Behave - Discussion
- Behave - Useful Resources
- Behave - Quick Guide
- Behave - Debugging
- Behave - Hooks
- Behave - Reports
- Behave - Retry Mechanism
- Behave - Exclude Tests
- Behave - Runner Script
- Behave - Step Parameters
- Behave - Step Functions
- Behave - Multi-Methods
- Behave - Optional Part
- Behave - Regular Expressions
- Behave - Step Matchers
- Behave - Enumeration
- Behave - Tags
- Behave - Data Types
- Behave - Background
- Behave - Steps in a Step
- Behave - Setup Table
- Behave - Multiline Text
- Behave - Scenario Outlines
- Behave - Step Parameters
- Behave - Supported Languages
- Behave - First Steps
- Behave - Step Implementations
- Behave - Feature Files
- Behave - Gherkin Keywords
- Behave - Feature Testing Setup
- Behave - Configuration Files
- Behave - Command Line
- Behave - Installation
- Behave - Introduction
- Behave - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
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 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
Output
The output obtained after running the feature file is given below and the command used is behave --no-capture -f plain.
The output shows the step up table printed.
Advertisements