5. Pytest自定义前置后置:fixture参数详解(了解)

一、前言

我们上节课讲到fixture自定义前置函数的时候,有5个非必填参数,scope ,params,autouse,ids,name。一般情况下这五个参数我们在工作中都不常用,但是个别情况会用到,这节课针对这五个参数做详细讲解。

二、学习目标

1.scope参数实例讲解

2.params参数实例讲解

3.autouse参数实例讲解

4.ids参数实例讲解

5.name参数实例讲解

三、知识点

1.scope参数实例讲解

  • 用于控制Fixture的作用范围
  • 作用类似于Pytest的setup/teardown
  • 默认取值为function(函数级别),控制范围的排序为:session > module > class > function
取值 范围 说明
function 函数级 每一个函数或方法都会调用
class 类级别 每个测试类只运行一次
module 模块级 每一个.py文件调用一次
session 会话级 每次会话只需要运行一次,会话内所有方法及类,模块都共享这个方法

代码示例:

#conftest.py前置定义文件
import pytest

@pytest.fixture(scope="session")
def init_session():
    print("回话前置")
    yield
    print("回话后置")

@pytest.fixture(scope="module")
def init_module():
    print("模块前置")
    yield
    print("模块后置")

@pytest.fixture(scope="class")
def init_class():
    print("类的前置")
    yield
    print("类的后置")

@pytest.fixture(scope="function")
def init_function():
    print("用例前置")
    yield
    print("用例后置")
#test_xxx.py测试用例文件
import pytest

def test_01():
    print("用例01")

@pytest.mark.usefixtures('init_module')
def test_02():
    print("用例02")

@pytest.mark.usefixtures('init_class')
class TestLogin():
    def test_1(self):
        print("用例1")

    @pytest.mark.usefixtures('init_function')
    def test_2(self):
        print("用例2")

运行效果:

test_pytest_demo6.py::test_01 PASSED                                     [ 25%]用例01

test_pytest_demo6.py::test_02 模块前置
PASSED                                     [ 50%]用例02

test_pytest_demo6.py::TestLogin::test_1 类的前置
PASSED                           [ 75%]用例1

test_pytest_demo6.py::TestLogin::test_2 用例前置
PASSED                           [100%]用例2
用例后置
类的后置
模块后置
============================== 4 passed in 0.03s ==============================

2.params参数实例讲解

  • Fixture的可选形参列表,支持列表传入
  • 默认None,每个param的值
  • fixture都会去调用执行一次,类似for循环
  • 可与参数ids一起使用,作为每个参数的标识,详见ids
  • 被Fixture装饰的函数要调用是采用:Request.param(固定写法)

代码示例:

#conftest.py前置定义文件
@pytest.fixture(params=["a","b",{"name":"user1"},{"name":"user2"}]) #params必须是列表
def demo(request):
    return request.param  #固定写法
#test_xxx.py测试用例文件
#测试用例文件test_XXX.py
import pytest

class TestCase():

    def test_case_01(self,demo):
        print("打印param:{}".format(demo))

运行效果:(列表中的每一组数据都执行了一遍测试用例)

test-demo7.py::TestCase::test_case_01[a] PASSED                          [ 25%]打印param:a

test-demo7.py::TestCase::test_case_01[b] PASSED                          [ 50%]打印param:b

test-demo7.py::TestCase::test_case_01[demo2] PASSED                      [ 75%]打印param:{'name': 'user1'}

test-demo7.py::TestCase::test_case_01[demo3] PASSED                      [100%]打印param:{'name': 'user2'}

============================== 4 passed in 0.03s ==============================

3.ids参数实例讲解

  • 用例标识ID
  • 与params配合使用,一对一关系

代码示例:

#conftest.py前置定义文件
@pytest.fixture(params=["a","b",{"name":"user1"},{"name":"user2"}],ids=["one","two","three","four"])
def demo(request):
    return request.param
#测试用例文件test_XXX.py
import pytest

class TestCase():

    def test_case_01(self,demo):
        print("打印param:{}".format(demo))

运行效果:

test-demo7.py::TestCase::test_case_01[one] 回话前置 #运行时显示了我们指定的ids
PASSED                        [ 25%]打印param:a

test-demo7.py::TestCase::test_case_01[two] PASSED                        [ 50%]打印param:b

test-demo7.py::TestCase::test_case_01[three] PASSED                      [ 75%]打印param:{'name': 'user1'}

test-demo7.py::TestCase::test_case_01[four] PASSED                       [100%]打印param:{'name': 'user2'}
回话后置

============================== 4 passed in 0.02s ==============================

4.autouse参数实例讲解

  • 默认False

  • 若为True,刚每个测试函数都会自动调用该fixture,无需传入fixture函数名

  • 由此我们可以总结出调用fixture的三种方式:
    1.函数或类里面方法直接传fixture的函数参数名称
      2.使用装饰器@pytest.mark.usefixtures()修饰
      3.autouse=True自动调用,无需传仍何参数,作用范围跟着scope走(谨慎使用)

代码示例:

#conftest.py前置定义文件
import pytest

@pytest.fixture(scope="session",autouse=True)
def init_session():
    print("回话前置")
    yield
    print("回话后置")
#测试用例文件test_XXX.py
import pytest

class TestCase():

    def test_case_01(self):  #这里没有调用前置,看看运行是否自动执行了
        print("测试用例01")

运行效果:

test-demo7.py::TestCase::test_case_01 回话前置  #前置自动执行了
PASSED                             [100%]测试用例01
回话后置
============================== 1 passed in 0.01s ==============================

5.name参数实例讲解

  • fixture的重命名
  • 通常来说使用 fixture 的测试函数会将 fixture 的函数名作为参数传递,但是 pytest 也允许将fixture重命名
  • 如果使用了name,那只能将name传如,函数名不再生效

代码示例:

#conftest.py前置定义文件
@pytest.fixture(scope="function",name="new_fixture")
def demo_name():
    print("用例前置")
    yield
    print("用例后置")
#test_xxx.py测试用例文件
#测试用例文件test_XXX.py
import pytest

class TestCase():

    def test_case_01(self,new_fixture): #使用新名字调用前置
        print("测试用例01")

运行效果:

test-demo7.py::TestCase::test_case_01 用例前置
PASSED                             [100%]测试用例01
用例后置
============================== 1 passed in 0.01s ==============================
posted @ 2023-01-17 10:25  测开星辰  阅读(294)  评论(0编辑  收藏  举报