【pytest】conftest.py使用

  1. 创建test_project 目录

test_project/sub/test_sub.py

复制代码
def test_baidu(test_url):

    print(f'sub ={test_url}')

test_project/conftest.py 设置钩子函数 只对当前目录 和子目录起作用

复制代码
import pytest
#设置测试钩子函数
@pytest.fixture()
def test_url():
    return  "https://www.baidu.com"

test_project/test_demo.py

复制代码
def test_baidu(test_url):
    print(test_url)

运行

复制代码
 pytest -s -v ./test_project

或 main 运行

复制代码
import pytest

if __name__=='__main__':
    #pytest.main(['-s','./fixture'])
    #pytest.main(['-v', './fixture','--junit-xml=./report/log.xml'])
    #pytest.main(['-v', './fixture', '--pastebin=all'])
    pytest.main(['-v','-s' ,'./test_project', '--pastebin=all'])

plugins: anyio-3.5.0
collecting ... collected 2 items

test_project/test_demo.py::test_baidu https://www.baidu.com
PASSED
test_project/sub/test_sub.py::test_baidu sub =https://www.baidu.com
PASSED

============================== 2 passed in 0.05s ==============================
==================== Sending information to Paste Service =====================
pastebin session-log: https://bpa.st/show/H4UQ
相关推荐
2601_962077601 天前
从零搭建Python接口自动化测试框架:pytest+requests实战指南
python·pytest·接口自动化·requests·框架搭建
weixin_440730501 天前
pytest结合allure生成html测试报告(step、story、severity、screenshot)
前端·html·pytest·allure报告
拜托多多指教1 天前
【Pytest框架学习】分层架构
python·pytest
2601_962299881 天前
python脚本如何单元测试
python·单元测试·pytest·unittest·mock对象
Tizzy JJ2 天前
Python + pytest 接口自动化测试框架实战:从零搭建企业级项目骨架
开发语言·python·pytest
Tizzy JJ2 天前
接口自动化测试实战:如何围绕真实业务场景设计高质量用例
python·自动化·pytest
测试老哥3 天前
Pytest 之assert断言的使用
自动化测试·软件测试·python·测试工具·测试用例·pytest·接口测试
边吃番茄边敲代码7 天前
从本地工具到 MCP:给 Agent 接入独立工具服务,并补齐自动化测试
人工智能·python·功能测试·ai·单元测试·pytest
SugarAbsinthe7 天前
【Agent开发实习小记】从人工逐条验证到自动化验收:新 API 接入前的效率瓶颈
人工智能·python·自动化·pytest