博客
关于我
Pytest测试框架快速搭建
阅读量:797 次
发布时间:2023-03-05

本文共 2580 字,大约阅读时间需要 8 分钟。

介绍

pytest 是一个成熟的 Python 测试框架,支持单元测试、功能测试、Selenium/Applium 自动化测试以及接口自动化测试(如 pytest + requests)。其丰富的插件生态和高度可扩展性,使其成为测试开发的首选工具。常用插件包括 pytest-selenium、pytest-html、pytest-rerunfailures 和 pytest-xdist 等。

安装

安装 pytest:

pip install pytest

验证安装:

pytest --version

安装 pytest-html 报告模板:

pip install pytest-html

安装失败后重复执行模板:

pip install pytest-rerunfailures

pytest 约束规则

  • 测试文件名应遵循 test_*.py*_test.py 格式。
  • 测试类以 Test 开头,不需要 __init__ 方法。
  • 测试方法以 test_ 开头, pytest 会自动查找符合约束的测试用例。
  • 批量执行脚本名应以 test_ 开头。
  • 编写脚本

    示例脚本:

    import pytestdef add(a, b):    return a + b@pytest.fixturedef TestCase():    passclass TestCase:    @pytest.fixture    def test_add(self):        assert add(1, 2) == 3if __name__ == '__main__':    pytest.main()

    运行

    运行命令:

    pytest [参数]

    常用参数

  • -v:输出详细信息。
  • -s:输出测试函数或方法的 print 内容。
  • -k:按关键字过滤用例。
  • -m:按分类执行测试。
  • -x:执行失败用例时立即停止。
  • --maxfail:指定最大失败次数。
  • --tb=line:错误信息以单行展示。
  • 参数化

    示例:

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

    前置后置

  • 模块级

    • setup_module():模块执行前运行一次。
    • teardown_module():模块执行后运行一次。
  • 函数级

    • setup_function():每个用例执行前运行一次。
    • teardown_function():每个用例执行后运行一次。
  • 类级

    • setup_class():类执行前运行一次。
    • teardown_class():类执行后运行一次。
  • 方法级

    • setup_method():每个方法执行前运行一次。
    • teardown_method():每个方法执行后运行一次。
  • 测试实例

    登录接口测试

    import pytestimport requests@pytest.fixturedef login():    print("登录中...")    yield    print("退出登录")@pytest.mark.parametrize('data', data_list)def test_login(data):    url = 'http://example.com/login'    response = requests.post(url, data=data)    assert response.status_code == 200    print(response.text)if __name__ == '__main__':    pytest.main()

    搜索接口测试

    import xlrdimport pytestimport requests@pytest.fixturedef search_params():    return {        'platf': 'Windows',        'excludePayTemplate': True,        'is_excluded': False,        'excluded_member_discount': False,        'keyword': '测试',        'start': 1,        'count': 10    }@pytest.mark.parametrize('params', params_list)def test_search(params):    url = 'https://template.wps.com/server/pf/template'    response = requests.get(url, params=params)    print(f"响应状态码:{response.status_code}")    print(f"响应内容:{response.text}")if __name__ == '__main__':    pytest.main()

    fixture

    fixture 是 pytest 的灵活前置和后置解决方案。示例:

    import pytest@pytest.fixturedef login():    print("登录中...")    yield    print("退出登录")def test_s1(login):    print("测试用例 1:登录后的操作")def test_s2():    print("测试用例 2:不需要登录的操作")if __name__ == '__main__':    pytest.main()

    HTML 报告生成

    安装 pytest-html:

    pip install pytest-html

    生成报告:

    pytest --html=report.html tests/

    结语

    通过以上内容,希望你能快速上手 pytest 并开始你的测试开发之旅!如果对某些内容有疑问或需要更深入的探讨,请随时联系我。

    转载地址:http://dnafk.baihongyu.com/

    你可能感兴趣的文章
    Python 3.5、ldap3 和 modify_password()
    查看>>
    python 3.6.8 升级至3.9版本升级
    查看>>
    Python 3.9 到 Python 3.12 的发展历程与区别
    查看>>
    python 32位和64位的区别在哪
    查看>>
    Python 3:何时使用 dict,何时使用元组列表?
    查看>>
    Python 3d 绘图 - 轴居中
    查看>>
    python ==》 字典
    查看>>
    python anaconda 安装使用
    查看>>
    python and或or 当参数传递的时候的用法
    查看>>
    Python append() 与列表上的 + 运算符,为什么这些会给出不同的结果?
    查看>>
    Python APP自动化测试工具adb与Monkey使用详解
    查看>>
    Python APP自动化测试框架Appium详解
    查看>>
    Python APP自动化测试框架开发实战
    查看>>
    python argparse模块
    查看>>
    Python asyncio库的学习和使用
    查看>>
    Python AttributeError:“dict“对象没有属性“append“
    查看>>
    Python base64和hashlib模块
    查看>>
    python basic programs
    查看>>
    python bert_gen.py 报错Unable to load weights from pytorch checkpoint file for......
    查看>>
    python binascii.Error: Incorrect padding
    查看>>