【pytest官方文档学习】覆盖全局变量的示例代码在8.2.x版本运行报错

我们经常会在conftest.py中定义一些fixture提供给测试方法使用,但是在有些测试中,我们用不上或者需要对其结果进行处理后再用,这时候,就需要我们对fixture进行覆盖或重写。

在学习pytest-8.2.x官方文档看到了它给出的覆盖全局fixture的示例如下:

python 复制代码
tests/
    conftest.py
        # content of tests/conftest.py
        import pytest

        @pytest.fixture
        def username():
            return 'username'

    test_something.py
        # content of tests/test_something.py
        def test_username(username):
            assert username == 'username'

    subfolder/
        conftest.py
            # content of tests/subfolder/conftest.py
            import pytest

            @pytest.fixture
            def username(username):
                return 'overridden-' + username

        test_something_else.py
            # content of tests/subfolder/test_something_else.py
            def test_username(username):
                assert username == 'overridden-username'

我们可以看到,在subfolder/conftest.py文件中重写的"username " fixture访问了tests/conftest.py 文件中的"username "。

这种用法官方文档中也给出了解释:"Note that the base or super fixture can be accessed from the overriding fixture easily - used in the example above."说明官方是也是支持这么用的。

但是当我在pycharm中运行的时候,却报错了:

百思不得其解的时候,在网上看到了这位博主的文章【pytest-fixture】十一、覆盖各个级别的fixture_fixture级别-CSDN博客,发现他的运行是成功的,仔细看了他的运行环境,发现pytest版本是6.2.5。莫非是pytest版本问题?

于是,我将环境也换成了他的版本,运行结果真的时PASSED。

这... 嗯...问题记录下来吧~~

相关推荐
小小张说故事1 分钟前
pytest 入门指南:Python 自动化测试的标配工具
python·机器学习
用户8356290780511 分钟前
使用 Python 在 Excel 中添加和管理图片
后端·python
用户3169353811832 分钟前
SQLAlchemy 的两套 API
python
伞伞悦读2 分钟前
【第45期】一行 eval 做计算器的代价:Python 输入校验和可测试的猜数字
python
用户8356290780513 分钟前
Python 设置 Excel 单元格边框与样式
后端·python
10年前端老司机4 分钟前
LLM降本提速三档对比:无缓存、普通缓存、语义缓存(LangChain生产落地)
人工智能·python·langchain
用户298698530145 分钟前
Python 文档格式转换实战:RTF 转 PDF 与 HTML
python·html·api
天天被压力6 分钟前
【Python 量化取数指南 #09】Python 拿港股通数据:港股通成交与港股财报实测
java·人工智能·python
杨杨杨大侠7 分钟前
给 Agent 加一个“判断器”:聊聊 Laya、Jev,以及怎么部署和选择
人工智能·python·agent
SL_staff7 分钟前
四维穿透式齐套判断:JVS-APS在制造计划系统中的技术实现
java·spring boot·python