【问题标题】:Getting fixture not found in pytest在pytest中找不到夹具
【发布时间】:2019-02-28 11:46:47
【问题描述】:

我在使用以下代码运行 pytest 时遇到以下错误,我无法找出问题所在,请查找下面的代码 sn-ps。

控制台输出:

================================================= 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 ===============================================

我的基类包含以下代码:

from selenium import webdriver
import pytest
import unittest

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


@pytest.mark.usefixtures("web_driver")
class Base(unittest.TestCase):
    '''
    This fixture contains the set up and tear down code for each test.

    '''
    pass

并且测试类包含以下代码:

from core.web.Base import Base

class test(Base):

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

测试夹具是 web_driver 仍然找不到错误!

【问题讨论】:

    标签: python selenium pytest


    【解决方案1】:

    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(),而不是web_driver.close()

    【讨论】:

    • 解决方案有效,但为什么我们不能在同一个文件中使用它?
    • @user1323a 更新了我的答案。
    • 当我在两个类中扩展基类并尝试运行测试时使用此夹具运行测试时出现错误:无法建立新连接:[WinError 10061] 无法建立连接,因为目标机器主动拒绝它'但是在运行单个类时它运行良好是基类问题吗?
    • @user1323a 您并行运行测试?如果按预期进行,则需要使用一些并行机制(例如 Selenium Grid)
    • 链接“将其移至 conftest 文件”是 oudatet(链接的文档中不存在该部分)
    猜你喜欢
    • 1970-01-01
    • 2020-12-17
    • 2022-01-21
    • 2016-07-27
    • 1970-01-01
    • 2023-01-25
    • 2021-08-30
    • 2021-10-01
    • 1970-01-01
    相关资源
    最近更新 更多