pythonpycharmpytest-fixtures

Getting attribute error in Python when trying to use the fixture at class level


I am using Selenium Python with Pytest framework and using conftest.py file where I have written a class level fixture

These are the plugins used. I am using Pycharm editor

Conftest.py file

@pytest.fixture(scope='class')

def driverObjectClass1(request):

    global driver

    sys.path.insert(0, os.getcwd())

    browser = option.browser

    chromeOpts = chrome_options()

    firefoxOpts = firefox_options()

    if option.headless:

        chromeOpts.add_argument("--headless")

        firefoxOpts.add_argument("--headless")

    if browser in 'firefox':

        driver = webdriver.Firefox(options=firefoxOpts)

    else:

        driver = webdriver.Chrome(options=chromeOpts)

    driver.maximize_window()

    driver.implicitly_wait(20)

    request.cls.driver = driver

    config = load_config()

    logFolder = log_Folder(config['clientID'])

    yield request.cls.driver, config, logFolder

    sleep(2)

    request.cls.driver.quit()

My class test_login.py

import pytest
from selenium.webdriver.common.by import By


from Pages.page_common import Common

@pytest.mark.usefixtures("driverObjectClass1")

class TestLogin1:


    def test_valid_login(self):

        driver = self.driver

        config = self.config

        logFolder = self.logFolder

        common = Common(driver)

        try:

            url = config['URL']

            clientId = config['clientID']

            username = config['username']

            password = config['password']

            app1 = config['app']

            branch = config['branch']

            print(clientId, username, password, app1, branch)

            common.SCHXPLogin(url, clientId, username, password)

            common.change_branch(branch)


        except Exception as e:

            common.logFailure("test_valid_login", logFolder, e)

getting this error/warning when applying the fixture like this. How to fetch the yield objects in the class where the fixture is applied?

Attribute Error

How to resolve this issue?

When I run the test, I get the following error

FAILED test_cases/test_login1.py::TestLogin1::test_valid_login - AttributeError: 'TestLogin1' object has no attribute 'config'

I tried searching a solution but couldn't find the answer. So I decided to ask here.


Solution

  • I updated the Fixture in Conftest.py file as below. Now I can run my tests without error. But Pycharm Editor still showing the attribute reference warning. But when uploaded the code in VS code editor, there is no such warning. So could be specific to Pycharm Editor.

    @pytest.fixture(scope='class')
    
def driverObjectClass1(request):
    
    global driver
    
    sys.path.insert(0, os.getcwd())
    
    browser = option.browser
    
    chromeOpts = chrome_options()
    
    firefoxOpts = firefox_options()
    
    if option.headless:
    
        chromeOpts.add_argument("--headless")
    
        firefoxOpts.add_argument("--headless")
    
    if browser in 'firefox':
    
        driver = webdriver.Firefox(options=firefoxOpts)
    
    else:
    
        driver = webdriver.Chrome(options=chromeOpts)
    
    driver.maximize_window()
    
    driver.implicitly_wait(20)
    
    request.cls.driver = driver
    
    config = load_config()
    
    logFolder = log_Folder(config['clientID'])
    
    request.cls.config = config
    
    request.cls.logFolder = logFolder
    
    yield request.cls.driver, request.cls.config, request.cls.logFolder
    
    sleep(2)
    
    request.cls.driver.quit()