在你使用behave或其他BDD框架之前, 你应该选择一个断言库。 python有很多这方面的第三方库。例如: hamcrest,nose.tools,  should-dsl, sure, compare, describe等。选择一个自己喜欢的。

  1. 参数化step

    # file:features/tutorial03_step_parameters.feature
    Feature: Step Parameters (tutorial03) Scenario: Blenders
    Given I put "apples" in a blender
    When I switch the blender on
    Then it should transform into "apple juice"
    #每个step中都包含一部分参数,这些参数可以传入不同的值
    from behave import given, when, then
    from hamcrest import assert_that, equal_to
    from blender import Blender @given('I put "{thing}" in a blender')
    def step_given_put_thing_into_blender(context, thing): #参数名字可以随便定义
    context.blender = Blender()
    context.blender.add(thing) @when('I switch the blender on')
    def step_when_switch_blender_on(context):
    context.blender.switch_on() @then('it should transform into "{other_thing}"')
    def step_then_should_transform_into(context, other_thing):
    assert_that(context.blender.result, equal_to(other_thing))
  2. 场景概要(scenario outline)

    Feature: Scenario Outline (tutorial04)
    
      Scenario Outline: Use Blender with <thing>
    Given I put "<thing>" in a blender
    When I switch the blender on
    Then it should transform into "<other thing>" Examples: Amphibians
    | thing | other thing |
    | Red Tree Frog | mush |
    | apples | apple juice | Examples: Consumer Electronics
    | thing | other thing |
    | iPhone | toxic waste |
    | Galaxy Nexus | toxic waste | #这个类型也可以叫做场景模板, template的概念, 相同的场景传入不同的参数, 参数名字是table的第一行, 可以在step中引用
    from behave import given, when, then
    from hamcrest import assert_that, equal_to
    from blender import Blender @given('I put "{thing}" in a blender')
    def step_given_put_thing_into_blender(context, thing):
    context.blender = Blender()
    context.blender.add(thing) @when('I switch the blender on')
    def step_when_switch_blender_on(context):
    context.blender.switch_on() @then('it should transform into "{other_thing}"')
    def step_then_should_transform_into(context, other_thing):
    assert_that(context.blender.result, equal_to(other_thing))
  3. 多行text(step data)

       Scenario: Some scenario
    Given a sample text loaded into the frobulator:
    """
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
    eiusmod tempor incididunt ut labore et dolore magna aliqua.
    """
    #step data在step_impl中可以直接用context.text引用 from behave import given, when, then
    from hamcrest import assert_that, equal_to @given('a sample text loaded into the frobulator')
    def step_impl(context):
    frobulator = getattr(context, "frobulator", None)
    if not frobulator:
    context.frobulator = Frobulator()
    context.frobulator.text = context.text
  4. setup table

    # file:features/tutorial06_step_setup_table.feature
    Feature: Step Setup Table (tutorial06) Scenario: Setup Table
    Given a set of specific users:
    | name | department |
    | Barry | Beer Cans |
    | Pudey | Silly Walks |
    | Two-Lumps | Silly Walks | #步骤中的table,在step_impl中使用context.table引用, 是由dict组成的数组 @given('a set of specific users')
    def step_impl(context):
    model = getattr(context, "model", None)
    if not model:
    context.model = CompanyModel()
    for row in context.table:
    context.model.add_user(row["name"], deparment=row["department"])
  5. Result table 和stepup的用法相似

  6. 在step中使用其他step

    # context.execute_steps函数, 不赘述
    @when('I do the same thing as before')
    def step_impl(context):
    context.execute_steps(u"""
    when I press the big {button_color} button
    and I duck
    """.format(button_color="red"))
  7. background

    # file:features/tutorial09_background.feature
    Feature: Using Background -- Fight or Flight (Natural Language Part2, tutorial09) Background: Ninja fight setup
    Given the ninja encounters another opponent Scenario: Weaker opponent
    Given the ninja has a third level black-belt #Background下面的step会在该feature中所有scenario执行之前执行
  8. 自定义数据类型

    在参数化的step中, 参数默认是按string解析的. 如果用户想按照其他类型处理,可自定义参数类型

      Scenario Outline: Calculator
    Given I have a calculator
    When I add "<x>" and "<y>"
    Then the calculator returns "<sum>" Examples:
    | x | y | sum |
    | 1 | 1 | 2 | #写一个转换函数,并注册
    from behave import register_type def parse_number(text):
    """
    Convert parsed text into a number.
    :param text: Parsed text, called by :py:meth:`parse.Parser.parse()`.
    :return: Number instance (integer), created from parsed text.
    """
    return int(text)
    # -- REGISTER: User-defined type converter (parse_type).
    register_type(Number=parse_number) #step_impl
    @when('I add "{x:Number}" and "{y:Number}"')
    def step_impl(context, x, y):
    assert isinstance(x, int)
    assert isinstance(y, int)
    context.calculator.add2(x, y)
  9. tag

    设置tag来规划自己的测试集合

    select/enable    --tags=@one    Only items with this tag.
    not (tilde/minus) --tags=-@one Only items without this tag.
    logical-or --tags=@one,@two If @one or @two is present.
    logical-and --tags=@one --tags=@two If both @one and @two are present.

Behave 基础的更多相关文章

  1. python 模块基础介绍

    从逻辑上组织代码,将一些有联系,完成特定功能相关的代码组织在一起,这些自我包含并且有组织的代码片段就是模块,将其他模块中属性附加到你的模块的操作叫做导入. 那些一个或多个.py文件组成的代码集合就称为 ...

  2. Lucene基础(2)

    上一篇:Lucene基础(1) 一.Lucene术语 Document, Field, Term, Query, Analyzer相信在其中大多数在之前已经理解了...对其中部分概念详细说明 Docu ...

  3. 自动化测试:behave

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  4. RunLoop总结:RunLoop基础知识

    没有实际应用场景,很难理解一些抽象空洞的东西,所以前面几篇文章先介绍了RunLoop的几个使用场景. 另外AsyncDisplayKit中也有大量使用RunLoop的示例. 关于实际的使用RunLoo ...

  5. Redis(REmote DIctionary Server)基础

    Redis(REmote DIctionary Server)基础 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. Redis是一个开放源代码(BSD许可)的内存数据结构存储,用作数 ...

  6. 【Android开发日记】之基础篇(一)——TextView+SpannableStringBuilder

    TextView是控件中最最基础的一个控件,也是最简单的一个控件.但如果仅此,我不会专门为TextView写一篇文章.最近发现了Android中有趣的一个类,那就是标题上写的SpannableStri ...

  7. Webpack探索【15】--- 基础构建原理详解(模块如何被组建&如何加载)&源码解读

    本文主要说明Webpack模块构建和加载的原理,对构建后的源码进行分析. 一 说明 本文以一个简单的示例,通过对构建好的bundle.js源码进行分析,说明Webpack的基础构建原理. 本文使用的W ...

  8. python 基础网络编程2

    python 基础网络编程2 前一篇讲了socketserver.py中BaseServer类, 下面介绍下TCPServer和UDPServer class TCPServer(BaseServer ...

  9. SWIG 3 中文手册——5. SWIG 基础知识

    目录 5 SWIG 基础知识 5.1 运行 SWIG 5.1.1 输入格式 5.1.2 SWIG 输出 5.1.3 注释 5.1.4 C 预处理器 5.1.5 SWIG 指令 5.1.6 解析限制 5 ...

随机推荐

  1. Eclipse 4.2 failed to start after TEE is installed

    ---------------  VM Arguments---------------  jvm_args: -Dosgi.requiredJavaVersion=1.6 -Dhelp.lucene ...

  2. f.lux在linux下的安装和使用

    安装还是蛮容易的~只是装完后在白天色温没什么变化就一直以为没有装成功 https://justgetflux.com/linux.html 这里下载,解压后 安装好以后xflux -l (经纬度) 就 ...

  3. 世界线(bzoj2894)(广义后缀自动机)

    由于春希对于第二世代操作的不熟练,所以刚使用完\(invasion process\)便掉落到了世界线之外,错综复杂的平行世界信息涌入到春希的意识中.春希明白了事件的真相. 在一个冬马与雪菜同时存在的 ...

  4. 关于finecms v5 会员头像 任意文件上传漏洞分析

    看到我私藏的一个洞被别人提交到补天拿奖金,所以我干脆在社区这里分享,给大家学习下 本文原创作者:常威,本文属i春秋原创奖励计划,未经许可禁止转载! 1.定位功能 下载源码在本地搭建起来后,正常登陆了用 ...

  5. docker容器的基本操作

    docker容器是独立运行的一个或一组应用,以及它们的运行态环境.下面具体介绍如何管理一个容器,包括容器的创建,启动和停止等. 启动容器 基于镜像新建一个容器并启动 将终止状态的容器重新启动 新建并启 ...

  6. 如何修改静态IP地址和动态IP地址

    打开控制面板,一般在电脑的菜单栏能找到,win8和win10可以使用快捷键(win键+X键),找不到的朋友可以搜索一下.   进入到网络和共享中心,点击更改适配器设置.   这里显示的是电脑所以的网络 ...

  7. C#使用 RNGCryptoServiceProvider 生成强随机字符串

    为了生成更加可靠的随机数,微软在System.Security.Cryptography命名空间下提供一个名为system.Security.Cryptography.RNGCryptoService ...

  8. day 46 Django 学习3 数据库单表操作以及反向解析

    前情提要: Django 已经学了不少了, 今天学习链接数据库的操作.以及相关的反向解析等 一:反向解析 1:反向解析模板层 跳转时设定url会随着前面的路由改变而改变         2:反向解析之 ...

  9. iOS-WKWebview 带有进度条加载的ViewController【KVO监听Webview加载进度】

    前言 为什么要说 WKWebview,在之前做电子书笔记时已经提过 WKWebview 在iOS8之后已完全替代 Webview,原因就不多说了,主要还是内存过大: 封装 封装一个基于 UIViewC ...

  10. 实验三:分别用for、while和do-while循环语句以及递归方法计算n!,并输出算式

    一.用for循环计算n! package for_package; import java.util.*;//导入含有输入类的包 public class for_class { /** * @par ...