自动化框架-pytest(一)

本文介绍了Pytest,一个强大的Python测试框架,涵盖了安装、运行方式、fixture的使用、作用域、自定义标记以及参数化测试。详细讲解了如何编写和执行各种类型的测试,包括前置/后置操作和协作开发中的配置管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

定义:
Pytest是一个用于编写和执行Python测试的全功能测试框架。它是一个第三方库,提供了简洁、灵活和易于使用的方式来编写各种类型的测试,包括单元测试、集成测试和功能测试等。
Pytest是一个功能虫大、易于使用和扩展的Python测试框架,提供了丰富的功能和灵活的测试编写方式,使得编写和执行Python测试变得更加高效和愉快。
安装:

pip install pytest

#检验
pytest --version

运行方式:
pytest——pytest.main([ ])
pytest -s——pytest.main([‘-s’])
pytest -s -v——pytest.main([‘-s’,‘-v’])
pytest -sv——pytest.main([‘-sv’])
pytest test_01.py::Test01::test001 -sv——pytest.main([‘test_01.py::Test01::test001’],‘-sv’)
#筛选运行
pytest.main([‘-k’,‘用例函数名’,‘-sv’])
测试前置、后置操作

def setup():
	print('前置操作')
def teardown():
	print('后置操作')
def test_example():
	print('执行测试用例')
def setup_function()
	print('执行函数级别的前置操作')
def teardown_function():
	print('执行函数级别的后置操作')
def setup_class()
	print('执行类级别的前置操作')
def teardown_class():
	print('执行类级别的后置操作')

pytest标签

@pytest.mark.skip
def test():
	print('跳过此条用例')
@pytest.mark.自定义标签
#自定义标签主要用于筛选用例,还可以进行逻辑判断
#自定义标签需要在pytest.ini文件中申明,格式如下
#[pytest]
#markers =
#	自定义标签:含义 

#执行
pytest.main(['-sv',-m,'标签名称'])
#按逻辑判断执行
pytest.main(['-sv',-m,'not 标签名称'])#非
pytest.main(['-sv',-m,'a or b'])#或
pytest.main(['-sv',-m,'a and b'])#和

fixture
参数:
scope:fixture的作用域,默认为function;
autouse:默认:False,需要用例手动调用该fixture;如果是True,所有作用域内的测试用例都会自动调用该fixture;
name:装饰器的名称,别名,一旦起了别名,那么fixture 本身的名字就不能使用了
params:可以传递一个可迭代对象,然后使用内置request接收参数, request为函数对象,使用request.param接收参数
ids:用例标识ID,与params配合使用,一对一关系
一、fixture作为参数使用
场景:用例1需要登录,用例2不需要登录

import pytest
@pytest.fixture()
def login():
    print("这个是登录方法")
def test_case1(login):
    print("test_case1,需要登录")
    pass
def test_case2():
    print("test_case2,不用登录")
    pass
if __name__ == '__main__':
    pytest.main()

二、fixture的作用范围
作用范围顺序:session>module>class>function
场景:执行用例前打开浏览器,执行完成关闭浏览器

import pytest
# 作用域:module
@pytest.fixture(scope="module")
def open():
    print("打开浏览器")
    yield
    print("执行teardown!")
    print("最后关闭浏览器")
@pytest.mark.usefixtures("open")
def test_search1():
    print("test_search1")
def test_search2(open):
    print("test_search2")
def test_search3(open):
    print("test_search3")

三、fixture与conftest.py结合使用
场景:协同开发时,共享配置
conftest.py文件:

import pytest
@pytest.fixture(scope="session")
def login():
    # setup操作
    print("用户需要先完成登录操作")
    username = "小明"
    bookname = "活着"
    yield username,name
    # teardown操作
    print("完成登出操作")

test_demo.py文件:

import pytest
def test_search(login):
    username,name = login
    print(f"用户名:{username},搜索商品:{bookname}")
def test_cart(login):
    print("添加购物车")
def test_order():
    print("下单《活着》")
def test_login(login):
    print("添加购物车之前先登录")
def test_get_product(connectDB):
    print("获取商品信息先连接数据库")
@pytest.fixture()
def connectDB():
    print("连接数据库")
    yield
    print("断开数据库")

四、fixture参数化

import pytest
@pytest.fixture(params=["小明", "小龙"])
def login(request):
    print(f"用户名:{request.param}")
    return request.param
def test_demo1(login):
    print(f"case 数据为: {login}")
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值