【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
相关推荐
姚青&11 天前
PyCharm 配置与界面化运行
pytest
姚青&11 天前
Pytest 测试用例断言
测试用例·pytest
姚青&11 天前
Pytest 命名规则
pytest
姚青&11 天前
Pytest 测试用例结构
测试用例·pytest
姚青&11 天前
Pytest 简介、安装与环境准备
pytest
测试渣12 天前
持续集成中的自动化测试框架优化实战指南
python·ci/cd·单元测试·自动化·pytest
小凯1234513 天前
pytest框架-详解(学习pytest框架这一篇就够了)
python·学习·pytest
逻极13 天前
pytest 入门指南:Python 测试框架从零到一(2025 实战版)
开发语言·python·pytest