【十】behave之用背景use background实例九

目标:使用后台概念在每个场景之前执行许多步骤

1.编写Feature Test 功能测试,这里是沿用之前的实例三

## Created by bxf at 2019/11/12
#Feature: Using Background --购买化妆品(Natural Language Part2,tutorial09)

Feature: 用背景
  Background: 肤质设置
    Given 不同女生不同肤质

  Scenario: 油皮
    Given 化妆品我有lanKou
    When 女生是girlB
    Then 脸的感觉应该是自然

  Scenario: 干皮
    Given 化妆品我有IPSA
    When 女生是girlA
    Then 脸的感觉应该是服帖

2.编写自动化测试

#encoding:utf-8
#date:2019/12/11 14:40
#@Author:sunny

from behave   import given, when, then
from hamcrest import assert_that, equal_to, is_not

class Cosmetics(object):
    """
    化妆品的种类
    """
    # pylint: disable=R0903

    def __init__(self, with_cosmetics_level=None):
        self.with_cosmetics_level = with_cosmetics_level
        self.girl = None

    def Fit(self):
        """
        Business logic how a Ninja should react to increase his survival rate.
        """
        assert self.with_cosmetics_level is not None
        assert self.girl is not None
        if self.girl == "girlB":
            return "自然"
        if "IPSA" in self.with_cosmetics_level:
            return "服帖"
        else:
            return "自然"

@given('不同女生不同肤质') def step_the_cosmetics_settings(context): """ 后台步骤在每个场景开始时调用,在其他步骤之前调用 :param context: :return: """ if hasattr(context,"Cosmetics_fit"): assert_that(context.Cosmetics_fit,is_not(None)) context.Cosmetics_fit=None @given('化妆品我有{cosmetics_level}') def step_the_cosmetics_has_a(context, cosmetics_level): context.cosmetics = Cosmetics(cosmetics_level) @when('女生是{girl}') def step_attacked_by(context, girl): context.cosmetics.girl = girl @then('脸的感觉应该是{feel}') def step_the_ninja_should(context, feel): assert_that(feel, equal_to(context.cosmetics.Fit()))

3.运行自动化测试

E:\exercise\producer2-test>behave
Feature: 用背景 # features/tutorial09_background.feature:4

  Background: 肤质设置  # features/tutorial09_background.feature:5

  Scenario: 油皮        # features/tutorial09_background.feature:8
    Given 不同女生不同肤质    # features/steps/step_tutorial09.py:31
    Given 化妆品我有lanKou # features/steps/step_tutorial09.py:44
    When 女生是girlB     # features/steps/step_tutorial09.py:48
    Then 脸的感觉应该是自然    # features/steps/step_tutorial09.py:52

  Scenario: 干皮      # features/tutorial09_background.feature:13
    Given 不同女生不同肤质  # features/steps/step_tutorial09.py:31
    Given 化妆品我有IPSA # features/steps/step_tutorial09.py:44
    When 女生是girlA   # features/steps/step_tutorial09.py:48
    Then 脸的感觉应该是服帖  # features/steps/step_tutorial09.py:52

1 feature passed, 0 failed, 0 skipped
2 scenarios passed, 0 failed, 0 skipped
8 steps passed, 0 failed, 0 skipped, 0 undefined
Took 0m0.007s

E:\exercise\producer2-test>
View Code
posted @ 2019-12-11 15:28  做一只热爱生活的小透明  阅读(404)  评论(0)    收藏  举报