【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
相关推荐
St_rive17 小时前
Pytest skip&&skipif跳过⽤例
运维·服务器·pytest
努力搬砖的咸鱼4 天前
AI Agent测试全景图:它到底改变了什么
人工智能·python·ai·集成测试·pytest·agent·ai编程
X1A0RAN5 天前
自动化流水线提效·微观篇:pytest 执行顺序优化
自动化·pytest
久久学姐12 天前
Python+Playwright+Pytest+BDD,用FSM打造高效测试框架
python·pytest·bdd·playwright·fsm
小白上线*^_^*13 天前
Pytest 自动化测试框架速通指南(二)
软件测试·pytest·python测试框架·ai测试基础
Logintern0914 天前
pytest的AST 断言重写
pytest
Tinyfacture15 天前
接口自动化之添加商品(pytest)
python·测试工具·自动化·pytest
ji_shuke16 天前
从零深入:基于 Playwright + Pytest + Allure 的企业级 Web 端到端自动化测试框架实战
前端·自动化测试·docker·jenkins·pytest·allure·playwright
祉猷并茂,雯华若锦18 天前
Selenium+Pytest自动化测试框架实战
selenium·测试工具·pytest
祉猷并茂,雯华若锦19 天前
Pytest+Selenium自动化测试:失败自动截图集成Allure报告
selenium·测试工具·pytest