【pytest】fixture基本用法(参数解释及示例)

  • 前言:

    • 在做自动化的过程中,编写用例时候需要用到用例的前置和用例的后置,其中pytest中有setup_class和teardown_class可以帮助我们完成这些,但是不够完善而且灵活性不够强。举个简单的例子,一个calss中有3条用例,其中2条需要登录,1条不需要登录,这个时候如果在用setup和teardown来做就有点不方便。这个时候就引入了新的知识点fixture。
    • 固件(fixture)是一些函数,pytest会在执行测试函数之前(或之后)加载运行他们。我们可以利用固件在程序运行前做初始化,运行后做清场
    • 定义fixture跟定义普通函数差不多,唯一区别就是在函数上加个装饰器@pytest.fixture(),fixture命名不要以test开头,跟用例区分开。fixture是有返回值得,没有返回值默认为None
  • fixture优势

    • 命名方式灵活,不局限于setup和teardown这几个命名
    • conftest.py 配置里可以实现数据共享,不需要import就能自动找到一些配置
    • scope="module" 可以实现多个.py跨文件共享前置, 每一个.py文件调用一次
    • scope="session" 以实现多个.py跨文件使用一个session来完成多个用例
  • fixture参数说明

@pytest.fixture(scope='function',params=params, autouse=autouse, ids=ids, name=name)
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#arg scope: scope 有四个级别参数 "function" (默认), "class", "module" or "session".
#arg params: 一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它
#arg autouse:  如果为True,则为所有测试激活fixture func 可以看到它。 如果为False(默认值)则显式需要参考来激活fixture
#arg ids: 每个字符串id的列表,每个字符串对应于params 这样他们就是测试ID的一部分。 如果没有提供ID它们将从params自动生成
#arg name:   fixture的名称。 这默认为装饰函数的名称。
  • scope参数
    • scope=‘function’ 实现方法级别的前/后置条件
#conftest.py
import pytest
@pytest.fixture(scope='function')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

    def test_three(self):
        print('Test_class-->>test three')
  • 执行结果

  • scope=‘class’ 实现类级别的前 / 后置条件;在需要前 / 后置条件的类中任意方法引用即可

#conftest.py
import pytest
@pytest.fixture(scope='class')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

    def test_three(self):
        print('Test_class-->>test three')
  • 执行结果,类执行前后执行一次

  • scope=‘module’ 实现模块级别的前 / 后置条件;模块中任意类下的任意方法有被引用即可

#conftest.py
import pytest
@pytest.fixture(scope='module')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

class Testdemo:
    def test_one(self):
        print('Test_demo-->>test three')
  • 执行结果,每个模块前后执行一次前置/后置

  • 多个.py文件情况下执行结果

  • scope=‘session’,实现会话级别的前 / 后置条件;每个模块中任意类下的任意方法有被引用,每个模块执行一次

#conftest.py
import pytest
@pytest.fixture(scope='session')
def my_fixture():
    print('fixture---前置')
    yield
    print('fixture---后置')

#test_demo.py
import pytest
class TestClass:
    def test_one(self,my_fixture):
        print('Test_class-->>test one')

    def test_two(self):
        print('Test_class-->>test two')

class Testdemo:
    def test_one(self):
        print('Test_demo-->>test three')

#test_sample.py
import pytest
class Testsample:
    def test_one(self,my_fixture):
        print('Test_sample-->>test one')

    def test_two(self):
        print('Test_sample-->>test two')
  • 执行结果

  • autouse参数

    • 默认为False,当autouse=True时,对应scope作用域的所有方法/ 类/模块的前/后置条件都生效
  • params参数

    • params 可以实现测试数据的参数化
#conftest.py
import pytest
param = [1,22,333]
@pytest.fixture(params=param)
def my_fixture(request):   #固定参数名
    #  return request.param   #固定返回值
    print('fixture---前置')
    yield request.param    
    print('fixture---后置')

##test_demo.py
class Testdemo:
    def test_one(self,my_fixture):
        print(my_fixture)
        print('Test_demo-->>test three')
  • 执行结果

  • ids参数:

    • ds参数是配合fixture的params参数用的,如果没有设置params参数,那么ids毫无意义;
    • ids参数是给每一项params参数设置自定义名称用的;
    • params参数值包含的列表有多少项值,那么ids参数就必须对应有多少项值
    • 示例:
  • name参数:给被装饰的方法设置别名
    -示例:

posted @ 2022-08-04 23:49  Tony_xiao  阅读(527)  评论(0编辑  收藏  举报