python行为驱动小笔记
admin
2023-07-24 04:02:35
0

执行方式:

lettuce features1
python行为驱动小笔记

D:\TOOL\PycharmProjects\python2\CS\xingweiqudong>lettuce features1
d:\program files\python\lib\site-packages\fuzzywuzzy\fuzz.py:35: UserWarning: Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning
  warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning')

Feature: Compute factorial       # \features1\zero.feature:1
  In order to play with Lettuce  # \features1\zero.feature:2
  As beginners                   # \features1\zero.feature:3
  We'll implement factorial      # \features1\zero.feature:4

  Scenario: Factorial of 0       # \features1\zero.feature:6
    Given I have the number 0    # \features1\steps.py:37
    When I compute its factorial # \features1\steps.py:44
    Then I see the number 1      # \features1\steps.py:51

  Scenario: Factorial of 1       # \features1\zero.feature:11
    Given I have the number 1    # \features1\steps.py:37
    When I compute its factorial # \features1\steps.py:44
    Then I see the number 1      # \features1\steps.py:51

  Scenario: Factorial of 2       # \features1\zero.feature:16
    Given I have the number 2    # \features1\steps.py:37
    When I compute its factorial # \features1\steps.py:44
    Then I see the number 2      # \features1\steps.py:51

  Scenario: Factorial of 3       # \features1\zero.feature:21
    Given I have the number 3    # \features1\steps.py:37
    When I compute its factorial # \features1\steps.py:44
    Then I see the number 6      # \features1\steps.py:51

1 feature (1 passed)
4 scenarios (4 passed)
12 steps (12 passed)

D:\TOOL\PycharmProjects\python2\CS\xingweiqudong>lettuce features
d:\program files\python\lib\site-packages\fuzzywuzzy\fuzz.py:35: UserWarning: Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning
  warnings.warn('Using slow pure-python SequenceMatcher. Install python-Levenshtein to remove this warning')

Feature: Compute factorial       # \features\zero.feature:1
  In order to play with Lettuce  # \features\zero.feature:2
  As beginners                   # \features\zero.feature:3
  We'll implement factorial      # \features\zero.feature:4

  Scenario: Factorial of 0       # \features\zero.feature:6
    Given I have the number 0    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 1      # \features\step.py:24

  Scenario: Factorial of 1       # \features\zero.feature:11
    Given I have the number 1    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 1      # \features\step.py:24

  Scenario: Factorial of 2       # \features\zero.feature:16
    Given I have the number 2    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 2      # \features\step.py:24

  Scenario: Factorial of 3       # \features\zero.feature:21
    Given I have the number 3    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 6      # \features\step.py:24

  Scenario: Factorial of 4       # \features\zero.feature:26
    Given I have the number 3    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 6      # \features\step.py:24

  Scenario: Factorial of 5       # \features\zero.feature:31
    Given I have the number 3    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 6      # \features\step.py:24

  Scenario: Factorial of 6       # \features\zero.feature:36
    Given I have the number 3    # \features\step.py:13
    When I compute its factorial # \features\step.py:18
    Then I see the number 6      # \features\step.py:24

1 feature (1 passed)
7 scenarios (7 passed)
21 steps (21 passed)

D:\TOOL\PycharmProjects\python2\CS\xingweiqudong>

代码1

# encoding=utf-8
from lettuce import world, steps

def factorial(number):
    number = int(number)
    if (number == 0) or (number == 1):
        return 1
    else:
        return reduce(lambda x, y: x * y, range(1, number + 1))

@steps
class FactorialSteps(object):
    """Methods in exclude or starting with _ will not be considered as step"""

    exclude = ['set_number', 'get_number']

    def __init__(self, environs):
        # 初始全局变量
        self.environs = environs

    def set_number(self, value):
        # 设置全局变量中的number变量的值
        self.environs.number = int(value)

    def get_number(self):
        # 从全局变量中取出number的值
        return self.environs.number

    def _assert_number_is(self, expected, msg="Got %d"):
        number = self.get_number()
        # 断言
        assert number == expected, msg % number

    def have_the_number(self, step, number):
        '''I have the number (\d+)'''
        # 上面的三引号引起的代码必须写,并且必须是三引号引起
        # 表示从场景步骤中获取需要的数据
        # 并将获得数据存到环境变量number中
        self.set_number(number)

    def i_compute_its_factorial(self, step):
        """When I compute its factorial"""
        number = self.get_number()
        # 调用factorial方法进行阶乘结算,
        # 并将结算结果存于全局变量中的number中
        self.set_number(factorial(number))

    def check_number(self, step, expected):
        '''I see the number (\d+)'''
        # 上面的三引号引起的代码必须写,并且必须是三引号引起
        # 表示从场景步骤中获取需要的数据以便断言测试结果
        self._assert_number_is(int(expected))

FactorialSteps(world)

用例

Feature: Compute factorial
  In order to play with Lettuce
  As beginners
  We'll implement factorial

  Scenario: Factorial of 0
    Given I have the number 0
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 1
    Given I have the number 1
    When I compute its factorial
    Then I see the number 1

  Scenario: Factorial of 2
    Given I have the number 2
    When I compute its factorial
    Then I see the number 2

  Scenario: Factorial of 3
    Given I have the number 3
    When I compute its factorial
    Then I see the number 6

相关内容

热门资讯

委内瑞拉强震已致235人死亡 新华社快讯:据委内瑞拉卫生部通报,委内瑞拉强震遇难人数升至235人。
空调开了,主机不转怎么回事 可能的原因是电源突然断电,可以通过检查一下内机和外机通电是否正常来解决这个问题。可能的原因是冷凝器出...
冰箱压缩机毛细管结霜也不制冷是... 冰箱压缩机毛细管结霜也不制冷是什么原因1、有可能是冰箱中的制冷剂太少了导致冰箱无法正常制冷。2、可能...
联合国已经启动紧急响应,援助委... 据凤凰卫视报道,委内瑞拉北部接连遭遇两次强烈地震,造成重大人员伤亡和大范围破坏。联合国秘书长古特雷斯...
冰箱压缩机加氟加好低压管多少时... 冰箱压缩机加氟加好低压管多少时间结霜加好氟以后低压管是冷的,但是不能有结霜情况,似有露水似没有露水是...
保鲜柜压缩机制冷管结霜 1、或许是因为把保鲜柜里面的制冷剂添加的太多了,导致在制冷的过程中由于提供的液量大于蒸发器的热量,所...
冰箱压缩机不停机,回气管结霜 1、可能是因为冷媒量太少了,然后导致蒸发器蒸发的压力很低,造成蒸发的问题,使得蒸发器结霜,然后慢慢的...
当世界冠军退役后直播卖海鲜 李园园在拍摄短视频。李园园的直播间和别人的有些不一样,背景简陋、几乎没什么布景,地点就设在她打包海鲜...
鹤壁市财政局原党组成员、副局长... 经中共鹤壁市委批准,鹤壁市纪委监委对鹤壁市财政局原党组成员、副局长孟涛严重违纪违法问题进行了立案审查...
“在党的领导下把家园建设得越来... 新华社山东德州6月25日电 题:“在党的领导下把家园建设得越来越美” ——习近平总书记在山东德州考察...