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

相关推荐
阿捞2几秒前
python-langchain框架(3-20-智能问答ZeroShot_ReAct Agent 从零搭建)
python·react.js·langchain
数据知道1 分钟前
claw-code 源码分析:从 REPL 到服务端——CLI / HTTP(SSE) / LSP 多入口如何共享同一颗 runtime 心?
python·网络协议·http·ai·里氏替换原则·claude code
不解不惑3 分钟前
gemma4 实现ASR语音识别
人工智能·python·语音识别
来自远方的老作者4 分钟前
第8章 流程控制-8.2 选择结构
开发语言·python·选择结构
kaico20185 分钟前
python常用标准库
开发语言·python
TTGGGFF5 分钟前
SnapTranslate 2.0:轻量级全场景划词翻译——添加生词本以及生词本复习AI助手功能!
python·划词翻译·git开源
杜子不疼.7 分钟前
Python + Selenium + AI 智能爬虫:自动识别反爬与数据提取
人工智能·python·selenium
财经资讯数据_灵砚智能9 分钟前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年4月8日
大数据·人工智能·python·信息可视化·自然语言处理
WangJunXiang615 分钟前
初识Flask框架
后端·python·flask
郝学胜-神的一滴15 分钟前
玩转PyTorch:detach函数破解自动微分张量转numpy难题
人工智能·pytorch·python·深度学习·机器学习·numpy