python selenium T5

python selenium T5

lettuce : BDD 行为驱动测试

$pip3 install lettuce

EG1 :

$ cat test_lettuce.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-


from lettuce import *


@step('I have the number (d+)')
def have_the_number(step, number):
    world.number = int(number)


@step('I compute its factorial')
def compute_its_factorial(step):
    world.number = factorial(world.number)


@step('I see the number (d+)')
def check_number(step, expected):

    expected = int(expected)
    assert world.number == expected, 
        "Got %d" % world.number


def factorial(number):
    number = int(number)
    if (number == 0) or (number == 1):
        return 1
    else:
        return number * factorial(number-1)

  

$ cat features/zero.feature
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

  

Result:

$ lettuce 

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    # test_lettuce.py:11
    When I compute its factorial # test_lettuce.py:16
    Then I see the number 1      # test_lettuce.py:21

  Scenario: Factorial of 1       # features/zero.feature:11
    Given I have the number 1    # test_lettuce.py:11
    When I compute its factorial # test_lettuce.py:16
    Then I see the number 1      # test_lettuce.py:21

  Scenario: Factorial of 2       # features/zero.feature:16
    Given I have the number 2    # test_lettuce.py:11
    When I compute its factorial # test_lettuce.py:16
    Then I see the number 2      # test_lettuce.py:21

  Scenario: Factorial of 3       # features/zero.feature:21
    Given I have the number 3    # test_lettuce.py:11
    When I compute its factorial # test_lettuce.py:16
    Then I see the number 6      # test_lettuce.py:21

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

  

EG2 :

  使用lettuce webdriver 自动化测试

原文地址:https://www.cnblogs.com/zsr0401/p/6492226.html