我正在使用pytest针进行测试。在conftest.py中,我创建了一个具有scope =“ class”的灯具。在此固定装置中,我将使用needle.driver创建浏览器实例,并使用请求对象尝试在其他测试类中使用它。
这适用于Python 3.7.4,needle版本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)