在你使用behave或其餘BDD框架以前, 你應該選擇一個斷言庫。 python有不少這方面的第三方庫。例如: hamcrest,nose.tools, should-dsl, sure, compare, describe等。選擇一個本身喜歡的。python
# 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))
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))
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
# 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"])
# 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"))
# 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執行以前執行
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)
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.