Python 如何在其他测试类中使用conftest中定义的针对象?

Python 如何在其他测试类中使用conftest中定义的针对象?,python,python-3.x,selenium-webdriver,pytest,Python,Python 3.x,Selenium Webdriver,Pytest,我使用pytest针进行测试。在conftest.py中,我创建了一个具有scope=“class”的夹具。在这个fixture中,我使用needle.driver创建了一个浏览器实例,并使用request对象尝试在其他测试类中使用它 这适用于Python3.7.4、Pinele版本0.3.11、pytest版本4.6.5 #content of conftest.py import pytest @pytest.fixture(scope="class") def setup(needle

我使用pytest针进行测试。在conftest.py中,我创建了一个具有scope=“class”的夹具。在这个fixture中,我使用needle.driver创建了一个浏览器实例,并使用request对象尝试在其他测试类中使用它

这适用于Python3.7.4、Pinele版本0.3.11、pytest版本4.6.5

#content of conftest.py
import  pytest

@pytest.fixture(scope="class")
def setup(needle , request):
    needle.driver.get('https://www.google.com')
    request.cls.driver=needle.driver

---------------------------------------------------------------

#content of test_case1.py
from selenium.webdriver.common.by import By
from selenium import webdriver
import os
from selenium.webdriver.common.keys import Keys
import pytest
import unittest

@pytest.mark.usefixtures("setup")
class test(unittest.TestCase):
   def test_example_element(self):
      self.driver.find_element_by_name("q").send_keys("Selenium")
      self.driver.assert_screenshot('D:\\Screenshot\\XPYgdg', (By.NAME, 'q'))
当我使用命令py.test--driver Chrome test_case1.py运行test_case1.py时,我得到以下错误:

ScopeMismatch: You tried to access the 'function' scoped fixture 'needle' with a 'class' scoped request object, involved factories
conftest.py:3:  def setup(needle, request)

pytest-needle
库中的
needle
夹具具有函数作用域(),因此不能在类作用域夹具中使用该函数。不过,您可以实现自己的类范围版本的
pineel
fixture。您还可以通过自动使用fixture将
pineel
fixture注入测试类。查看一个关于如何做到这一点的示例。谢谢@hoefling的建议。经过一些研究,我找到了一个解决方案。有关更多信息,请参阅link()。
pytest-needle
库中的
needle
fixture具有函数作用域(),因此不能在类作用域fixture中使用。不过,您可以实现自己的类范围版本的
pineel
fixture。您还可以通过自动使用fixture将
pineel
fixture注入测试类。查看一个关于如何做到这一点的示例。谢谢@hoefling的建议。经过一些研究,我找到了一个解决方案。有关更多信息,请参阅link()。