zoukankan      html  css  js  c++  java
  • Pytest之fixture简单使用

    最近在写自动化脚本的时候遇到一个问题,两个用例或者两个方法之间需要进行参数传递该什么做呢?这时候fixture就派上用场了。

    一、fixture写法

    fixture需要在函数上加个装饰器@pytest.fixture()。

    fixture命令最好不要以test开头,跟用例区分开来。

    fixture是有返回值的,没有返回值的话默认为None。

    例子1:

    fixture返回一个普通字符,如果用例需要调用fixture的返回值,直接就是把fixture的函数名称当做变量名称。

    import pytest
    
    @pytest.fixture()
    def name():
    name = "Lily"
    return name
    
    def test2(name):
    assert name == "Lily"
    
    if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture.py'])

    例子2:fixture返回多个值。可以返回一个元祖、列表或者字典,然后从里面取出对应数据。

    import pytest
    @pytest.fixture()
    def name():
    Chinese_name = "Zhang Lili"
    English_name = "Lily"
    return (Chinese_name, English_name)
    
    def test1(name):
    Chinese_name = name[0]
    English_name = name[1]
    assert Chinese_name == "Zhang Lili"
    assert English_name == "Lily"

    if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture.py'])

    二、调用fixture的常用方法

    1、在函数或者类里面直接传fixture的函数名称

    import pytest
    @pytest.fixture()
    def name():
    name = 'Lily'
    return name
    
    def test1(name):
    print(name)
    
    class Testcase:
    def test2(self,name):
    print(name)
    
    if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture1.py'])

    2、使用装饰器@pytest.mark.usefixtures()修饰需要运行的用例

    import pytest
    @pytest.fixture()
    def name():
    name = "Lily"
    return name
    
    @pytest.mark.usefixtures('name')
    def test1():
    print(name)
    
    @pytest.mark.usefixtures('name')
    class Testcase:
    def test2(self):
    print(name)
    
    def test3(self):
    print(name)
    
    
    if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture1.py'])

    3、叠加使用usefixtures,注意叠加顺序,先执行的放底层,后执行的放上层。

    import pytest
    @pytest.fixture()
    def name1():
    print('---开始执行name---')
    
    @pytest.fixture()
    def name2():
    print('---开始执行name1---')
    
    @pytest.mark.usefixtures('name1')
    @pytest.mark.usefixtures('name2')
    def test1():
    print('---执行test1---')
    
    @pytest.mark.usefixtures('name2')
    @pytest.mark.usefixtures('name1')
    class Testcase():
    def test2(self):
    print('---执行test2---')
    
    def test3(self):
    print('---执行test3---')
    
    if __name__ == '__main__':
    pytest.main(['-s', 'test_fixture1.py'])
    4、usefixtures与传fixture区别
    如果fixture有返回值,那么usefixture就无法获取到返回值,这个是装饰器usefixture与用例直接传fixture参数的区别。
    当fixture需要用到return出来的参数时,只能讲参数名称直接当参数传入,不需要用到return出来的参数时,两种方式都可以。
     
    三、fixture参数简单说明
    fixture(scope='function',params=None,autouse=False,ids=None,name=None)
    • scope:有四个级别参数"function"(默认),"class","module","session":

       --scope="function",@pytest.fixture()如果不写参数,参数就是scope="function",它的作用范围是每个测试用例来之前运行一次,销毁代码在测试用例之后运行。

       --scope="class",fixture为class级别的时候,如果一个class里面有多个用例,都调用了次fixture,那么此fixture只在此class里所有用例开始前执行一次。

       --scope="module",fixture为module时,在当前.py脚本里面所有用例开始前只执行一次。

               --scope="session",fixture为session级别是可以跨.py模块调用的,也就是当我们有多个.py文件的用例的时候,如果多个用例只需调用一次fixture,那就可以设置为scope="session",并且写到conftest.py文件里。(备注:conftest.py文件名称时固定的,pytest会自动识别该文件。放到项目的根目录下就可以全局调用了,如果放到某个package下,那就在改package内有效。)

    • params:一个可选的参数列表,它将导致多个参数调用fixture功能和所有测试使用它。
    • autouse:如果True,则为所有测试激活fixture func可以看到它。如果为False则显示需要参考来激活fixture
    •    --autouse参数默认是False没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了。格式:@pytest.fixture(scope='module', autouse=True)
    • ids:每个字符串id的列表,每个字符串对应于params这样他们就是测试ID的一部分。如果没有提供ID它们将从params自动生成
    • name:fixture的名称。这默认为装饰函数的名称。如果fixture在定义它的统一模块中使用,夹具的功能名称将被请求夹具的功能arg遮蔽,解决这个问题的一种方法时将装饰函数命令"fixture_<fixturename>"然后使用"@pytest.fixture(name='<fixturename>')"。
     
  • 相关阅读:
    112.路径总和
    二叉树的中序遍历
    HTML基础及案例
    web概念概述
    Spring JDBC
    数据库连接池
    JDBC连接池&JDBCTemplate
    JDBC
    MySQL多表&事务
    DCL
  • 原文地址:https://www.cnblogs.com/daydayup-lin/p/14180804.html
Copyright © 2011-2022 走看看