Getting fixture not found in pytest

14,592

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()

Share:
14,592
user1323a
Author by

user1323a

I'm working at SDET at software company in Pune and I love python.

Updated on June 26, 2022

Comments

  • user1323a
    user1323a almost 2 years

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

    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 !

  • user1323a
    user1323a about 5 years
    Solution worked but why cant we use outside the class it in same file it ?
  • Guy
    Guy about 5 years
    @user1323a Updated my answer.
  • user1323a
    user1323a about 5 years
    when Im running tests using this fixture when extending base class in two classes and trying to run test im getting error :Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it' but when running single class it runs fine is it a base class issue ?
  • Guy
    Guy about 5 years
    @user1323a You run the tests in parallel? If you do it's expected, you need to use some parallelism mechanize (like Selenium Grid for example)
  • user1323a
    user1323a about 5 years
    issue gets resolved when i comment out driver.close line and it runs. I think problem is it driver gets closed and then the call is made to driver but how can i achive this without commenting out the driver.close becouse it uses same driver for all the tests how can i open browser for every test i tried changing scope
  • Guy
    Guy about 5 years
    @user1323a I couldn't reproduce. Open a new question with the code of the base class and the tests you are trying to run, it's hard to solve this in the comments.
  • yankee
    yankee over 2 years
    The link "move it to conftest file" ist oudatet (section does not exist in the document linked)
  • Guy
    Guy over 2 years
    @yankee updated, thanks.