注意要切换到test目录下面执行
用pytyhon2.7.15版本
1、TDD:测试驱动开发(Test-Driven Development)
TDD的原理是在开发功能代码之前,先编写单元测试代码,先编写单元测试用例代码,测试代码需要确定编写什么产品代码。TDD首先考虑使用需求(对象,功能,过程,接口等),主要编写测试用例框架对功能的过程和接口进行设计,测试框架可以持续验证。
2、ATDD:验收测试驱动开发(Acceptance Test Driven Development)
在准备实施一个功能之前,需要定义期望的质量和验收明细,以明确且达成共识的验收计划,以此来驱动开发人员的功能开发实现和测试人员的测试脚本开发。
3、BDD:行为驱动开发(Behavior Driven Development)
行为驱动开发是一种敏杰开发技术,由开发者,QA,非技术人员,商业者参与,从需求出发,强调行为
4、python中生菜Lettuce是其开发框架,主要由五点:自然语言描述行为,python定义步骤,运行观看失败,编写代码使其通过,运行观看通过。
安装 pip2 install lettuce
特别注意,有py3存在时,py3不要装lettuce,要不要会产生错误
出现找不到features则表明已经装好
具体步骤,以计算阶乘为例子。
描述行为:zero.feature
# Created by Administrator at 2018/8/17 0017
Feature: Computer 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
语法图:
描述步骤:setp.py,首先是运行观察失败。
# coding=utf-8
"""
自然语言进行测试
"""
from lettuce import *
@step('I have the number (\d+)')
@step('I compute its factorial')
@step('I see the number (\d+)')
其中steps里面都是使用@step这个注解来表示你要做的行为对应的代码实现,
编写代码使其通过
# coding=utf-8
"""
自然语言进行测试
"""
from lettuce import *
@step('I have the number (\d+)')
def have_the_number(step, number):
world.number = int(number)
@step('I compute its factorial')
def compute_its_factorial(step):
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
def factorial(number):
number = int(number)
if (number == 0) or (number == 1):
return 1
else:
return number*factorial(number-1)
观察代码通过