接口自动化框架搭建(四):pytest的使用

1,使用说明

网上资料比较多,我这边就简单写下

1,目录结构

2,test_1.py创建两条测试用例

python 复制代码
def test_1():
    print('test1')

def test_2():
    print('test2')

3,在pycharm中执行

4,执行结果:

2,使用命令行执行

python 复制代码
pytest test_1.py 

3,通过代码执行(所有用例)

在run.py中创建执行函数

python 复制代码
import pytest

def run():
	#    pytest.main(['-v', './cases'])  -v打印执行结果
    pytest.main(['./cases']) # 执行cases文件下所有测试用例

if __name__ == '__main__':
    run()

执行结果:

4,通过代码执行(指定单条用例)

1,创建test_2.py文件,便于区分,目录如下

2,test2_py文件代码如下:

python 复制代码
def test_2_1():
    print('test_2_1')

def test_2_2():
    print('test_2_2')

3,执行

python 复制代码
import pytest

def run():
	# 指定执行test_2.py文件的用例执行
    pytest.main(['-v', '-k', 'test_2.py'])

if __name__ == '__main__':
    run()

4,执行结果:

5,通过代码执行(指定多条用例)

1,执行

python 复制代码
import pytest

def run():
	# 文件使用or连接
    pytest.main(['-v', '-k', 'test_2.py or test_1.py'])

if __name__ == '__main__':
    run()

2,执行结果

相关推荐
*neverGiveUp*11 分钟前
Python基础语法
开发语言·python
Marst Code16 分钟前
[特殊字符] 五大 Workflow 模式详解
人工智能·python
小白学大数据22 分钟前
爬虫优化:Python 剔除无效超时代理实操
服务器·爬虫·python
lianyinghhh34 分钟前
FlowGame 从零上手:开源 AI 工作流编排框架与 Vue 3 接入实战
python·低代码·开源·vue·rag·flowgame·ai工作流编排
搞科研的小刘选手36 分钟前
【西安交通大学主办】第六届人工智能、自动化与高性能计算国际会议 (AIAHPC 2026)
网络·人工智能·机器学习·数据挖掘·自动化·云计算·并行式
玫幽倩38 分钟前
2026盘古石取证决赛(APK取证)
数据库·python·电子取证·aes·隐藏·笔记软件·手机取证
Dream_ksw38 分钟前
Python多继承之super()继承问题解决
开发语言·python
装不满的克莱因瓶1 小时前
基于 Python 进行二维空间线性可分数据单/多层感知器实战
人工智能·python·深度学习·神经网络·ai·卷积
2601_950368911 小时前
稀土合金粉末采购指南:3步筛选靠谱镁钆供应商
大数据·运维·人工智能·python
至天1 小时前
FastAPI 接入 FastAPI-Limiter 以及使用 Redis 进行限流指南
redis·python·fastapi·请求限流