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

相关推荐
ikun_文2 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes2 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明2 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn2 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困2 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao4 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼4 小时前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君4 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python
lupai15 小时前
手机在网状态接口实测效果与质量评估
大数据·python·智能手机·api接口
天远Date Lab15 小时前
零信任架构实战:基于天远双人婚姻评估查询构建自动化房产按揭联合审查网关
运维·人工智能·架构·自动化