Skip to content
Advertisement

Getting fixture not found in pytest

I am getting following error while running pytest using following code im unable to figure out whats wrong please find below code snippets.

Console ouput :

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

testspagestest.py E                                                                                            [100%]

======================================================= ERRORS ========================================================
__________________________________________ ERROR at setup of test.test_test ___________________________________________
file D:WorkspaceAutomationProjecttestspagestest.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:WorkspaceAutomationProjecttestspagestest.py:5
=============================================== 1 error in 0.12 seconds ===============================================

My Base class contains following code:

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

and test class contains following code:

from core.web.Base import Base

class test(Base):

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

The test fixture is web_driver still getting error not found !

Advertisement

Answer

web_driver() is defined outside Base class scope, so it’s invisible to the usefixtures as it is part of test class scope. You could move it to conftest file, but IMHO a better solution is to move web_driver inside 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()

As a side note, it should be driver.close(), not web_driver.close()

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement