1. 程式人生 > >python行為驅動小筆記

python行為驅動小筆記

() pass ber utf vpd have odi block obj

執行方式:

lettuce features1
技術分享圖片

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

python行為驅動小筆記