python接口自动化12-pytest前后置与fixture,,前言我们都知道在自动


前言

我们都知道在自动化测试中都会用到前后置,pytest 相比 unittest 无论是前后置还是插件等都灵活了许多,还能自己用 fixture 来定义。(甩 unttest 半条街?)

首先了解一下,用例运行前后置级别如下:

模块级:全局的,整个模块开只运行一次,优先于测试用例。类级别:定义在类里面,只针对此类生效。类似unittest的cls装饰器函数级:只对函数生效,类下面的函数不生效。方法级:定义在类里面,每个用例都执行一次

一、setup、teardown级别

1、模块级别:setup_module、teardown_module

全局的,整个模块开只运行一次,优先于测试用例。

技术图片

2、类级别:setup_class、teardown_class

类级别:定义在类里面,只针对此类生效。类似unittest的cls装饰器

技术图片

3、函数级:setup_function、teardown_function

函数级:只对函数生效,类下面的函数不生效。

技术图片

4、方法级:setup_method、teardown_method

方法级:定义在类里面的函数,也叫方法,每个用例都执行一次

技术图片

最后全部执行打印,代码:

def setup_module():    print(‘\n整个模块 前 只运行一次‘)def teardown_module():    print(‘\n整个模块 后 只运行一次‘)def setup_function():    print(‘\n不在类中的函数,每个用例 前 只运行一次‘)def teardown_function():    print(‘\n不在类中的函数,每个用例 后 只运行一次‘)def test_ab():    b = 2    assert b < 3def test_aba():    b = 2    assert b < 3class Test_api():    def setup_class(self):        print(‘\n此类用例 前 只执行一次‘)    def teardown_class(self):        print(‘\n此类用例 后 只执行一次‘)    def setup_method(self):        print(‘\n此类每个用例 前 只执行一次‘)    def teardown_method(self):        print(‘\n此类每个用例 后 执行一次‘)    def test_aa(self):        a = 1        print(‘\n我是用例:a‘)       # pytest -s 显示打印内容        assert a > 0    def test_b(self):        b = 2        assert b < 3

技术图片

二、fixture简单使用

1、Fixture 其实就是自定义 pytest 执行用例前置和后置操作,首先创建 conftest.py 文件 (规定此命名)

2、导入 pytest 模块,运用 pytest.fixture 装饰器,默认级别为:函数级,如图二源码

技术图片

技术图片

3、其它用例文件调用即可,如下定义一个函数,继承 conftest.py 文件里的 setup_login 函数即可调用:

‘‘‘ 运用 fixtures 定义的顺序 ‘‘‘def test_001(setup_login):    print(‘\n上面是登录,我现在点击进入home‘)

技术图片

4、cmd 运行结果如下:

先执行了 conftest.py 文件里的 setup_login 函数,再执行运行的用例.py文件;

G:\python_study\study\pytest_demo\study>pytest -s test_fixture.py================================================= test session starts =================================================platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0rootdir: G:\python_study\study\pytest_demo\study, inifile:collected 1 itemtest_fixture.py先执行登录上面是登录,我现在点击进入home.============================================== 1 passed in 0.07 seconds ===============================================

5、conftest.py 文件,自定义函数后置操作:yield

技术图片

G:\python_study\study\pytest_demo\study>pytest -s test_fixture.py================================================= test session starts =================================================platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0rootdir: G:\python_study\study\pytest_demo\study, inifile:collected 1 itemtest_fixture.py先执行登录上面是登录,我现在点击进入home.测试数据最后执行清理============================================== 1 passed in 0.06 seconds ===============================================

6、多个自定义函数和全局级别展示:(全局的比如用于登录获取到token其他用例模块就不需要再登录了)

①conftest.py 文件代码如下:

import pytest@pytest.fixture(scope=‘session‘)    # scope=‘session‘ 任何文件共享def setu_login():    print(‘\n用例先登录‘)@pytest.fixture()def open_html():    print(‘\n打开页面‘)    # 后置操作    yield    print(‘\n测试数据最后执行清理‘)

②用例文件代码如下:

def test_001(setu_login):    print(‘\n上面是登录,我现在点击进入home‘)def test_002(open_html):    print(‘\n没登录,打开html‘)def test_003(setu_login, open_html):    print(‘\n登录后,打开html‘)def test_data(open_html):    print(‘测试数据1‘)def test_data1(open_html):    print(‘测试数据122‘)

③cmd 运行结果:

G:\python_study\study\pytest_demo\study>pytest -s test_fixture.py================================================= test session starts =================================================platform win32 -- Python 3.6.5, pytest-3.6.3, py-1.8.0, pluggy-0.6.0rootdir: G:\python_study\study\pytest_demo\study, inifile:collected 5 itemstest_fixture.py用例先登录上面是登录,我现在点击进入home.打开页面没登录,打开html.测试数据最后执行清理打开页面登录后,打开html.测试数据最后执行清理打开页面测试数据1.测试数据最后执行清理打开页面测试数据122.测试数据最后执行清理============================================== 5 passed in 0.06 seconds ===============================================

技术图片

看完之后,有没有甩 unittest 框架半条街你说了算?pytest 成为了目前主流的任意玩框架。

对于运行用例级别都是setup_xx,teartown_xx,后面接module、class是不是很好记呢?欢迎来QQ交流群:482713805

python接口自动化12-pytest前后置与fixture

评论关闭