zoukankan      html  css  js  c++  java
  • pytest-2-前后置与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('
    整个模块 前 只运行一次')
    
    def teardown_module():
        print('
    整个模块 后 只运行一次')
    
    def setup_function():
        print('
    不在类中的函数,每个用例 前 只运行一次')
    
    def teardown_function():
        print('
    不在类中的函数,每个用例 后 只运行一次')
    
    def test_ab():
        b = 2
        assert b < 3
    
    def test_aba():
        b = 2
        assert b < 3
    
    class Test_api():
    
        def setup_class(self):
            print('
    此类用例 前 只执行一次')
        def teardown_class(self):
            print('
    此类用例 后 只执行一次')
    
        def setup_method(self):
            print('
    此类每个用例 前 只执行一次')
    
        def teardown_method(self):
            print('
    此类每个用例 后 执行一次')
    
        def test_aa(self):
            a = 1
            print('
    我是用例: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('
    上面是登录,我现在点击进入home')

    4、cmd 运行结果如下:

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

    G:python_studystudypytest_demostudy>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_studystudypytest_demostudy, inifile:
    collected 1 item
    
    test_fixture.py
    先执行登录
    
    上面是登录,我现在点击进入home
    .
    
    ============================================== 1 passed in 0.07 seconds ===============================================

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

    G:python_studystudypytest_demostudy>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_studystudypytest_demostudy, inifile:
    collected 1 item
    
    test_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('
    用例先登录')
    
    @pytest.fixture()
    def open_html():
        print('
    打开页面')
    
        # 后置操作
        yield
        print('
    测试数据最后执行清理')

    ②用例文件代码如下:

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

    ③cmd 运行结果:

    G:python_studystudypytest_demostudy>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_studystudypytest_demostudy, 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

  • 相关阅读:
    all the tops
    es6 and typescript
    [leetcode]question5: Longest Palindromic Substring
    webpack and publish lib
    HTTPClient to use http/https protocol to send request
    《算法导论》-分治法-笔记
    《Linux C编程一站式学习》-笔记
    WIN7中同时打开多个独立Excel窗口
    RAD Studio XE6之Tpanel
    vb中StatusBar1.Panels(3).Text = Format(Date, "yyyy年mm月dd日")是什么意思
  • 原文地址:https://www.cnblogs.com/gsxl/p/12051552.html
Copyright © 2011-2022 走看看