在pytes中找不到Getting fixture

2024-10-01 15:31:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我在使用以下代码运行pytest时遇到以下错误im无法找出错误,请查找下面的代码片段。在

控制台输出:

================================================= test session starts =================================================
platform win32 -- Python 3.7.2, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: D:\Workspace\AutomationProject, inifile:
plugins: cov-2.6.1, allure-pytest-2.5.5
collected 1 item

tests\pages\test.py E                                                                                            [100%]

======================================================= ERRORS ========================================================
__________________________________________ ERROR at setup of test.test_test ___________________________________________
file D:\Workspace\AutomationProject\tests\pages\test.py, line 5
      def test_test(self):
E       fixture 'web_driver' not found
>       available fixtures: _UnitTestCase__pytest_class_setup, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

D:\Workspace\AutomationProject\tests\pages\test.py:5
=============================================== 1 error in 0.12 seconds ===============================================

我的基类包含以下代码:

^{pr2}$

测试类包含以下代码:

from core.web.Base import Base

class test(Base):

    def test_test(self):
        self.driver.get("http://google.com")

测试夹具是网页驱动仍然得到错误没有找到!在


Tags: 代码pytestselfwebbasepytestdef
1条回答
网友
1楼 · 发布于 2024-10-01 15:31:03

web_driver()是在Base类范围之外定义的,因此它对usefixtures不可见,因为它是test类作用域的一部分。你可以move it to conftest file,但是更好的解决方案是将web_driver移到Base

@pytest.mark.usefixtures("web_driver")
class Base(unittest.TestCase):

    @pytest.fixture(scope="class")
    def web_driver(self, request):
        driver = webdriver.Chrome("C:/chromedriver.exe")
        request.cls.driver = driver
        yield
        driver.close()

作为旁注,它应该是driver.close(),而不是{}

相关问题 更多 >

    热门问题