小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

pytest---usefixtures簡單實用

 Coder編程 2022-08-15 發(fā)布于北京

  前面介紹的fixture都是通過函數(shù)的方式進(jìn)行傳參的,如果遇到一個class中都需要傳入一個fixture,那一個個寫進(jìn)函數(shù)中,太麻煩,今天安靜介紹一種方法,通過class直接傳入fixture

usefixtures

usefixtures是fixture用來標(biāo)記class中的一個方法。用法結(jié)果相當(dāng)于和setup和teardown的結(jié)果一樣。每個用例函數(shù)都會進(jìn)行執(zhí)行fixture中的前置內(nèi)容和后置內(nèi)容

#coding:utf-8

import pytest


@pytest.fixture()
def login():
    print('輸入用戶名,密碼。完成登錄')
    yield
    print('退出')

@pytest.mark.usefixtures('login')
class Test_01:

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

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

    def test_03(self):
        print('---用例03---')


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

通過執(zhí)行結(jié)果可以發(fā)現(xiàn),通過使用usefixture方法后,class下的每個用例都會執(zhí)行前置和后置內(nèi)容。

多個usefixture使用

如果我們需要對一個class中傳入多個fixture,我們就可以直接疊加2個usefixture方法,有2種寫法:

第一種:

直接在usefixture中進(jìn)行加入另外的fixture方法名稱

#coding:utf-8

import pytest

@pytest.fixture()
def fun():
    print('這是fun的fixture方法')

@pytest.fixture()
def login():
    print('輸入用戶名,密碼。完成登錄')
    yield
    print('退出')


@pytest.mark.usefixtures('login','fun')
class Test_01:

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

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

    def test_03(self):
        print('---用例03---')


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

這種方法的執(zhí)行順序,那個fixture名稱在前面,就先執(zhí)行那個方法

 

第2種:

通過疊加的形式進(jìn)行傳參,再次創(chuàng)建一個usefixture方法

#coding:utf-8

import pytest

@pytest.fixture()
def fun():
    print('這是fun的fixture方法')

@pytest.fixture()
def login():
    print('輸入用戶名,密碼。完成登錄')
    yield
    print('退出')


@pytest.mark.usefixtures('login')
@pytest.mark.usefixtures('fun')
class Test_01:

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

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

    def test_03(self):
        print('---用例03---')


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

 

這種方法的執(zhí)行順序,那個usefixture方法在下面,先執(zhí)行那個。

 

混合使用

我們還有一種使用方法就是函數(shù)的fixture和類的usefixture同時使用

#coding:utf-8

import pytest

@pytest.fixture()
def fun():
    print('這是fun的fixture方法')

@pytest.fixture()
def login():
    print('輸入用戶名,密碼。完成登錄')
    yield
    print('退出')


@pytest.mark.usefixtures('login')
class Test_01:

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

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

    def test_03(self,fun):
        print('---用例03---')


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

 

通過上面執(zhí)行結(jié)果可以看出來,usefixture和fixture可以同時使用,執(zhí)行的順序肯定是先usefixture然后在fixture。

 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多