博客
关于我
Pytest测试框架快速搭建
阅读量:804 次
发布时间: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 UI自动化测试集成UnitTest
    查看>>
    python unicode-escape
    查看>>
    Python unittest mock:是否可以在测试时模拟方法默认参数的值?
    查看>>
    Python unittest单元测试框架 TestSuite测试套件
    查看>>
    PYTHON调离线语音合成并实时播放
    查看>>
    python unittest高级特性!
    查看>>
    Python unittest:如何将标准输出消息临时重定向到缓冲区并测试其内容?
    查看>>
    Python urllib/Requests下载文件失败,但浏览器下载失败
    查看>>
    Python urllib2 文件上传问题
    查看>>
    Python urllib2.open 连接由对等错误重置
    查看>>
    python urllib2详解及实例
    查看>>
    Python url请求提示certificate verify failed unable to get local issuer certificate
    查看>>
    Python UTC 日期时间对象的 ISO 格式不包括 Z(祖鲁语或零偏移)
    查看>>
    python valueerror object2_python遇到错误记录
    查看>>
    python vars的作用
    查看>>
    Python vcrpy库:HTTP请求记录和重放
    查看>>
    Python virtualenv
    查看>>
    python vue3实现大文件分段续传(断点续传)--带暂停和继续功能
    查看>>
    Python WebDriver如何打印整个页面源(html)
    查看>>
    Python WebSocket自动化测试:构建高效接口测试框架
    查看>>