gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 02:20:50 26 4
gpt4 key购买 nike

我正在尝试使用 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 移动到另一个类范围的 fixture:

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 fixture,它将在 get_driver 之后调用(因为 pytest.mark.usefixtures( 'get_driver')).

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com