Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python pytest,使用teardown quit为每个测试文件初始化webdriver_Python_Testing_Webdriver_Pytest_Fixtures - Fatal编程技术网

Python pytest,使用teardown quit为每个测试文件初始化webdriver

Python pytest,使用teardown quit为每个测试文件初始化webdriver,python,testing,webdriver,pytest,fixtures,Python,Testing,Webdriver,Pytest,Fixtures,测试模块中代码的当前部分: def test_01(): driver.get('https://www.google.com') import pytest from selenium import webdriver from selenium.webdriver.support.ui import WebDriverWait @pytest.fixture(autouse=True) def browser(): driver = webdriver.Chrome(e

测试模块中代码的当前部分:

def test_01():
    driver.get('https://www.google.com')
import pytest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait


@pytest.fixture(autouse=True)
def browser():
    driver = webdriver.Chrome(executable_path=r"C:\webdrivers\1\chromedriver.exe")
    driver.implicitly_wait(5)
    wait = WebDriverWait(driver, 10)
    driver.maximize_window()
    yield
    driver.quit()
确认测试代码:

def test_01():
    driver.get('https://www.google.com')
import pytest
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait


@pytest.fixture(autouse=True)
def browser():
    driver = webdriver.Chrome(executable_path=r"C:\webdrivers\1\chromedriver.exe")
    driver.implicitly_wait(5)
    wait = WebDriverWait(driver, 10)
    driver.maximize_window()
    yield
    driver.quit()
结果:“E名称错误:未定义名称“驱动程序”

目标结果:在不使用类的情况下初始化webdriver,将webdriver设置为每个测试函数的驱动程序,使用它运行函数,并使用conftest中的fixtures postcondition退出。我有很多测试文件,这就是为什么我要做一次


我也尝试过从fixture返回变量,但据我所知,测试函数仍然需要fixture的变量,这对我来说是错误的。例如:fixture-returnx,testfunction(fixture):x=fixture。而且它仍然不能与webdriver\driver一起使用(或者说我没有弄清楚)。

您的测试函数需要将fixture作为参数,这是问题的第一部分

例如:

def测试_01(驾驶员):
司机,上车https://www.google.com')
但是您还没有
驱动程序
夹具,只有一个名为
浏览器
,因此您需要更改夹具的名称:

@pytest.fixture(autouse=True)
def驱动程序(请求):
...
最后,夹具需要返回驱动程序,以便您可以使用它

@pytest.fixture(autouse=True)
def驱动程序(请求):
...
屈服驱动力
...

非常感谢您,先生,它很有效!你们能澄清一下吗?若我使用autouse参数,为什么我要将参数链接到我的测试函数?我相信,
autouse
可以确保在每次测试之前调用夹具,例如,如果您想在每次测试之前进行一些常见的设置。我认为它的目的是主要用于夹具不返回任何东西的地方。如果夹具确实返回了一些东西,那么您的测试需要一种方法来访问返回的东西。有几种方法可以做到这一点:1。将结果添加到全局命名空间2。将结果作为参数传递给test
1
是混乱的,我不相信pytest会这样做。我认为
2
就是这样发生的,所以您的测试需要定义该参数。