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

相关推荐
Sisphusssss2 小时前
香橙派5plus GPIO
linux·python·ubuntu
nbzy8883 小时前
企业资料证据可信度评分:区分自述字段、可核验材料与缺失信息
自动化·内容生成·锌合金压铸件
颜酱3 小时前
08 | 把维度值同步到 Elasticsearch(生成阶段)
人工智能·python·langchain
久久学姐3 小时前
基础转码学 AI:Java+Python 双语言入门,3 个月可落地实战项目
java·python·ai·转码·实战项目
ZHOU_WUYI3 小时前
4. light wam 模型loss计算过程
开发语言·人工智能·python
ji_shuke3 小时前
从零深入:基于 Playwright + Pytest + Allure 的企业级 Web 端到端自动化测试框架实战
前端·自动化测试·docker·jenkins·pytest·allure·playwright
nwsuaf_huasir3 小时前
【无标题】
python
Python私教5 小时前
Codex 写出的代码能跑却算错钱:我用 3 个测试拆穿一次 AI 编程幻觉
python·单元测试·ai编程
Python私教5 小时前
我只写了一个 add 工具,终于把 MCP 的 Host、Client、Server 跑明白了
python·ai编程·mcp
曦尧6 小时前
Pascal Editor:基于 React Three Fiber + WebGPU 的开源浏览器端 3D 建筑编辑器深度解析
ai·自动化