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

前言

咱们都知道在自动化测试中都会用到先后置,pytest 相比 unittest 不管是先后置仍是插件等都灵活了许多,还能本身用 fixture 来定义。(甩 unttest 半条街?)html

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

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

1、setup、teardown级别

一、模块级别:setup_module、teardown_moduleapi

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

 二、类级别:setup_class、teardown_class框架

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

 三、函数级:setup_function、teardown_function测试

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

 四、方法级:setup_method、teardown_method插件

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

 最后所有执行打印,代码:

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 < 3

def test_aba():
    b = 2
    assert b < 3

class 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

 2、fixture简单使用

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

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

 

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

''' 运用 fixtures 定义的顺序 '''
def test_001(setup_login):
    print('\n上面是登陆,我如今点击进入home')

四、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.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 1 item

test_fixture.py
先执行登陆

上面是登陆,我如今点击进入home
.

============================================== 1 passed in 0.07 seconds ===============================================

五、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.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 1 item

test_fixture.py
先执行登陆

上面是登陆,我如今点击进入home
.
测试数据最后执行清理


============================================== 1 passed in 0.06 seconds ===============================================

六、多个自定义函数和全局级别展现:(全局的好比用于登陆获取到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.0
rootdir: G:\python_study\study\pytest_demo\study, inifile:
collected 5 items

test_fixture.py
用例先登陆

上面是登陆,我如今点击进入home
.
打开页面

没登陆,打开html
.
测试数据最后执行清理

打开页面

登陆后,打开html
.
测试数据最后执行清理

打开页面
测试数据1
.
测试数据最后执行清理

打开页面
测试数据122
.
测试数据最后执行清理


============================================== 5 passed in 0.06 seconds ===============================================

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

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

相关文章
相关标签/搜索