Python2 Module 之 Lettuce

安装

pip install lettuce

测试用例

01 测试用例有两个部分组成: 01 测试方法(测试时执行的函数) 02 行为文件(测试步骤描述文件)
02 执行测试用例的过程: 测试方法 - > 读取 - > 行为文件 - > 获取参数 - > 执行测试方法 - > 断言
03 行为文件中的描述 与 测试方法的装饰器中的描述 必须一一对应(可以使用正则表达式进行匹配, 这样就可以在行为文件中修改参数了)

测试文件

测试方法文件
from lettuce import *
'''
概述: 这里写三个方法:
01 使用装饰器匹配行为文件, 读取一个数字
02 计算阶乘
03 比较结果
'''
from lettuce import *
'''
FileName: steps.py
概述: 这里写三个方法:
01 使用装饰器匹配行为文件, 读取一个数字
02 计算阶乘
03 比较结果
'''
# 匹配行为文件中的:  Given I have the number 0
@step('I have the number (\+d)') # (+\d) 正则匹配
def have_the_number(step, number):
    world.number = int(number)

# 匹配行为文件中的: I compute its factorial
@step('I compute its factorial')
def compute_its_factorial(step):
    world.number = factorial(world.number)

# 匹配行为文件中的: I see the number 
@step('I see the number (\+d)')
def compute_its_factorial(step,expected):
    expected = int(expected)
    assert world.number == ecpected,"got %d" % world.number

def factorial(number):
    number = int(number)
    if(number == 0) or (number == 1):
        return 1
    else:
        return number*factorial(number-1)
测试行为文件

场景:
我有一个数字0
我去计算它的阶乘
我看到了结果1
场景…
转为代码如下:

Feature: Compute factorial
	FileName: zero.feature
    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 3
        Given I have the number 3
        When I compute its factorial
        Then I see the number 6

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

执行测试用例

运行lettuce自动测试
注: lettuce 不兼容 python3
01 打开 cmd
02 cd 行为文件的目录
03 输入 lettuce 请勿输入其他, 这里会自动向下级目录查找行为文件, 并完成行为文件 和 测试方法文件的关联, 继而完成测试用例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值