• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pytest框架使用展示

武飞扬头像
随风随心随意
帮助1

  1. 模块:pytest
    安装:pip3 install pytest
    导入模块:import pytest
  2. 基本使用方法:
    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', 'xxx', 'success'),
         ('用户名为空', '', 'xxx', '用户名不能为空'),
         ('密码错误', 'admin', 'xxx', '密码不正确')]


@pytest.mark.parametrize('name,user,pwd,ex', cases)
def test_login(name, user, pwd, ex):
    '''登陆成功'''
    print(f'验证:{name}')
    url = 'https://manage-saas-test.xxx.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": "xxxx"}'
#     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": "xxxx"}'
#     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": "xxxx"}'
#     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__])
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggbicj
系列文章
更多 icon
同类精品
更多 icon
继续加载