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

相关推荐
Le_ee28 分钟前
科来流量分析/结合MCP自动化
运维·自动化
盼小辉丶33 分钟前
PyTorch强化学习实战——融合人类示范数据的高效强化学习
人工智能·pytorch·python·深度学习·强化学习
聪明蛋子哟1 小时前
MCP协议深度落地:如何用Python/Java双语言实现统一的工具调用网关?
java·开发语言·python
李妍.1 小时前
PyTorch GPU 版安装记录(RTX 5060 + Python 3.14)
人工智能·pytorch·python
90后的晨仔1 小时前
Python 基础语法完整汇总
python
广慈新医知1 小时前
当健康问题越来越复杂,AI更适合先帮我们“把问题问清楚”
人工智能·python
一晌小贪欢1 小时前
python-第27天:Python面向对象详解
开发语言·python·数据可视化·面向对象·python办公
青 春 记 忆1 小时前
LeetCode 350. 两个数组的交集 II|Python 解法详解
python·算法·leetcode
2601_962387821 小时前
web自动化测试实战教程【selenium/unittest/pytest】【共193课
selenium·pytest·devops·web自动化测试·unittest
智购科技自动售卖机厂家3 小时前
2026自动售货机库存热力图分析:从设备分布到补货优先级的数据可视化实践~YH
大数据·python·信息可视化