pytest框架编写

pytest框架编写

pytest分为四个步骤:编写测试用例 - 收集测试用例 - 执行测试用例 - 生成测试报告。

编写测试用例:
用例名称、用例步骤、预期结果 、实际结果 、前置后置
1、用例名称:要以test_开头
2、断言:(实际和预期的比对) assert 表达式(True/False)
AssertionError
用例失败:1、出现了AssertionError 2、用例抛其它异常了。
用例呈现的2种形式:
1、.py下的函数,函数名以test_开头
2、.py下类(没有__init__方法)里面的方法,方法名以test_开头

自动收集测试用例:
1、收集用例的目录:以rootdir作为根目录。从rootdir目录下开始搜索用例。
2、目录下的文件过滤:文件名以test_开头的py文件,或者文件名以_test结尾的py文件。
3、文件下的用例过滤:.py下的函数,函数名以test_开头/.py下类(Test开头)里面的方法,方法名以test_开头

执行顺序:
   先找到哪个文件,就先执行哪个文件里的用例。
   文件顺序:ASCII
   文件里有多个用例,用例按什么顺序执行?
   文件内部:自上而下,按照代码先后顺序执行。

pytest报告:
html报告 - html插件
1、pip install pytest-html
2、pytest命令加入参数:–html=报告路径(相对于rootdir)

allure报告 :
用allure命令去生成报告:
ps: 测试结果文件的路径不能出错哦。
命令:allure serve 测试结果文件的路径

	在pytest执行用例的命令当中,添加:
 	--alluredir=路径(相对于rootdir)

文件1:

def login_check(username=None, password=None):
    """ 登录校验的函数
    :param username: 账号
    :param password: 密码
    :return: dict type
    """
    if username != None and password != None:
        if username == 'python33' and password == 'lemonban':
            return {"code": 0, "msg": "登录成功"}
        else:
            return {"code": 1, "msg": "账号或密码不正确"}
    else:
        return {"code": 1, "msg": "所有的参数不能为空"}

"""
1、账号密码正确 
入参:账号python27 密码lemonban 
预期结果:{"code": 0, "msg": "登录成功"} 
实际结果: 

2、账号正确,密码错误 
入参:账号python27 密码lemonban11 
预期结果:{"code": 1, "msg": "账号或密码不正确"} 
实际结果: 

3、账号错误,密码正确, 
入参:账号python25 密码lemonban 
预期结果:{"code": 1, "msg": "账号或密码不正确"} 
实际结果:

4、账号为空 
入参:账号为空 密码lemonban11 
预期结果:{"code": 1, "msg": "所以的参数不能为空"} 
实际结果: 

5、密码为空、 
入参:账号Python6 密码为空 
预期结果:{"code": 1, "msg": "所以的参数不能为空"} 
实际结果 
"""
文件2:
from test_2.test_016pytest.login import login_check

"""
1、账号密码正确 
入参:账号python27 密码lemonban 
预期结果:{"code": 0, "msg": "登录成功"} 
实际结果: 
"""

def test_1():
    data = ["python27", "lemonban"]
    expect = {"code": 0, "msg": "登录成功"}
    actual = login_check(*data)
    assert expect == actual

"""
2、账号正确,密码错误 
入参:账号python27 密码lemonban11 
预期结果:{"code": 1, "msg": "账号或密码不正确"} 
实际结果:
"""
def test_2():
    data = ["python27", "lemonban11"]
    expect = {"code": 1, "msg": "账号或密码不正确"}
    actual = login_check(*data)
    assert expect == actual

"""
3、账号错误,密码正确, 
入参:账号python25 密码lemonban 
预期结果:{"code": 1, "msg": "账号或密码不正确"} 
实际结果:
"""
def test_3():
    data = ["python25", "lemonban"]
    expect = {"code": 1, "msg": "账号或密码不正确"}
    actual = login_check(*data)
    assert expect == actual

"""
4、账号为空 
入参:账号为空 密码lemonban11 
预期结果:{"code": 1, "msg": "所以的参数不能为空"} 
实际结果: 
"""
def test_4():
    data = [" ", "lemonban11"]
    expect = {"code": 1, "msg": "所以的参数不能为空"}
    actual = login_check(*data)
    assert expect == actual

"""
5、密码为空、 
入参:账号Python6 密码为空 
预期结果:{"code": 1, "msg": "所以的参数不能为空"} 
实际结果
"""
def test_5():
    data = ["Python6"," "]
    expect = {"code": 1, "msg": "所以的参数不能为空"}
    actual = login_check(*data)
    assert expect == actual

文件3:
import pytest

pytest.main(["-s","-v","--html= test_html.html","--alluredir= allure_files"])
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值