本人学习的时候基本上是按照behave的tutorial教程一步步学习的,这篇文章就当Behave教程的翻译版吧(*^__^*) 嘻嘻……。

1         安装behave

安装好python后,使用 pip install behave命令安装behave

------ behave的官方网站: http://pythonhosted.org/behave/

2         一个简单的实例

新建下面几个文件,文件结构如下

firstCase

firstCase/wordcheck.feature

firstCase/steps

firstCase/steps/wordcheck.py

wordcheck.feature的内容如下:

Feature: word check
  Scenario: Check letters
    Given I have a letter y
    When I input letter y
   
Then the inputed letter is Equal with y

wordcheck.py内容如下:

__author__ = 'helen'
from behave import * @Given('I have a letter')
def step_impl(context):
    pass @When('I input letter {letter}')
def step_impl(context,letter):
    context.letter = letter @Then('the inputed letter is Equal with {TargetLetter}')
def step_impl(context,TargetLetter):
    assert context.letter is context.TargetLetter

完成这两个文件,我们的第一个case就完成了,下面通过cmd命令来执行它。

第一次运行,结果如下,说没有TargetLetter参数。

检查了wordcheck.py,发现@Then的部份忘记给TargetLetter赋值了,加上红框部份。

第二次运行,执行通过,如下图所示:

就这样,一个简单的实例就完成了,是不是好简单(*^__^*) 嘻嘻……

3         features文件夹

我上面写的例子中的 “firstCase” 文件夹就相当于现在说的features文件夹。

下面简单说一下.feature和.py文件:

1)         .feature文件是用于编写测试场,你可以把各种场景和数据写在里面,支持中文;

2)         .py文件就是根据你写的测试场景和数据来执行测试。

3)         最好.feature文件与.py文件一一对应。

如官网教程所说,简单点的项目文件结构如下:

features/

features/everything.feature

features/steps/

features/steps/steps.py

复杂一点的结构如下:

features/
features/signup.feature
features/login.feature
features/account_details.feature
features/environment.py
features/steps/
features/steps/website.py
features/steps/utils.py

4         .feature文件介绍

一个.feature文件里可以写多个Scenario,如下所示:

Feature: comparison
  Scenario: comparison two words
    Given I have two words
    When input a and a
   
Then two words Equal   Scenario: comparison two numbers
    Given I have two numbers
    When I input number1 3 and number2 2
   
Then the first number big then the last number

下面对这些关键词做一些简单的解析:

1)         Feature:功能名称

2)         Scenario:场景名称

3)         Given:给出测试前提条件;

4)         when:相当我们的测试步骤;

5)         Then:给出期望结果

有一个值得注意的是,你可以把“And”或“But”加到这些步骤中,如下:

Feature: word check
  Scenario: Check letters
    Given I have a letter
    When I input letter y
     
But I just want to show the key word "But"
    Then the inputed letter is Equal with y
     
And continue to next case

但是要注意,你在.py中要写入相应的步骤处理方法,如下图所示,你要加入“When I just want to show the key word "But"”和“Then continue to next case”相对应的方法,

4.1  Scenario Outlines

在测试同一个场景时,很多时候我们需要输入各种各样的数据来验证不同的结果输出,这时我们用Scenario Outlines就可以实现了。如下图分别输大小写字母来验证场景

Feature: word check
  Scenario Outline: Check letters
    Given I have a letter
    When I input letter <keyword>
   
Then the inputed letter is Equal with <targetword>    Examples: Lowercase letters
    |keyword|targetword|
    |
a      |a         |
    |
b      |b         |     Examples: Capital letters
    |keyword|targetword|
    |
F      |F         |
    |
D      |D         |

执行结果如下:

4.2  在场景中添加注释

在场景中,你可以通过"""来输出更多注释信息,这些注释会成为Context的“.text”属性值,如下图分别在Given\When\Then中加入注释:

Feature: word check
  Scenario : show notes
    Given I want to show some notes
    """
    This is Given Notes
    """
    When just want to show some notes
    """
    This is When notes
    """
    Then there is a word "Then" in the attribute ".text"
    """
    This is Then notes
    """

然后在py文件中,我们最后判断"Then"是否存在Context.text中,如下代码:

__author__ = 'helen'
from behave import * @Given('I want to show some notes')
def step_impl(context):
    pass @When('just want to show some notes')
def step_impl(context):
    pass @Then('there is a word "Then" in the attribute ".text"')
def step_impl(context):
    assert context.text.find("Then")

最后执行通过,执行结果如下,在结果中输出了注释:

4.3  使用table

跟上面的context.text一样,在场景中可以一个表格作为context.table属性值,如下图在Given中加入表格:

Feature: show the table
  Scenario: some scenario
  Given a set of specific users
     | name      | department  |
     |
Barry     | Beer Cans   |
     |
Pudey     | Silly Walks |
     |
Two-Lumps | Silly Walks |  When we count the number of people in each department
 Then we will find two people in "Silly Walks"
  But we will find one person in "Beer Cans"

6         Environment.py

Environment.py是个非常重要的文件,放在feature文件夹下,与.feature文件并列。下面是Environment.py中定义的一些方法:

before_step(context, step), after_step(context, step)

These run before and after every step.

before_scenario(context, scenario), after_scenario(context, scenario)

These run before and after each scenario is run.

before_feature(context, feature), after_feature(context, feature)

These run before and after each feature file is exercised.

before_tag(context, tag), after_tag(context, tag)

These run before and after a section tagged with the given name. They are invoked for each tag encountered in the order they’re found in the feature file. See controlling things with tags.

before_all(context), after_all(context)

These run before and after the whole shooting match.

下面是一个简单的例子,大家可以根据自己的需要定义方法:

# coding:utf-8
__author__ = 'helen'
import sys
from behave import *
from selenium import webdriver # 开始测试前,定义系统编码为utf-8
def before_all(context):
    reload(sys)
    sys.setdefaultencoding('utf-8') def before_feature(context):
    context.driver = webdriver.Firefox() def after_feature(context):
    context.driver.quit()

7         通过标签tag来控制测试执行

标签以@开头,如下示例:

Feature: find a look
  @valid
  Scenario: look up a book
   Given I search for a valid book
    Then the result page will include "success"   @invalid
  Scenario: look up an invalid book
    Given I search for a invalid book
     Then the result page will include "failure"

执行的时候在behave 后面加上tag 标签即可,如我只测试“valid”这个场景,那么就输入“behave --tags=valid”,执行如下图所示,一个场景跳过忽略:

如果你想执行若干个不同标签的场景,你可以这么写“behave --tags=valid,invalid”;

如果你想执行除了@invalid外的所有场景,你可以这么写“behave --tags=-invalid”;

如果你要执行标签包含了 “valid”和“invalid”两个签标的场景,你可以这么写“behave --tags=valid --tags=invalid”

当然,tags在py文件中也起作用,例如

def before_feature(context):
    if 'browser' in feature.tags:
        context.driver = webdriver.Firefox() def after_feature(context):
    if 'browser' in feature.tags:
        context.driver.quit()

BDD框架:behave学习记录的更多相关文章

  1. Java爬虫框架Jsoup学习记录

    Jsoup的作用 当你想获得某网页的内容,可以使用此框架做个爬虫程序,爬某图片网站的图片(先获得图片地址,之后再借助其他工具下载图片)或者是小说网站的小说内容 我使用Jsoup写出的一款小说下载器,小 ...

  2. easyUI框架之学习记录汇总

    在添加完之后,可以使用 $.parser.parse();这个方法进行处理:(1) 对整个页面重新渲染: $.parser.parse(); (2) 渲染某个特定的objectvar targetOb ...

  3. 4.VUE前端框架学习记录四:Vue组件化编码2

    VUE前端框架学习记录四:Vue组件化编码2文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...

  4. 3.VUE前端框架学习记录三:Vue组件化编码1

    VUE前端框架学习记录三:Vue组件化编码1文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...

  5. 2.VUE前端框架学习记录二

    VUE前端框架学习记录二:Vue核心基础2(完结)文字信息没办法描述清楚,主要看编码实战里面,有附带有一个完整可用的Html页面,有需要的同学到脑图里面自取.脑图地址http://naotu.baid ...

  6. 1.VUE前端框架学习记录一

    VUE前端框架学习记录一文字信息没办法描述清楚,主要看编码实战里面,有附带有一个完整可用的Html页面,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/file/f0 ...

  7. Quartz 学习记录1

    原因 公司有一些批量定时任务可能需要在夜间执行,用的是quartz和spring batch两个框架.quartz是个定时任务框架,spring batch是个批处理框架. 虽然我自己的小玩意儿平时不 ...

  8. Java 静态内部类与非静态内部类 学习记录.

    目的 为什么会有这篇文章呢,是因为我在学习各种框架的时候发现很多框架都用到了这些内部类的小技巧,虽然我平时写代码的时候基本不用,但是看别人代码的话至少要了解基本知识吧,另外到底内部类应该应用在哪些场合 ...

  9. UWP学习记录3-设计和UI之样式

    UWP学习记录3-设计和UI之样式 1.颜色 在系统的“设置”>“个性化”>“颜色”里,提供了主题色选择.选定主题色后,会根据颜色亮度的 HSB 值创建浅色和深色的主题色. 应用可使用阴影 ...

随机推荐

  1. 解决word启动时报找不到mathpage.wll错误

    按下面的网址进行操作即可: http://www.mathtype.cn/wenti/word-jianrong.html

  2. Tsinsen-A1491 家族【并查集】

    问题描述 阿狸和桃子养了n个小阿狸, 小阿狸们每天都在一起玩的很开心. 作为工程师的阿狸在对小阿狸们之间的关系进行研究以后发现了小阿狸的人际关系由某种神奇的相互作用决定, 阿狸称之为“键”. 每个键有 ...

  3. Java Spring MVC项目搭建(二)——项目配置

    1.站点配置文件web.xml 每一个Spring MVC 项目都必须有一个站点配置文件web.xml,他的主要功能吗....有一位大哥已经整理的很好,我借来了,大家看看: 引用博客地址: http: ...

  4. iOS 之 系统机制

    iOS 沙盒 iOS 8 之 新特性 iOS 操作系统整体架构层次讲解

  5. UVa 706 & ZOJ 1146 LC-Display

    题目大意:给你一个数字n和字体大小s,输出数字的液晶显示.直接模拟,代码如下: #include <stdio.h> void draw(int n,int s,int row) { in ...

  6. arcgis 瓦片图加载规则(转载)

    arcgis 瓦片图加载规则 最近需要做地图离线的功能,要能下载指定区域的瓦片图,我们都知道如何加载谷歌和天地图的加载规则,但是网上貌似没有找到如何加载arcgis自己发布的瓦片图规则,好不容易找到一 ...

  7. OSG开发概览(转载)

    OSG开发概览 1 OSG基础知识 Ø OSG是Open Scene Graphic 的缩写,OSG于1997年诞生于以为滑翔机爱好者之手,Don burns  为了对滑翔机的飞行进行模拟,对open ...

  8. 浅谈JavaWEB入门必备知识之Servlet入门案例详解

    工欲善其事.必先利其器,想要成为JavaWEB高手那么你不知道servlet是一个什么玩意的话,那就肯定没法玩下去,那么servlet究竟是个什么玩意?下面,仅此个人观点并通过一个小小的案例来为大家详 ...

  9. Unity3D在NGUI中使用mask

    过程是这样的:最近一直想做一个头像的mask效果,后来发现原来unity的mask需要用shader来写,网上找了不少资料,也能实现,不过大多数都是用render texture作为相机投影的text ...

  10. jQuery trigger one用法

    jQuery trigger one用法: <%@ page language="java" import="java.util.*" pageEncod ...