Featured image of post Pytest中自动指定可引用fixture

Pytest中自动指定可引用fixture

当我们指定fixture时,能否隐式指定并可以引用呢?

在我们使用 Pytest 框架进行自动化测试时,强大的 fixture 夹具为框架的灵活应用提供了极大的便利。比如我们可以利用 fixture 的autouse属性,使它在测试方法的不同范围层级上自动生效。但如果要引用fixture的返回,我们通常还是要明确指定,稍显麻烦

autouse指定fixture,但还是需要显式携带为参数才可调用

Selenium 自动化时最常用的webdriver为例, 实现自动化时,因为webdriver 对象通常会在整个执行期间都生效,而且会频繁地进行调用。

通常的做法是可以在 conftest.py 文件中编写一个session层级的通用driver夹具,设置 autouse为启用:

conftest.py

1
2
3
4
5
6
7
@pytest.fixture(scope="session",autouse=True)
def driver():
	option = webdriver.ChromeOptions()  
	driver = webdriver.Chrome(options=option)  
	driver.implicitly_wait(3)  
	driver = webdriver.Chrome()  
	yield driver

然后我们在另一个测试文件中,编写测试类和测试方法时,就可以直接引用这个driver夹具

test_auto_driver.py

1
2
3
4
class TestDriver:  
    def Test_open_blog(self, driver):  
        driver.get("https://chengxiaqiucao.github.io/")  
        assert driver.title == "秋 草 观 “测” 台"

但如果整个测试工程比较大,类似的测试类、测试方法比较多的时候,每定义一次测试方法都要指定这个通用fixture,才能引用driver对象,是不是总感觉不太优雅?

Pytest内置Fixture: Request

其实大家学习过通过Fixture来参数化的方法的话,应该知道通过Fixture进行参数化,是通过pytest的一个内部fixture request来完成的。 request应该是pytest中最为重要的一个内置夹具了。而利用这个fixture,我们其实可以实现隐式地将一些通用fixture默认地传递给各个测试类

request简介

通过官方文档的介绍:

request 是一个用于提供当前测试方法上下文信息,以及通过param参数实现参数化的一个内置夹具。

利用request,我们可以在运行过程中获取当前fixture的诸多上下文信息。比如夹具名称、路径、当前的session、模块、类信息等。

下图是引用fixture时,断点得到的request对象属性

那上例中作为一个在session级别生效的通用的fixture,我们可以通过它的session属性,获取当前session中的所有类,再将driver对象赋值给相关类属性,这样我们在定义测试类时,就可以无需另外指定fixture,直接通过类属性根据需要引用即可。

实现fixture自动赋值到类属性

所以修改上面案例的代码如下:

conftest.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
@pytest.fixture(scope="session",autouse=True)  
def driver(request):  
    """  
    自动应用fixture,并设置到调用的类属性中  
    :param request: pytest内置fixture,用于获取fixture的上下文  
    :return:  
    """    driver = webdriver.Chrome()  
    try:  
        session = request.node  
        for item in session.items:  
            cls = item.getparent(pytest.Class)  
            setattr(cls.obj, 'driver', driver)  
        yield 
    finally:  
        driver.quit()

这里我们将driver对象添加为类属性,而fixture本身不再yield任何对象返回

相应的,测试脚本中,直接通过self.driver 易用类属性即可

test_auto_driver.py

1
2
3
4
class TestDriver:  
    def Test_open_blog(self, driver):  
        self.driver.get("https://chengxiaqiucao.github.io/")  
        assert self.driver.title == "秋 草 观 “测” 台"

执行效果, 用例同样正确完成执行。

以上~~

使用 Hugo 构建
主题 StackJimmy 设计