大爱

Python BDD自动化测试框架初探

2014-08-07

1. 什么是BDD

BDD全称Behavior Driven Development,译作”行为驱动开发”,是基于TDD (Test Driven Development 测试驱动开发)的软件开发过程和方法。

BDD可以让项目成员(甚至是不懂编程的)使用自然语言来描述系统功能和场景,从而根据这些描述步骤进行系统自动化的测试。(详见附录4.1)

2. 常用BDD框架介绍

目前常用的BDD测试框架有Ruby中的Cucumber,Python中的Behave、Lettuce及Freshen等。

基本的流程如下图所示(Lettuce官方图):

lettuce

简单来说就是”写用例->跑测试->看结果->写实现->看结果”这样的一个循环。

Behave网站列出了上面提到的几个自动化测试框架的对比(详见附录4.2),基于此原因,本文选择behave来介绍Python BDD自动化测试框架。

3. Behave示例

3.1 Behave安装

1
2
pip install behave # 全新安装
pip install -U behave # 更新

3.2 基础知识

3.2.1 Gherkin语法

上文提到的几种BDD框架均使用Gherkin 语法,这是一种简单易懂的语言,使用关键字来定义系统特征和测试。

使用Gherkin语法编写的feature文件基本结构如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Title (one line describing the story/feature)

As a [role]
I want [feature]
So that [benefit]

Scenario: Title1 (for Behaviour 1)

Given [context or setup]
And [some more context]...
When [event occurs]
Then [expected outcome]
And [another outcome]...

Scenario: Title2 (for Behaviour 2)
...

3.2.2 断言模块

使用BDD测试框架前,需要选择一个好用的断言模块。Python有很多可用的断言模块(下表),本文我们选择hamcrest模块为例。

Matcher Library Description
Native assert Starting point, but not enough information when assert fails.
hamcrest First assertion matcher library, now part of JUnit4. Supports several programming languages: http://code.google.com/p/hamcrest/
nose.tools Part of the nose test framework
should-dsl An interesting small matcher library, http://www.should-dsl.info/
sure Provided by the maker of lettuce, github:/gabrielfalcao/sure
compare http://pypi.python.org/pypi/compare
describe http://pypi.python.org/pypi/describe

3.2.3 目录结构

Behave项目的目录格式如下:

1
2
3
4
5
6
$PROJECT/
+-- features/ -- Contains all feature files.
| +-- steps/
| | +-- step_*.py -- Step definitions for features.
| +-- environment.py -- Environment for global setup...
| +-- tutorial*.feature -- Feature files.

3.3 Behave示例

我们以fibnacci数列计算为例,来了解下behave框架结构及如何使用(网上基本都是以阶乘或WEB页面为例,为显示本文的原创性,我们以fib数列为例)

首先,比较pythonic的实现fib数列的代码如下:

1
2
3
4
5
6
7
# -*- coding:utf-8 -*-
def fibs(num):
a=b=1
for i in range(num):
yield a
a,b=b,a+b
print list(fibs(10))

使用behave的详细步骤如下:

1) 新建目录fib,在此目录下新建文件fib.feature,内容如下

1
2
3
4
5
6
7
8
Feature:Calc Fib
In order to introduce Behave
We calc fib as example

Scenario: Calc fib number
Given we have the number 10
when we calc the fib
then we get the fib number 55

2) 新建目录fib/steps,在此目录下新建文件fib.py,内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from behave import *
from hamcrest import *

def fibs(num):
a=b=1
for i in range(num):
yield a
a,b=b,a+b

@given('we have the number {number}')
def have_number(context,number):
context.fib_number = int(number)

@when('we calc the fib')
def calc_fib(context):
context.fib_number=list(fibs(context.fib_number))[-1]

@then('we get the fib number {number}')
def get_number(context,number):
context.expected_number = int(number)
assert_that(context.fib_number, equal_to(context.expected_number),"Calc fib number: %d" %context.fib_number)

这段Python代码主要分为三部分:

@given部分将输入的number=10转为整形并存入变量中

@when部分调用fib函数,计算fib数列值

@then 部分将计算出的fib值与预期值进行断言比较,判断结果是否相等

3) 切换到fib目录,执行behave命令,结果如下

behave_result

4) Scenario Outlines场景大纲

有时相同的一个Scenario需要在很多不同的参数情况下运行,为了避免写过多重复的Scenario ,我们需要使用Scenario Outlines,如fib.feature文件内容可以修改如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Feature:Calc Fib
In order to introduce Behave
We calc fib as example

Scenario Outline: Calc fib number
Given we have the number <number>
when we calc the fib
then we get the fib number <fib_number>

Examples: Some Numbers
| number | fib_number |
| 1 | 1 |
| 2 | 2 |
| 10 | 55 |

执行结果如下:

behave_result2

5) 更多的beahve示例可以参考附录4.4

4. 附录

作者只对Python BDD自动化测试框架进行了简单了解和学习,没有在实际的项目中进行实践,本文只算是一篇BDD自动化测试的入门。

各位若想更深入了解BDD,可参考如下网页资料:

4.1 BDD详细介绍:https://pythonhosted.org/behave/philosophy.html

4.2 常用测试框架对比:https://pythonhosted.org/behave/comparison.html

4.3 Hamcrest断言模块:https://github.com/hamcrest/PyHamcrest

4.4 Behave示例:http://jenisys.github.io/behave.example/

使用支付宝打赏
使用微信打赏

若你觉得我的文章对你有帮助,欢迎点击上方按钮对我打赏

扫描二维码,分享此文章