快乐学习
前程无忧、中华英才非你莫属!

Python行为驱动开发BDD-1

一、课程大纲


1、第一步

编写谁都能看的懂的功能描述文档

# file:features/tutorial01_basics.feature
Feature: Showing off behave (tutorial01)

Scenario: Run a simple test
    Given we have behave installed
    When we implement a test
    Then behave will test it for us!

提供自动化测试的代码

# file:features/steps/step_tutorial01.py
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave import given, when, then

@given('we have behave installed')
def step_impl(context):
    pass

@when('we implement a test')
def step_impl(context):
    assert True is not False

@then('behave will test it for us!')
def step_impl(context):
    assert context.failed is False

运行自动化测试代码,只需要运行功能描述文件.feature

$ behave ../features/tutorial01_basics.feature
Feature: Showing off behave (tutorial01)   # ../features/tutorial01_basics.feature:1

  Scenario: Run a simple test        # ../features/tutorial01_basics.feature:3
    Given we have behave installed   # ../features/steps/step_tutorial01.py:12
    When we implement a test         # ../features/steps/step_tutorial01.py:16
    Then behave will test it for us! # ../features/steps/step_tutorial01.py:20

1 feature passed, 0 failed, 0 skipped
1 scenario passed, 0 failed, 0 skipped
3 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.000s

2、自然语言

编写完整的功能描述文件

# file:features/tutorial02_natural_language.feature
Feature: Fight or Flight (Natural Language, tutorial02)

    In order to increase the ninja survival rate,
    As a ninja commander
    I want my ninjas to decide whether to take on an opponent
    based on their skill levels.

    Scenario: Weaker opponent
        Given the ninja has a third level black-belt
        When attacked by a samurai
        Then the ninja should engage the opponent

    Scenario: Stronger opponent
        Given the ninja has a third level black-belt
        When attacked by Chuck Norris
        Then the ninja should run for his life

具体自动化测试代码

# file:features/steps/step_tutorial02.py
# ----------------------------------------------------------------------------
# STEPS:
# ----------------------------------------------------------------------------
from behave   import given, when, then
from hamcrest import assert_that, equal_to, is_not

@given('the ninja has a {achievement_level}')
def step_the_ninja_has_a(context, achievement_level):
    context.ninja_fight = NinjaFight(achievement_level)

@when('attacked by a {opponent_role}')
def step_attacked_by_a(context, opponent_role):
    context.ninja_fight.opponent = opponent_role

@when('attacked by {opponent}')
def step_attacked_by(context, opponent):
    context.ninja_fight.opponent = opponent

@then('the ninja should {reaction}')
def step_the_ninja_should(context, reaction):
    assert_that(reaction, equal_to(context.ninja_fight.decision()))

具体为业务系统的被测业务逻辑、

# file:features/steps/step_tutorial02.py
# ----------------------------------------------------------------------------
# PROBLEM DOMAIN:
# ----------------------------------------------------------------------------
class NinjaFight(object):
    """
    Domain model for ninja fights.
    """
    # pylint: disable=R0903

    def __init__(self, with_ninja_level=None):
        self.with_ninja_level = with_ninja_level
        self.opponent = None

    def decision(self):
        """
        Business logic how a Ninja should react to increase his survival rate.
        """
        assert self.with_ninja_level is not None
        assert self.opponent is not None
        if self.opponent == "Chuck Norris":
            return "run for his life"
        if "black-belt" in self.with_ninja_level:
            return "engage the opponent"
        else:
            return "run for his life"

运行完整实例

$ behave ../features/tutorial02_natural_language.feature
Feature: Fight or Flight (Natural Language, tutorial02)   # ../features/tutorial02_natural_language.feature:1
  In order to increase the ninja survival rate,
  As a ninja commander
  I want my ninjas to decide whether to take on an opponent
  based on their skill levels.

  Scenario: Weaker opponent                      # ../features/tutorial02_natural_language.feature:8
    Given the ninja has a third level black-belt # ../features/steps/step_tutorial02.py:57
    When attacked by a samurai                   # ../features/steps/step_tutorial02.py:61
    Then the ninja should engage the opponent    # ../features/steps/step_tutorial02.py:69

  Scenario: Stronger opponent                    # ../features/tutorial02_natural_language.feature:13
    Given the ninja has a third level black-belt # ../features/steps/step_tutorial02.py:57
    When attacked by Chuck Norris                # ../features/steps/step_tutorial02.py:65
    Then the ninja should run for his life       # ../features/steps/step_tutorial02.py:69

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
6 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.001s

打赏

未经允许不得转载:同乐学堂 » Python行为驱动开发BDD-1

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

特别的技术,给特别的你!

联系QQ:1071235258QQ群:367203382
error: Sorry,暂时内容不可复制!