Python Pytest调用fixture之@pytest.mark.usefixtures()、叠加usefixtures、@pytest.fixture(autouse=True)用法详解
生活随笔
收集整理的這篇文章主要介紹了
Python Pytest调用fixture之@pytest.mark.usefixtures()、叠加usefixtures、@pytest.fixture(autouse=True)用法详解
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
usefixtures與傳fixture區別
?如果fixture有返回值,那么usefixture就無法獲取到返回值,這個是裝飾器usefixture與用例直接傳fixture參數的區別。
當fixture需要用到return出來的參數時,只能講參數名稱直接當參數傳入,不需要用到return出來的參數時,兩種方式都可以。
?
1.函數或類里面方法直接傳fixture的函數參數名稱
@pytest.fixture() def test1():print('\n開始執行function')def test_a(test1):print('---用例a執行---')class TestCase:def test_b(self,test1):print('---用例b執行')if __name__=="__main__":pytest.main(["-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test collected 2 itemstest03.py 開始執行function ---用例a執行--- . 開始執行function ---用例b執行 .============================== 2 passed in 0.04s ==============================Process finished with exit code 02、fixture自動使用autouse=True 當用例很多的時候,每次都傳這個參數,會很麻煩。fixture里面有個參數autouse,默認是False沒開啟的,可以設置為True開啟自動使用fixture功能,這樣用例就不用每次都去傳參了 autouse設置為True,自動調用fixture功能
@pytest.fixture(autouse=True) def test1():print('\n開始執行function')def test_a():print('---用例a執行---')class TestCase:def test_b(self):print('---用例b執行')if __name__=="__main__":pytest.main(["-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test collected 2 itemstest03.py 開始執行function ---用例a執行--- . 開始執行function ---用例b執行 .============================== 2 passed in 0.04s ==============================Process finished with exit code 0 @pytest.fixture(scope='module', autouse=True) def test1():print('\n開始執行module')@pytest.fixture(scope='class', autouse=True) def test2():print('\n開始執行class')@pytest.fixture(scope='function', autouse=True) def test3():print('\n開始執行function')def test_a():print('---用例a執行---')def test_d():print('---用例d執行---')class TestCase:def test_b(self):print('---用例b執行---')def test_c(self):print('---用例c執行---')if __name__=="__main__":pytest.main(["-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test collected 4 itemstest03.py 開始執行module開始執行class開始執行function ---用例a執行--- . 開始執行class開始執行function ---用例d執行--- . 開始執行class開始執行function ---用例b執行--- . 開始執行function ---用例c執行--- .============================== 4 passed in 0.05s ==============================Process finished with exit code 03、使用裝飾器@pytest.mark.usefixtures()修飾需要運行的用例
@pytest.fixture() def test1():print('\n開始執行function')@pytest.mark.usefixtures('test1') def test_a():print('---用例a執行---')@pytest.mark.usefixtures('test1') class TestCase:def test_b(self):print('---用例b執行---')def test_c(self):print('---用例c執行---')if __name__=="__main__":pytest.main(["-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test collected 3 itemstest03.py 開始執行function ---用例a執行--- . 開始執行function ---用例b執行--- . 開始執行function ---用例c執行--- .============================== 3 passed in 0.05s ==============================Process finished with exit code 04、疊加usefixtures
如果一個方法或者一個class用例想要同時調用多個fixture,可以使用@pytest.mark.usefixture()進行疊加。注意疊加順序,先執行的放底層,后執行的放上層。
@pytest.fixture() def test1():print('\n開始執行function1')@pytest.fixture() def test2():print('\n開始執行function2')@pytest.mark.usefixtures('test1') @pytest.mark.usefixtures('test2') def test_a():print('---用例a執行---')@pytest.mark.usefixtures('test2') @pytest.mark.usefixtures('test1') class TestCase:def test_b(self):print('---用例b執行---')def test_c(self):print('---用例c執行---')if __name__=="__main__":pytest.main(["-s","test03.py"])"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/test/test03.py ============================= test session starts ============================= platform win32 -- Python 3.5.2, pytest-5.1.2, py-1.8.0, pluggy-0.12.0 rootdir: C:\Users\wangli\PycharmProjects\Test\test collected 3 itemstest03.py 開始執行function2開始執行function1 ---用例a執行--- . 開始執行function1開始執行function2 ---用例b執行--- . 開始執行function1開始執行function2 ---用例c執行--- .============================== 3 passed in 0.04s ==============================Process finished with exit code 0?
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的Python Pytest调用fixture之@pytest.mark.usefixtures()、叠加usefixtures、@pytest.fixture(autouse=True)用法详解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java gzip rest_RestT
- 下一篇: java 假设当前时间_Java中与日期