【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
相关推荐
小熊出擊1 天前
【pytest】finalizer 执行顺序:FILO 原则
python·测试工具·单元测试·pytest
小熊出擊2 天前
【pytest】使用 marker 向 fixture 传递数据
python·pytest
wa的一声哭了2 天前
Deep Learning Optimizer | Adam、AdamW
人工智能·深度学习·神经网络·机器学习·自然语言处理·transformer·pytest
专职4 天前
pytest生成测试用例,allure生成测试报告
测试用例·pytest
小丁爱养花7 天前
接口自动化测试 - pytest [1]
python·自动化·pytest
向上的车轮8 天前
如何用AI工具开发一个轻量化CRM系统(七):AI生成pytest测试脚本
pytest
慌糖9 天前
自动化接口框架搭建分享-pytest第三部分
运维·自动化·pytest
Script kid9 天前
Pytest框架速成
数据库·pytest
小熊出擊9 天前
[pytest] 一文掌握 fixture 的作用域(scope)机制
python·功能测试·单元测试·自动化·pytest
小熊出擊10 天前
[pytest] autouse 参数:自动使用fixture
python·测试工具·单元测试·自动化·pytest