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 项目中文件的项目设置如下所示

Project Setup

输出

运行功能文件后获得的输出如下所示,使用的命令为 **behave --no-capture -f plain**。

Setup Table

输出显示了打印的设置表。

广告
© . All rights reserved.