python - 如何使 pytest 驱动程序实例在我的测试用例中可用?

标签 python selenium-webdriver pytest

我正在尝试使用 Python、Pytest 构建一个基于 Selenium 的自动化框架。

我的目的是在类级别创建一个驱动程序实例,方法是在文件 conftest.py 中初始化它并使其在所有测试用例中可用,以便用户不需要创建驱动程序每个测试用例中的实例。

conftest.py 中的驱动程序实例:

@pytest.fixture(scope="class")
def get_driver(request):
    from selenium import webdriver
    driver = webdriver.Chrome()
    request.cls.driver = driver
    yield
    driver.quit()

BaseTestCase 类如下所示:

@pytest.mark.usefixtures("get_driver")
class BaseTestCase(unittest.TestCase):

    def __init__(self, *args, **kwargs):
        super(BaseTestCase, self).__init__(*args, **kwargs)

    @classmethod
    def setUpClass(cls):
        if hasattr(super(BaseTestCase, cls), "setUpClass"):
            super(BaseTestCase, cls).setUpClass()

我的测试用例如下:

from ..pages.google import Google

class Test_Google_Page(BaseTestCase):

    @classmethod
    def setUpClass(self):
        self.page = Google(self.driver, "https://www.google.com/")

我的页面 Google 扩展到 BasePage,如下所示:

class BasePage(object):
    def __init__(self, driver, url=None, root_element = 'body'):
        super(BasePage, self).__init__()

        self.driver = driver
        self._root_element = root_element
        self.driver.set_script_timeout(script_timeout)

当我执行测试用例时,出现以下错误:

    @classmethod
    def setUpClass(self):
>       driver = self.driver
E       AttributeError: type object 'Test_Google_Page' has no attribute 'driver'

如何通过简单地调用 self.driver 使驱动程序实例在我的测试用例中可用?

最佳答案

类作用域的固定装置在 setUpClass 类方法之后执行,因此当执行 Test_Google_Page.setUpClass 时,get_driver 尚未运行。查看执行顺序:

import unittest
import pytest


@pytest.fixture(scope='class')
def fixture_class_scoped(request):
    print(f'{request.cls.__name__}::fixture_class_scoped()')


@pytest.mark.usefixtures('fixture_class_scoped')
class TestCls(unittest.TestCase):

    @classmethod
    def setUpClass(cls):
        print(f'{cls.__name__}::setUpClass()')

    def setUp(self):
        print(f'{self.__class__.__name__}::setUp()')

    @pytest.fixture()
    def fixture_instance_scoped(self):
        print(f'{self.__class__.__name__}::fixture_instance_scoped()')

    @pytest.mark.usefixtures('fixture_instance_scoped')
    def test_bar(self):
        print(f'{self.__class__.__name__}::test_bar()')
        assert True

当使用例如运行测试时pytest -sv,输出结果:

TestCls::setUpClass()
TestCls::fixture_class_scoped()
TestCls::fixture_instance_scoped()
TestCls::setUp()
TestCls::test_bar()

因此,解决方案是将代码从 setUpClass 移至例如设置:

class Test_Google_Page(BaseTestCase):

    def setUp(self):
        self.page = Google(self.driver, "https://www.google.com/")

I am afraid I may not use setUp because in my most of my class file I have multiple test cases and I just want to do one time setup in setUpClass instead of launching in each time before calling any test method.

然后我会将代码从 setUpClass 移动到另一个类范围的固定装置:

import pytest

@pytest.mark.usefixtures('get_driver')
@pytest.fixture(scope='class')
def init_google_page(request):
    request.cls.page = Google(request.cls.driver,
                              "https://www.google.com/")


@pytest.mark.usefixtures('init_google_page')
class Test_Google_Page(BaseTestCase):
    ...

以前的 setUpClass 现在是 init_google_page 固定装置,它将在 get_driver 之后调用(因为 pytest.mark.usefixtures( 'get_driver'))。

关于python - 如何使 pytest 驱动程序实例在我的测试用例中可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51682413/

相关文章:

python - 如何在 Matplotlib TextBox 小部件中设置光标位置?

ruby - 如何在 goto 之前设置 cookie?

python - pytest:在每次循环失败运行时写入时间戳

python - 如何通过命令行将 firefox/chrome headless 模式传递给 pytest

python - 如何在身份验证后获取 LinkedIn 返回 URL

python - 如何加快查找哪个图像像素颜色不在给定颜色列表中

python - Pandas :我如何迭代两个格式完全相同的数据帧?

javascript - 在 javascript 中使用 Selenium Driver 获取 WebElement 文本内容

java - 以 .docx 或 .pdf 结尾的链接

python - 通过 Travis-CI 在测试目录上运行覆盖