【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
相关推荐
测试开发Kevin21 小时前
从投入产出、效率、上手难易度等角度综合对比 pytest 和 unittest 框架
python·pytest
测试开发Kevin3 天前
以pytest_addoption 为例,讲解pytest框架中钩子函数的应用
python·pytest
川石教育9 天前
Pytest中的fixture装饰器详解
python自动化测试·pytest·pytest自动化测试框架·pytest测试框架·pytest单元测试框架
春风又。9 天前
接口自动化——参数化
python·测试工具·自动化·pytest
XTY0011 天前
mac电脑pytest生成测试报告
pytest
程序员的世界你不懂11 天前
pytest-前后置及fixture运用
pytest
天才测试猿13 天前
基于Pytest接口自动化的requests模块项目实战以及接口关联方法详解
自动化测试·软件测试·python·测试工具·单元测试·测试用例·pytest
HtwHUAT14 天前
五、UI自动化测试05--PyTest框架
经验分享·python·ui·pytest
程序员的世界你不懂14 天前
Pytest-mark使用详解(跳过、标记、参数 化)
pytest
fish_study_csdn16 天前
pytest 技术总结
开发语言·python·pytest