行为驱动
行为驱动开发,英文全称是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
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 &