【问题标题】:Pytest fixture not able to quit the test after execution in python pytest在 python pytest 中执行后,Pytest 夹具无法退出测试
【发布时间】:2018-04-24 07:17:26
【问题描述】:

我在 conftest.py 中使用下面的夹具​​来打开和关闭浏览器: conftest.py:

@pytest.fixture(scope='session')
def browser():
    print("Setting up webdriver connection...")
    global driver
    if driver is None:
        profile = webdriver.FirefoxProfile()
        profile.accept_untrusted_certs = True
        profile.set_preference("network.proxy.type",1)
        driver = webdriver.Firefox(firefox_profile=profile)
        driver.maximize_window()
        yield driver
        driver.quit()
        print("Webdriver connection closed..")

在我的测试类中,我有以下步骤 test_login :

def test_login(self, browser):
        login_page = pagep.LoginPage(browser)
        login_page.login('hptest376@gmail.com', "test12345*")
        login_page.login('user', "pwd")
        assert "Sum" in browser.title

在 test_login 之后,我的测试类中多了一个测试步骤:

def test_ppt(a):
    a=12
    print a

问题:我在 driver.quit() 的夹具中遇到错误,并且浏览器没有关闭。

如果我在“test_login(self, browser)”之后删除“test_ppt(a)”步骤,那么测试运行正常。

想知道我需要在我的设备“browser()”中更新什么,以便执行 driver.quit()。

【问题讨论】:

  • driver.quit() 遇到什么错误?
  • 我发现测试属于下一步,即 test_ppt(a) 尝试关闭浏览器,因为夹具的范围是 'session' 。我将夹具的范围更改为“功能”,现在测试通过了。 @pytest.fixture(scope='function') def browser():

标签: python selenium-webdriver pytest


【解决方案1】:

将fixture的范围改为'function',测试通过了。

@pytest.fixture(scope='function') 
def browser():

【讨论】:

    猜你喜欢
    • 2021-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多