软件测试|Pytest必会技巧(四)使用autouse实现自动传参

举报
霍格沃兹测试开发 发表于 2023/02/10 16:15:08 2023/02/10
1.1k+ 0 0
【摘要】 Pytest fixture之autouse使用写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦。fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了。调用fixture三种方法函数或类里面方法直接传fixtu...

Pytest fixture之autouse使用

写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦。fixture里面有个参数autouse,默认是Fasle没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了。

调用fixture三种方法

  • 函数或类里面方法直接传fixture的函数参数名称
  • 使用装饰器@pytest.mark.usefixtures()修饰
  • autouse=True自动使用

使用fixture传参

先定义start功能,用例全部传start参数,调用该功能

import pytest
@pytest.fixture(scope="function")
def start(request):
    print('\n-----开始执行function----')
def test_a(start):
    print("-------用例a执行-------")
class Test_aaa():

    def test_01(self, start):
        print('-----------用例01--------------')

    def test_02(self, start):
        print('-----------用例02------------')

if __name__ == "__main__":
    pytest.main(["-vs", "test_demo.py"])

============================= test session starts =============================
collecting ... collected 3 items

test_demo.py::test_a 
-----开始执行function----
PASSED                                              [ 33%]-------用例a执行-------

test_demo.py::Test_aaa::test_01 
-----开始执行function----
PASSED                                   [ 66%]-----------用例01--------------

test_demo.py::Test_aaa::test_02 
-----开始执行function----
PASSED                                   [100%]-----------用例02------------
============================== 3 passed in 0.03s ==============================

Process finished with exit code 0

使用usefixtures传参

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

import pytest

@pytest.fixture(scope="function")
def start(request):
    print('\n-----开始执行function----')
@pytest.mark.usefixtures("start")
def test_a():
    print("-------用例a执行-------")

@pytest.mark.usefixtures("start")
class Test_aaa():

    def test_01(self):
        print('-----------用例01--------------')

    def test_02(self):
        print('-----------用例02------------')

if __name__ == "__main__":
    pytest.main(["-vs", "test_demo.py"])

============================= test session starts =============================
collecting ... collected 3 items

test_demo.py::test_a 
-----开始执行function----
PASSED                                              [ 33%]-------用例a执行-------

test_demo.py::Test_aaa::test_01 
-----开始执行function----
PASSED                                   [ 66%]-----------用例01--------------

test_demo.py::Test_aaa::test_02 
-----开始执行function----
PASSED                                   [100%]-----------用例02------------
============================== 3 passed in 0.02s ==============================

设置autouse=True

autouse设置为True,自动调用fixture功能

  • 设置scope为module级别,在当前.py用例模块只执行一次,autouse=True自动使用
  • 设置scope为function级别,每个用例前都调用一次,自动使用
import pytest
@pytest.fixture(scope="module", autouse=True)
def start(request):
    print('\n-----开始执行moule----')
    print('module      : %s' % request.module.__name__)
    print('----------启动浏览器---------')
    yield
    print("------------结束测试 end!-----------")
@pytest.fixture(scope="function", autouse=True)
def open_home(request):
    print("function:%s \n--------回到首页--------" % request.function.__name__)
def test_01():
    print('-----------用例01--------------')

def test_02():
    print('-----------用例02------------')

if __name__ == "__main__":
    pytest.main(["-vs", "test_demo.py"])

============================= test session starts =============================
collecting ... collected 2 items

test_demo.py::test_01 
-----开始执行moule----
module      : demos.test_demo
----------启动浏览器---------
function:test_01 
--------回到首页--------
PASSED                                             [ 50%]-----------用例01--------------

test_demo.py::test_02 function:test_02 
--------回到首页--------
PASSED                                             [100%]-----------用例02------------
------------结束测试 end!-----------
============================== 2 passed in 0.02s ==============================

上面是写在函数里,写在类里一样可以使用

import pytest
@pytest.fixture(scope="module", autouse=True)
def start(request):
    print('\n-----开始执行moule----')
    print('module      : %s' % request.module.__name__)
    print('----------启动浏览器---------')
    yield
    print("------------结束测试 end!-----------")
class Test_aaa():
    @pytest.fixture(scope="function", autouse=True)
    def open_home(self, request):
        print("function:%s \n--------回到首页--------" % request.function.__name__)
    def test_01(self):
        print('-----------用例01--------------')

    def test_02(self):
        print('-----------用例02------------')

if __name__ == "__main__":
    pytest.main(["-vs", "test_demo.py"])

============================= test session starts =============================
collecting ... collected 2 items

test_demo.py::Test_aaa::test_01 
-----开始执行moule----
module      : demos.test_demo
----------启动浏览器---------
function:test_01 
--------回到首页--------
PASSED                                   [ 50%]-----------用例01--------------

test_demo.py::Test_aaa::test_02 function:test_02 
--------回到首页--------
PASSED                                   [100%]-----------用例02------------
------------结束测试 end!-----------
============================== 2 passed in 0.01s ==============================

【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

作者其他文章

评论(0

抱歉,系统识别当前为高风险访问,暂不支持该操作

    全部回复

    上滑加载中

    设置昵称

    在此一键设置昵称,即可参与社区互动!

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

    *长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。