Record life and learning |

winkie

园龄:4年10个月粉丝:9关注:10

3.pytest实现用例的前后置fixture

测试用例实现前后置,有多种方法。在实际编写测试脚本时,要根据实际情况选择

一. xunit类型

def setup_function()
def teardown_function()

def setup_method(self)
def teardown_method(self)

@classmethod
def setup_class(cls)
@classmethod
def teardown_class(cls)

二. unittest类型

#先要在class类中继承unittest.testcase
@classmethod
def setUpClass(cls)
@classmethod
def tearDownClass(cls)
def setup(self)
def tearDown(self)

三. pytest中的fixture类型

1. 定义夹具

@pytest.fixture
def fixture_func():
    print('前置条件')
    yield  #这里可以有返回值,用于夹具的继承
    print('后置条件')

@pytest.fixture(scope,autouse)

scope的取值有:
1.function 默认范围,函数范围,在测试完成后结束 【 类似setup/teardown】
2.class 在类中最后一个测试完成后结束 【类似setupClass/teardownClass】
3.module 在模块中最后一个测试完成后结束
4.package 在包中的最后一个测试完成后结束
5.session 在一次会话中的最有一个测试完成后结束【一次pytest叫做一次会话】

autouse取值默认False
1.模块级别/包级别/会话级别需要加

2.使用夹具

@pytest.mark.usefixtures("fixture_func")
def test():
    print('开始测试')

3.共享夹具

  • 共享夹具都放在conftest.py文件中
  • 会从测试文件开始,一层一层目录查找conftest.py文件
  • 测试用例可以直接调用共享夹具中的夹具,不需要额外导入
#conftest.py文件,在该文件下的夹具为共享夹具
@pytest.fixture(scope='class')
def driver(pytestconfig):
    browser = pytestconfig.getoption('--browser')
    if browser == 'edge':
        s = Service(executable_path=settings.BROWSER_DRIVERS['edge'])
        with webdriver.Edge(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
     if browser == 'chrome':
        s = Service(executable_path=settings.BROWSER_DRIVERS['chrome'])
        with webdriver.Chrome(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
     if browser == 'firefox':
        s = Service(executable_path=settings.BROWSER_DRIVERS['firefox'])
        with webdriver.Firefox(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd

4.夹具多重使用的执行步骤

  • 1.先执行范围大的,再执行范围小的
  • 2.同级别,按照先后顺序去调用夹具

5.夹具的继承

只能从大范围或者同范围的继承

夹具继承后,可以得到夹具到返回值

事例:

#conftest.py 共享文件
@pytest.fixture(scope='class')
def driver(pytestconfig):
    browser = pytestconfig.getoption('--browser')
    if browser == 'edge':
        s = Service(executable_path=settings.BROWSER_DRIVERS['edge']) #webdriver存放路径需要自己修改
        with webdriver.Edge(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd      #这里有返回值,用于夹具的继承
    if browser == 'chrome':
        s = Service(executable_path=settings.BROWSER_DRIVERS['chrome'])
        with webdriver.Chrome(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
    if browser == 'firefox':
        s = Service(executable_path=settings.BROWSER_DRIVERS['firefox'])
        with webdriver.Firefox(service=s) as wd:  # 最大化浏览器
            # wd.maximize_window()
            yield wd
#测试用例文件
class TestMaxWindow():
    def test_maxWindow(self, driver):
      	#执行步骤:
        #1.会在共享夹具中找到driver,yield之前的为前置操作
        #2.执行到yield,夹具有返回值则得到参数
        #3.在该测试用例中可以用夹具返回的参数
        #4.测试用例结束后,继续执行夹具中的后置操作
        driver.maximize_window()

本文作者:winkie

本文链接:https://www.cnblogs.com/zz-winkie/p/16528843.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   winkie  阅读(76)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起