pytest--fixture值autouse=True

前言

平常写自动化用例会写一些前置的fixture操作,用例需要用到就直接传该函数的参数名称就行了。当用例很多的时候,每次都传这个参数,会比较麻烦。

fixture里面有个参数autouse,默认是False没开启的,可以设置为True开启自动使用fixture功能,这样用例就不用每次都去传参了。

调用fixture三种方法

  1.函数或类里面方法直接传fixture的函数参数名称

  2.使用装饰器@pytest.mark.usefixtures()修饰

  3.autouse=True自动使用

用例传fixture参数

方法一:先定义go功能(go可以改为任意名字),用例全部传go参数,调用该功能

#test_fix1.py
import pytest
@pytest.fixture(scope="function")
def go(request):
    print("\n----开始执行function----")
def test_1(go):
    print("---用例1执行---")
class Test_aaa():
    def test_2(self,go):
        print("-----用例2----")
    def test_3(self,go):
        print("---用例3---")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 
----开始执行function----
.---用例1执行---
                                                          [100%]

============================== 3 passed in 0.11s ==============================

Process finished with exit code 0

----开始执行function----
.-----用例2----

----开始执行function----
.---用例3---

装饰器usefixtures

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

#test_fix1.py
import pytest
@pytest.fixture(scope="function")
def go(request):
    print("\n----开始执行function----")
 
@pytest.mark.usefixtures("go")
def test_1():
    print("---用例1执行---")
 
@pytest.mark.usefixtures("go")
class Test_aaa():
    def test_2(self):
        print("-----用例2----")
    def test_3(self):
        print("---用例3---")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 
----开始执行function----
.---用例1执行---
                                                          [100%]

============================== 3 passed in 0.12s ==============================

Process finished with exit code 0

----开始执行function----
.-----用例2----

----开始执行function----
.---用例3---

设置autouse=True

方法三、autouse设置为True,自动调用fixture功能

  go设置scope为module级别,在当前.py用例模块只执行一次,autouse=True自动使用。

  xixi设置scope为function级别,每个用例前都调用一次,自动使用。

#test_fix1.py
import pytest
@pytest.fixture(scope="module",autouse=True)
def go(request):
    print("\n----开始执行module----")
    print("module:{}".format(request.module.__name__))
    print("-=-=启动浏览器-=-=")
    yield
    print("=-=-结束测试 end =-=-")
 
@pytest.fixture(scope="function",autouse=True)
def xixi(request):
    print("function:{} \n===回到首页===".format(request.function.__name__))
 
def test_1():
    print("---用例1执行---")
 
class Test_aaa():
    def test_2(self):
        print("-----用例2----")
    def test_3(self):
        print("---用例3---")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"]) 

运行结果

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 
----开始执行module----
module:xyautotest.test_001
-=-=启动浏览器-=-=
function:test_1 
===回到首页===
.---用例1执行---
                                                          [100%]

============================== 3 passed in 0.13s ==============================

Process finished with exit code 0
function:test_2 
===回到首页===
.-----用例2----
function:test_3 
===回到首页===
.---用例3---
=-=-结束测试 end =-=-

上面函数写到class里,作用域就只有类里的方法了。

#test_fix1.py
import pytest
@pytest.fixture(scope="module",autouse=True)
def go(request):
    print("\n----开始执行module----")
    print("module:{}".format(request.module.__name__))
    print("-=-=启动浏览器-=-=")
    yield
    print("=-=-结束测试 end =-=-")
 
def test_1():
    print("---用例1执行---")
 
class Test_aaa():
    @pytest.fixture(scope="function", autouse=True)
    def xixi(self,request):
        print("\n function:{} ===回到首页===".format(request.function.__name__))
 
    def test_2(self):
        print("-----用例2----")
    def test_3(self):
        print("---用例3---")
if __name__=="__main__":
    pytest.main(["-s","test_fix1.py"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 3 items

test_001.py 
----开始执行module----
module:xyautotest.test_001
-=-=启动浏览器-=-=
.---用例1执行---
                                                          [100%]

============================== 3 passed in 0.12s ==============================

Process finished with exit code 0

 function:test_2 ===回到首页===
.-----用例2----

 function:test_3 ===回到首页===
.---用例3---
=-=-结束测试 end =-=-
posted @ 2021-09-13 13:45  qiupeng  阅读(162)  评论(0)    收藏  举报