首页 > 其他分享 >pytest测试框架(一)初识

pytest测试框架(一)初识

时间:2022-09-07 20:02:23浏览次数:90  
标签:answer 框架 初识 assert 用例 pytest 测试 test

简介

pytest是一个全功能的python的测试工具,与python自带的unittest测试框架类似,但pytest使用起来更加的简洁和高效,并兼容unittest框架。pytest可以结合requests实现接口测试,结合selenium、appium实现ui自动化测试,还可以结合allure2集成到Jenkins中实现持续集成,支持的扩展插件非常多,具体可以查看文档:https://docs.pytest.org/

安装

pip install -U pytest

用例识别与运行

  • 用例编写规范
    • 测试文件以 test_开头或以_test结尾
    • 测试类以Test开头,并且不能带有__init__方法
    • 测试函数以test_开头
    • 断言使用assert
# content of test_sample.py
def inc(x):
    return x + 1


def test_answer():
    assert inc(3) == 5
  • 用例运行
    • 直接使用pytest命令执行, pytest会收集当前目录和递归查找子目录下所有符合编写规范的函数、类、方法来执行
$ pytest
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-7.x.y, pluggy-1.x.y
rootdir: /home/sweet/project
collected 1 item

test_sample.py F                                                     [100%]

================================= FAILURES =================================
_______________________________ test_answer ________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:6: AssertionError
========================= short test summary info ==========================
FAILED test_sample.py::test_answer - assert 4 == 5
============================ 1 failed in 0.12s =============================
  • 结果分析
    • 执行结果中, F 代表用例未通过, . 代表用例通过

标签:answer,框架,初识,assert,用例,pytest,测试,test
From: https://www.cnblogs.com/xxiaow/p/16667090.html

相关文章