• Behave 基础


    在你使用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.
  • 相关阅读:
    微信公众平台回复音乐
    Else is very important
    Generate source code from wsdl
    PHP Simple HTML DOM Parser: check elements with multiple classes
    Get Version is the first function is called?
    Remote debug GWT UI
    Remote Debug For Java Application On Tomcat
    SetStyleName引起的Regression Issue
    做乘法运算的时候需要考虑越界问题
    MySQL Uniall All
  • 原文地址:https://www.cnblogs.com/v394435982/p/6401844.html
Copyright © 2020-2023  润新知