码迷,mamicode.com
首页 > 编程语言 > 详细

Behave + Selenium(Python) ------ (第一篇)

时间:2014-11-22 22:56:59      阅读:519      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

 最近一个项目用了behave来做测试,因为之前没有接触过,所以写下最近的心得总结。 做自动化的人估计对selenium已经不是很陌生了,但是对于Behave工具,估计很少有人听说过。Behave是BDD(Behavior drive development - 行为驱动开发)的框架。这个框架用来做敏捷开发比较多。QA,开发人员,客户和产品经理都可以加入Behave中来。其中behave包括的feature部分,一般来说是客户或者不懂代码的人来写,然后QA根据客户的行为来编写测试代码。

Behave既可以用来测试网页的功能,也可以用来单元测试测试开发者的代码。

准备工作:

1. 下载python,然后在环境变量里面,把c:\Python2.7加到path里面去 --------- 具体参见 http://weixiaolu.iteye.com/blog/1617440

2. 下载pip 工具(在python里面基本上下载工具都用pip)

3. 打开cmd窗口,使用 pip install behave命令安装behave   ------ behave的官方网站: http://pythonhosted.org/behave/

4. 使用pip install selenium命令安装selenium

5. 使用pip install pyhamcrest命令安装hamcrest. 为什么安装hamcrest呢? 我们熟悉在测试里面都要用的assert语句,然而hamcrest API就是可以用来对actual value和expected value进行判断的。

好了基本上准备工作完成,然后我们开始第一个简单的自动化脚本。

 

一、首先新建一个文件夹命名为feature,在这个文件里面再新建example01文件夹,在example01文件里面新建example01.feature文件

 

#../feature/example01/example01.feature Feature:Show off behave Scenario: Show off behave   Given behave install   When I pass 5 to number variables   Then parameter number must be bigger than 4

二、在example01文件夹里面新建steps文件夹,然后在steps文件夹里面新建example01.py文件

#../feature/example01/steps/example01.py

@Given(‘behave install‘)

def step_impl(context):

     pass  

@when(‘I pass {number:d} to number variables‘)

def step_impl(context, number):

     context.number = number    

@Then(‘parameter number must be bigger than 4‘)

def step_impl(context):

  assert 5>4

三、最后一步打开cmd,然后cd到你feature所在的目录,执行behave命令, 结果出来了:

 

bubuko.com,布布扣

问题解决:

1. 如果你遇到以下问题,则是你的代码中出现tab, space混合使用缩进的原因。 请把你的代码统一规范,要么tab缩进,要么space缩进。 一般都使用tab缩进。

bubuko.com,布布扣

 

Behave + Selenium(Python) ------ (第一篇)

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/tman/p/4115795.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!