Behave 设置表
一个步骤可以有一个与之相关的文本和数据表。我们可以在一个步骤中添加一个数据表。建议将表格数据缩进,并且每一行都必须有相同的列号。
一列数据应该用|符号分开。
带表的特征文件(Login.feature)
该特征文件如下所述
Feature − User Information
Scenario − Check login functionality
   Given Collection of credentials
      | username |password |
      | user1    | pwd1    |
      | user2    | pwd2    |
   Then user should be logged in
一个表可以通过上下文变量(在步骤函数中传递)中的 .table 属性被执行的 Python 代码访问。一个表是一个Table的实例。我们可以使用设置表来促进测试的设置。
Python 代码
访问表的Python代码。(login_module.py)如下所示
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)
相应的步骤实现文件(step_implg.py) 。
该文件如下 –
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
项目设置
Python项目中文件的项目设置如下

输出
运行特征文件后得到的输出如下,使用的命令是 behave –no-capture -f plain .

该输出显示了打印出的升序表。
极客教程