【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
相关推荐
摘星编程4 天前
AI 帮我写单测:pytest 覆盖率提升 40% 的协作日志
人工智能·pytest·测试驱动开发·代码覆盖率·ai协作开发
lucia_zl6 天前
pytest并发测试,资源问题导致用例失败解决办法
pytest
鱼鱼说测试6 天前
Selenium4+Pytest自动化测试框架实战
pytest
XYiFfang12 天前
【Pytest】解决Pytest中Teardown钩子的TypeError:实例方法与类方法的调用差异
python·pytest
Kingairy14 天前
Pytest 插件:pytest_runtest_protocol
python·pytest
AIZHINAN14 天前
Pytest 插件介绍和开发
测试工具·pytest·插件开发
灰阳阳16 天前
替身演员的艺术:pytest-mock 从入门到飙戏
自动化测试·python·pytest·unit testing·pytest-mock
年年测试20 天前
Playwright与PyTest结合指南
pytest
focksorCr20 天前
pytest 并发执行用例(基于受限的测试资源)
python·pytest