最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

automation - Using conftest python file, but unable to access driver and driver method in different python file at class level -

programmeradmin0浏览0评论

I have created conftest.py file but I am not able to access driver and driver methods to different class of another file. please let me know what I am missing here, as I am new to python

conftest.py

import pytest
from selenium import webdriver

@pytest.fixture
def setup(request):
    driver = webdriver.Chrome()
    driver.get(";)
    request.cls.driver = driver
    yield driver
    driver.quit()

test_login.py

import pytest
from testcases.conftest import setup


@pytest.mark.usefixtures("setup")
class VerifyLoginTest:

    def test_login(self):
        self.

Here above not getting self.driver in Class VerifyLoginTest inside def test_login method i am supposed to have self.driver after writing self. but not getting any suggestion for driver also not it's method.

I have created conftest.py file but I am not able to access driver and driver methods to different class of another file. please let me know what I am missing here, as I am new to python

conftest.py

import pytest
from selenium import webdriver

@pytest.fixture
def setup(request):
    driver = webdriver.Chrome()
    driver.get("https://dev-web-azamtv.videoready.tv")
    request.cls.driver = driver
    yield driver
    driver.quit()

test_login.py

import pytest
from testcases.conftest import setup


@pytest.mark.usefixtures("setup")
class VerifyLoginTest:

    def test_login(self):
        self.

Here above not getting self.driver in Class VerifyLoginTest inside def test_login method i am supposed to have self.driver after writing self. but not getting any suggestion for driver also not it's method.

Share Improve this question asked yesterday Nihit GargNihit Garg 1 New contributor Nihit Garg is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • Use driver as an argument the usual way fixtures are used instead of assigning it to request.cls (which will not make it an attribute of the test class). – MrBean Bremen Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

By copy pasting your code in a file named test_driver.py:

from selenium import webdriver
import pytest


@pytest.fixture
def setup(request):
    driver = webdriver.Chrome()
    driver.get("https://dev-web-azamtv.videoready.tv")
    request.cls.driver = driver
    yield driver
    driver.quit()


@pytest.mark.usefixtures("setup")
class VerifyLoginTest:

    def test_login(self):
        self.driver

when running it with pytest: pytest test_driver.py I get the following content:

====================== test session starts ================================
platform linux -- Python 3.13.2, pytest-8.3.4, pluggy-1.5.0
collected 0 items 

When adding the inheritance on the VerifyLoginTest class, tests can run correctly (even if it doesn't check anything):

import unittest

from selenium import webdriver
import pytest


@pytest.fixture
def setup(request):
    driver = webdriver.Chrome()
    driver.get("https://dev-web-azamtv.videoready.tv")
    request.cls.driver = driver
    yield driver
    driver.quit()


@pytest.mark.usefixtures("setup")
class VerifyLoginTest(unittest.TestCase):

    def test_login(self):
        self.driver

and then the run gives:

======================== test session starts =========================
platform linux -- Python 3.13.2, pytest-8.3.4, pluggy-1.5.0
collected 1 item                                                                                                                                   

test_driver.py .                                                                                                [100%]

======================= 1 passed in 5.90s ============================

Concerning the completion, I don't know why it's not working in Pycharm. In my opinion, it's not a good pytest practice to mix unittest style and fixtures. I would rewrite your code like this:

from selenium import webdriver
import pytest


@pytest.fixture
def driver(request):
    driver = webdriver.Chrome()
    driver.get("https://dev-web-azamtv.videoready.tv")
    yield driver
    driver.quit()



def test_login(driver):
    driver

like this, you would have the auto completion on driver variable (I rename the fixture).

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论