自动化测试之行为驱动

行为驱动

行为驱动开发,英文全称是Behavior Driven Development,英文缩写为BDD。lettuce是实现BDD开发模式的一种测试框架,实现了使用自然语言来执行相关联测试的代码的需求。lettuce是基于Cucumber的一款非常易于使用的BDD工具,可以执行纯文本的功能描述。
环境准备:
在py -2交互环境下安装lettuce模块。pip install lettuce。
第一个英文语言行为驱动测试的例子:
测试逻辑:
(1)从lettuce全局变量命名空间world中取得一个整数。
(2)计算该整数的阶乘。
(3)断言计算结果的正确性。
features
-steps.py(写测试脚本和自然语言的测试步骤进行对应)
-xxxx.feature(写自然语言的测试步骤的)
在cmd中,cd进入features的父目录,执行lettuce
自动化测试之行为驱动_第1张图片

Feature: Compute factorial
  In order to play with Lettuce
  As beginners
  We'll implement factorial

  Scenario: Factorial of 0
    Given I have the number 0
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 1
    Given I have the number 1
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 2
    Given I have the number 2
    When I compute its factorial
    Then I see the number 2

  Scenario: Factorial of 3
    Given I have the number 3
    When I compute its factorial
    Then I see the number 6

steps.py

#encoding=utf-8
from lettuce import *

# 用于计算整数的阶乘函数
def factorial(number):
    number = int(number)
    if (number == 0) or (number == 1):
        return 1
    else:
        return reduce(lambda x, y: x * y, range(1, number + 1))

@step('I have the number (\d+)')
def have_the_number(step, number):
    # 将通过正则表达式匹配的数字存于全局变量world中
    world.number = int(number)

@step('I compute its factorial')
def compute_its_factorial(step):
    # 从全局变量world中取出匹配的数字,
    # 计算其阶乘,并将结果再存回world中
    world.number = factorial(world.number)

@step('I see the number (\d+)')
def check_number(step, expected):
    # 通过正则匹配到预期数字
    expected = int(expected)
    # 断言计算阶乘结果是否等于预期
    assert world.number == expected, "Got %d" %world.number

自动化测试之行为驱动_第2张图片
steps.py用类的方式实现:

#encoding=utf-8
from lettuce import world, steps

def factorial(number):
  number = int(number)
  if (number == 0) or (number == 1):
    return 1
  else:
      return reduce(lambda x, y: x * y, range(1, number + 1))

@steps
class FactorialSteps(object):
    """Methods in exclude or starting with _ will not be considered as step"""

    exclude = ['set_number', 'get_number']

    def __init__(self, environs):
        # 初始全局变量
        self.environs = environs

    def set_number(self, value):
        # 设置全局变量中的number变量的值
        self.environs.number = int(value)

    def get_number(self):
        # 从全局变量中取出number的值
        return self.environs.number

    def _assert_number_is(self, expected, msg="Got %d"):
        number = self.get_number()
        # 断言
        assert number == expected, msg % number

    def have_the_number(self, step, number):
        '''I have the number (\d+)'''
        # 上面的三引号引起的代码必须写,并且必须是三引号引起
        # 表示从场景步骤中获取需要的数据
        # 并将获得数据存到环境变量number中
        self.set_number(number)

    def i_compute_its_factorial(self, step):
        """When I compute its factorial"""
        number = self.get_number()
        # 调用factorial方法进行阶乘结算,
        # 并将结算结果存于全局变量中的number中
        self.set_number(factorial(number))

    def check_number(self, step, expected):
        '''I see the number (\d+)'''
        # 上面的三引号引起的代码必须写,并且必须是三引号引起
        # 表示从场景步骤中获取需要的数据以便断言测试结果
        self._assert_number_is(int(expected))

FactorialSteps(world)

你可能感兴趣的:(自动化测试之行为驱动)