You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行pytest时出现fixture 'setup'未找到错误,求排查解决

pytest 找不到setup Fixture问题排查与解决

问题现象

运行pytest执行自动化测试时,出现fixture 'setup' not found错误,相关控制台输出及代码如下:

控制台错误输出

PS C:\Bhargav\Python Learning\Projects\MBDailyBoost> pytest
================================================================================= test session starts =================================================================================
platform win32 -- Python 3.10.2, pytest-7.1.2, pluggy-1.0.0
rootdir: C:\Bhargav\Python Learning\Projects\MBDailyBoost
plugins: allure-pytest-2.9.45, html-3.1.1, metadata-2.0.2
collected 2 items
E       fixture 'setup' not found
>       available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, extra, include_metadata_in_junit_xml, metadata, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

C:\Bhargav\Python Learning\Projects\MBDailyBoost\testCases\test_login.py:22
=============================================================================== short test summary info =============================================================================== 
ERROR testCases/test_login.py::Test_001_Login::test_homePageTitle
ERROR testCases/test_login.py::Test_001_Login::test_Login

相关代码

conftest.py

import pytest
from selenium import webdriver

@pytest.fixture(scope="class")
def setup():
    global driver
    # driver = webdriver.Chrome(executable_path="C:\\chromedriver.exe")
    driver = webdriver.Chrome()
    driver.maximize_window()

    return driver

test_login.py

import time
from pageObjects.LoginPageGMB import LoginPage

class Test_001_Login:
    baseURL = ""
    usename = ""
    password = ""

    def test_homePageTitle(self, setup):
        self.driver = setup
        self.driver.get(self.baseURL)
        self.driver.maximize_window()
        actual_title = self.driver.title
        if actual_title == "Google Business Profile – Get Listed on Google":
            assert True
        else:
            assert False

问题原因

  1. conftest.py位置错误:pytest仅自动加载测试根目录(即运行pytest命令的目录,此处为MBDailyBoost)或测试用例所在子目录(testCases)下的conftest.py文件,若文件不在这两个位置,无法识别其中定义的fixture。
  2. 文件名拼写错误:若文件名写成conf_test.py或其他变体,pytest不会识别为全局fixture配置文件。
  3. 缓存干扰:pytest的缓存目录可能残留旧配置,导致新定义的fixture未被加载。

解决办法

1. 调整conftest.py位置

将conftest.py移至以下任一位置:

  • 项目根目录(C:\Bhargav\Python Learning\Projects\MBDailyBoost):所有子目录的测试用例均可共享该fixture;
  • testCases目录下:仅该目录内的测试用例可使用该fixture。

2. 检查文件名拼写

确保文件名是全小写的conftest.py,无拼写错误。

3. 清除pytest缓存

若上述方法无效,删除缓存目录后重新运行:

# Windows PowerShell命令
Remove-Item -Recurse -Force .pytest_cache

再执行测试:

pytest

额外优化建议

去掉setup fixture中的global driver,改用yield实现teardown逻辑,自动回收浏览器资源:

import pytest
from selenium import webdriver

@pytest.fixture(scope="class")
def setup():
    driver = webdriver.Chrome()
    driver.maximize_window()
    yield driver  # 返回driver给测试用例
    driver.quit()  # 测试结束后自动关闭浏览器

内容的提问来源于stack exchange,提问作者Bhargav Zilpilwar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 05:06:34