pytest测试框架中的setup和tearDown - 1

这篇博客探讨了pytest框架中使用fixture替代传统setup/tearDown的方法,强调了fixture在处理资源开启和关闭(如浏览器driver)上的优势,以及如何通过fixture提升代码的整洁性,例如在数据库操作中建立连接、获取句柄、释放资源。文中还提到,如果fixture不使用yield,它将作为参数传递给使用它的测试函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这次内容是阅读文档pytest fixtures: explicit, modular, scalable的一些总结,pytest fixture功能很丰富,功能远不止用来构建测试中传统的setup/teardown。

但是还是先看下用pytest.fixture特性写的setup/teardown,据stakoverflow上一哥们说,这还是目前的最佳实践。

import time
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
from utils.log import logger
from utils.config import get_url

@pytest.fixture()
def chrome_driver(scope="function"):
    """
    scope="function"是scope的默认值,表示这是一个function级别的fixture
    """
    print("setup() begin")
    driver = webdriver.Chrome()
    driver.get(get_url())
    print("setup() end")
    yield driver 
    #这里会返回driver,给使用这个fixture为参数的test_函数使用,
    #test_函数结束后,会回到这里,继续执行后面语句
    print("teardown() begin")
    driver.close()
    print("teardown() end")


class TestBaiDu(object):

    locator_kw = (By.ID, 'kw')
    locator_su = (By.ID, 'su')
    locator_result = (By.XPATH, '//div[contains(@class, "result")]/h3/a')

    def test_search_0(self, chrome_driver):
        """
        这里的chrome_driver是在本模块中定义的fixture,这里输入
        的参数是上面yield driver中返回的driver
        """
        chrome_driver.find_element(*self.locator_kw).send_keys(u'selenium 测试')
        chrome_driver.find_element(*self.locator_su).click()
        time.sleep(2)
        links = chrome_driver.find_elements(*self.locator_result)
        for link in links:
            logger.info(link.text)

这样写看起来有点pythonic的味道,我理解写这样fixture形式的setup/teardown函数,主要还是给那些需要打开然后关闭的资源,比如上面例子中的浏览器driver,确实需要收尾(driver.quit())。

可能还有其他应用,比如写一个数据库查询的函数,就可以把连接数据库,获得数据查询句柄,yield 句柄,关闭数据库句柄,关闭数据连接写成一个fixture,这样代码应该清爽多了。

@pytest.fixture(scope="module") #一个module里的所有函数共用一个句柄实例
def sql_query():
    #连接数据库
    #获得数据库查询句柄
    yield "查询句柄"
    #关闭句柄
    #关闭数据库连接

fixture如果不用到yield,则只是把fixture函数里返回的值,作为参数给到使用fixture的函数,代码如下

@pytest.fixture()
def fruit_name():
    return  "apple"

def test_fruit(fruit_name):
    assert "apple" == fruit_name
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值