zoukankan      html  css  js  c++  java
  • pytest测试框进阶(一)

    pytest修饰器用来标记固定工厂函数,在其他函数,模块,类或整个工程进行调用它时会被激活并执行,通常会被用于完成预置处理和重复操作。

    方法:fixture(scope="function", params=None, autouse=False, ids=None, name=None)
    常用参数:

    scope:被标记方法的作用域

    function" (default):作用于每个测试方法,每个test都运行一次

    "class":作用于整个类,每个class的所有test只运行一次

    "module":作用于整个模块,每个module的所有test只运行一次

    "session:作用于整个session(慎用),每个session只运行一次

    params:(list类型)提供参数数据,供调用标记方法的函数使用

    autouse:是否自动运行,默认为False不运行,设置为True自动运行

    fixture(通过参数引用)

    import pytest
    class Test_ABC:
        @pytest.fixture()
        def before(self):
            print("----->before")
    
        def test_a(self, before):    # ️ test_a方法传入了被fixture标识的函数,已变量的形式print("------>test_a")
            assert 1

    执行结果:

    test_one.py ----->before
    ------>test_a
    fixture(通过函数引用)

    import pytest
    @pytest.fixture()   # fixture标记的函数可以应用于测试类外部
    def after():
        print("------>after")
    
    @pytest.mark.usefixtures("after")
    class Test_abc:
        def setup(self):
            print("----->setup")
    
        def test_b(self):
            print("------>test_b")
    
        def test_c(self):
            print("---->test_c")

    执行结果:

    test_one.py ------>after
    ----->setup
    ------>test_b
    .------>after
    ----->setup
    ---->test_c

    fixture(默认设置为运行)

    import pytest
    
    @pytest.fixture(autouse=True)
    def middler():
        print("------->middler")
    
    class Test_def:
        def setup(self):
            print("------>setup")
        def test_d(self):
            print("----->test_d")
        def test_e(self):
            print("----->test_e")

    执行结果:

    test_one.py ------->middler
    ------>setup
    ----->test_d
    .------->middler
    ------>setup
    ----->test_e


    fixture(设置作用域为function)

    import pytest
    
    @pytest.fixture(scope="function", autouse=True)
    def aft():
        print("------>aft")
    
    class Test_ghi:
        def setup(self):
            print("------>setup")
        def test_f(self):
            print("------>test_f")
        def test_g(self):
            print("------>test_g")

    执行结果:

    test_one.py ------>aft
    ------>setup
    ------>test_f
    .------>aft
    ------>setup
    ------>test_g

    fixture(设置作用域为class)

    import pytest
    
    @pytest.fixture(scope="class", autouse=True)
    def eftg():
        print("------->eftg")
    class Test_lmn:
        def setup(self):
            print("------>setup")
        def test_h(self):
            print("----->test_h")
        def test_i(self):
            print("----->test_i")

    执行结果:

    test_one.py ------->eftg
    ------>setup
    ----->test_h
    .------>setup
    ----->test_i

    fixture(返回值)

    import pytest
    
    @pytest.fixture()
    def data():
        return 2
    class Test_ABC:
        def test_a(self, data):
            print("-------->test_a")
            assert data != 3

    执行结果

    test_one.py -------->test_a

  • 相关阅读:
    指令
    linux学习之多高并发服务器篇(三)
    linux学习之高并发服务器篇(二)
    linux学习之多高并发服务器篇(一)
    Linux学习之socket编程(二)
    Linux学习之socket编程(一)
    myeclipse中如何修改Servlet模板_day01
    Properties的使用以及配置文件值的获取
    Sql_Server中如何判断表中某字段是否存在
    微博开发流程-01
  • 原文地址:https://www.cnblogs.com/zhouzetian/p/13599105.html
Copyright © 2011-2022 走看看