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

相关推荐
阿童木写作15 分钟前
跨马翻译:一站式图片视频智能处理,提升跨境电商工作效率
人工智能·python·音视频
Python图像识别-119 分钟前
基于yolov8的钢铁缺陷检测系统-2027毕业版(UI界面+Python项目源码+模型+标注好的数据集)
开发语言·python·yolo
aqi0030 分钟前
15天学会AI应用开发(十五)使用LangChain封装AI执行链
人工智能·python·大模型·ai编程·ai应用
出门吃三碗饭1 小时前
如何从0到1构建自动化的咖啡和冰激凌机器人?
运维·机器人·自动化
曦尧1 小时前
AWS 官方 AI 编码 Agent 工具包:Agent Toolkit for AWS 深度解读
ai·自动化
kisbad1 小时前
Day 012|Embedding 和向量数据库:知识库检索到底在检什么
数据库·python·embedding·agent
va学弟1 小时前
LeetCode 热题 100 题解(1):哈希
python·算法·leetcode·哈希算法
言乐61 小时前
Python实现基本搜索引擎
java·linux·开发语言·python·搜索引擎
FriendshipT1 小时前
Ultralytics:解读C3k2模块
人工智能·pytorch·python·深度学习·目标检测