栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

pytest框架使用示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pytest框架使用示例

    模块:pytest
    安装:pip3 install pytest
    导入模块:import pytest基本使用方法:
    2.1说明:
    2.1.1函数名必须以test开头
    2.1.2类名必须以Test开头,类中的方法需要以test开头
    2.1.2.1类中不能有构造方法
    2.1.3使用assert进行断言
    2.1.4脚本名必须以test开头
    2.2作用于函数
import pytest


def test_1():
    assert 1 == 1


def atest_1():
    print(123)
    assert 1 == 1


def test_1():
    assert 12 > 100


if __name__ == '__main__':
    # pytest.main(['-s', __file__])
    pytest.main(['-v', 'test_pytest/test_1_func.py'])
    pytest.main(['-q', 'test_pytest/test_1_func.py'])

2.3. 作用于类

import pytest


class Test_Add:
    # def __init__(self):
    #     pass

    def test(self):
        assert 12 > 100

    def atest(self):
        assert 12 > 100


if __name__ == '__main__':
    pytest.main(['-v', __file__])
	2.4. 用例执行

方式
脚本:pytest.mian([参数1,参数2,参数。。。])

命令行
pytest 参数 脚本
pytest 参数 脚本
python -m pytest 参数脚本

参数
-s,
-v,
-q,
-k,通过关键字匹配脚本、函数名、类名、方法名
-x,如果测试执行过程中有fail的用例,则测试立即停止
–maxfile=n,当fail用例达到指定的数量,则退出测试
-m,对用例进行标记,执行指定的用例

1.在项目根目录下新建文件pytest.ini
2.在pytest.ini文件中添加标记

[pytest]
markers = a
          b
          c
          smoke_case

c. 使用装饰器标记测试用例@pytest.mark.xxx

@pytest.mark.a
def test_1():
    assert 1 == 1


@pytest.mark.b
def test_2():
    print(123)
    assert 1 == 1


@pytest.mark.c
def test_3():
    assert 12 > 100

4.执行测试时,使用-m标记即可执行指定的用例
python3 -m pytest -v -m a ./

跳过用例
@pytest.mark.skip(reason=xxx),无条件跳过指定用例
@pytest.mark.skip(reason='此版本无条件跳过用例')

@pytest.mark.skipif(条件,reason=xxx),有条件跳过指定用例
@pytest.mark.skipif(1 == 1, reason='此版本有条件跳过用例')

参数化
@pytest.mark.parametrize('参数1,参数2,参数...',值)
参数,与被装饰的函数的形参相同
值,传递给参数的数据,如果需要给多个参数传数据可以将这些数据封包到元组中

import pytest


@pytest.mark.parametrize('a', [1, 2, 3, 4, 5])
def test(a):
    print(a)


@pytest.mark.parametrize('a,b,c', [(1, 2, 3), (0, 1, 1), (1, -1, 2)])
def test_1(a, b, c):
    assert a + b == c


if __name__ == '__main__':
    pytest.main(['-v', __file__])
import json

import pytest
import requests

rq = requests.session()

cases = [('登陆成功', 'admin', '123456', 'success'),
         ('用户名为空', '', '123456', '用户名不能为空'),
         ('密码错误', 'admin', '1234567', '密码不正确')]


@pytest.mark.parametrize('name,user,pwd,ex', cases)
def test_login(name, user, pwd, ex):
    '''登陆成功'''
    print(f'验证:{name}')
    url = 'https://manage-saas-test.styd.cn/login/account'
    login_data = {"name": user, "password": pwd}
    header = {"app-id": "10002", 'app-version': '1.18.3', 'Content-Type': 'application/json; charset=utf-8',
              'device-type': '2', 'User-Agent': 'Dart/2.8(dart:io)', 'app-code': '0'}
    r = rq.post(url=url, data=json.dumps(login_data), headers=header)
    print(r.text)

    resp = json.loads(r.text)
    resp_msg = resp['msg']
    print(resp_msg)
    assert resp_msg == ex


@pytest.mark.parametrize('case', cases)
def test_login_1(case):
    '''登陆成功'''
    print(f'验证:{case[0]}')
    url = 'http://localhost:8080/login/account'
    login_data = {"name": case[1], "password": case[2]}
    header = {'Content-Type': 'application/json; charset=utf-8'}
    r = rq.post(url=url, data=json.dumps(login_data), headers=header)
    print(r.text)

    resp = json.loads(r.text)
    resp_msg = resp['msg']
    print(resp_msg)
    assert resp_msg == case[3]


#
# def test_login_success():
#     '''登陆成功'''
#     url = 'http://localhost:8080/login/account'
#     login_data = '{"name": "admin", "password": "123456"}'
#     header = {'Content-Type': 'application/json; charset=utf-8'}
#     r = rq.post(url=url, data=login_data, headers=header)
#     print(r.text)
#
#     resp = json.loads(r.text)
#     resp_msg = resp['msg']
#     resp_code = resp['code']
#     assert resp_msg == 'success' and resp_code == 0
#
#
# def test_login_error_1():
#     '''用户名为空'''
#     url = 'https://localhost:8080/login/account'
#     login_data = '{"name": "", "password": "123456"}'
#     header = {'Content-Type': 'application/json; charset=utf-8'}
#     r = rq.post(url=url, data=login_data, headers=header)
#     print(r.text)
#
#     resp = json.loads(r.text)
#     resp_msg = resp['msg']
#     resp_code = resp['code']
#     assert resp_msg == '用户名不能为空' and resp_code == 10004
#
#
# def test_login_error_2():
#     '''密码错误'''
#     url = 'http://localhost:8080/login/account'
#     login_data = '{"name": "admin", "password": "1234567"}'
#     header = {'Content-Type': 'application/json; charset=utf-8'}
#     r = rq.post(url=url, data=login_data, headers=header)
#     print(r.text)
#
#     resp = json.loads(r.text)
#     resp_msg = resp['msg']
#     resp_code = resp['code']
#     assert resp_msg == '密码不正确' and resp_code == 60100


if __name__ == '__main__':
    # test_login_success()
    pytest.main(['-v', __file__])

fixture
创建:@pytest.fixture([name,scope,params,autouse])

name,指定fixture名称,如果不指定则默认为被装饰的函数名
scope,指定fixture作用范围,module、class、function(默认)、session、package
params,参数 autouse,设置为True,实现自动调用fixture

#前置局部fixture:
import pytest


@pytest.fixture(name='fx', scope='module', autouse=True, params=[1, 2, 3, 4])
def login():
    print('登录')


def test_add():
    print('添加会员')


def test_query_store():
    print('查询库存')


if __name__ == '__main__':
    pytest.main(['-s', __file__])
#后置局部fixture:
import pytest


@pytest.fixture(name='fx', scope='module', autouse=True, params=[1, 2, 3, 4])
def login():
    print('登录')
    yield '注销'


def test_add(fx):
    a = fx
    print('添加会员')
    print(a)


def test_query_store():
    print('查询库存')


if __name__ == '__main__':
    pytest.main(['-s', __file__])
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/769371.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号