接口自动化框架搭建(四):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,执行结果

相关推荐
艺杯羹5 分钟前
MCP本地服务集成避坑:从Stdio管道僵死到双向JSON-RPC稳健通信
人工智能·python·ai·系统架构·大语言模型·ai编程·mcp
happylifetree9 分钟前
Python78:核心语法-面向对象基础-类与对象
python
duansamve15 分钟前
Python中进程
python
日日大王学编程1 小时前
AI Agent 技术发展历程、演进逻辑与技术范式综述
人工智能·python
春涧草茶1 小时前
慢就是快16-1协程
开发语言·前端·python
5233AI开发笔记2 小时前
API 返回 429:先区分限速、并发和配额,再决定是否重试
python
databook2 小时前
经常听到的透视表是干什么的?
python·数据分析·pandas
特立独行的猫A2 小时前
我用仓颉写了个多平台发文工具:一份 Markdown,一条命令发六个平台
java·后端·python
陆陆陆六2 小时前
# 五个AI引擎的答案采集:反爬、去重与时间窗对齐的三个工程细节
python·net·ai答案采集
东莞市壹星自动化设备有限公司4 小时前
透明圆瓶内部特征怎样定位:折射追踪与近远相位分支
嵌入式硬件·自动化·需求分析·几何学·骨骼绑定