日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Pytest前后置处理

發(fā)布時間:2024/2/28 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Pytest前后置处理 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Pytest前后置處理

Pytest框架實現(xiàn)一些前后置的處理,常用三種。

1.setup/teardown,setup_class/teardown_class

使用場景:用例執(zhí)行之前,需要做初始化操作;用例執(zhí)行結(jié)束之后,需要做資源清理
總配置文件pytest.ini

[pytest] addopts = -vs testpaths = testcase/test_setup_teardown.py python_files = test_*.py python_classes = Test* python_functions = test

1.1、setup/teardown

測試用例:

# -*- coding: UTF-8 -*- class TestSetupTeardown:def setup(self):print('\n在執(zhí)行測試用例之前初始化的代碼')def test_01_login(self):print('\n登錄')def test_02_browse(self):print('\n瀏覽網(wǎng)頁')def test_03_exit(self):print('\n退出')def teardown(self):print('\n清理資源')

命令行操作:

% pytest ============================================================================= test session starts ============================================================================= platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.9', 'Platform': 'Darwin-20.2.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'xdist': '2.3.0', 'html': '3.1.1', 'ordering': '0.6', 'rerunfailures': '10.1', 'metadata': '1.11.0', 'forked': '1.3.0'}} rootdir: /Users/xxx/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/test_setup_teardown.py plugins: xdist-2.3.0, html-3.1.1, ordering-0.6, rerunfailures-10.1, metadata-1.11.0, forked-1.3.0 collected 3 items testcase/test_setup_teardown.py::TestSetupTeardown::test_01_login 在執(zhí)行測試用例之前初始化的代碼登錄 PASSED 清理資源testcase/test_setup_teardown.py::TestSetupTeardown::test_02_browse 在執(zhí)行測試用例之前初始化的代碼瀏覽網(wǎng)頁 PASSED 清理資源testcase/test_setup_teardown.py::TestSetupTeardown::test_03_exit 在執(zhí)行測試用例之前初始化的代碼退出 PASSED 清理資源============================================================================== 3 passed in 0.01s ==============================================================================

1.2、setup_class/teardown_class

測試用例:

# -*- coding: UTF-8 -*- class TestSetupTeardown:def setup_class(self):"""在所有的用力之前只執(zhí)行一次:return:"""print('\n在每個類執(zhí)行前的初始化操作。比如:創(chuàng)建日志對象,創(chuàng)建數(shù)據(jù)庫的連接,創(chuàng)建接口的請求對象')def test_01_login(self):print('登錄')def test_02_browse(self):print('\n瀏覽網(wǎng)頁')def test_03_exit(self):print('\n退出')def teardown_class(self):print('\n在每個類執(zhí)行后的掃尾工作。比如:銷毀日志對象,銷毀數(shù)據(jù)庫的連接,銷毀接口的請求對象。')

命令行操作:

% pytest ============================================================================= test session starts ============================================================================= platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.9', 'Platform': 'Darwin-20.2.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'xdist': '2.3.0', 'html': '3.1.1', 'ordering': '0.6', 'rerunfailures': '10.1', 'metadata': '1.11.0', 'forked': '1.3.0'}} rootdir: /Users/xxx/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/test_setup_teardown.py plugins: xdist-2.3.0, html-3.1.1, ordering-0.6, rerunfailures-10.1, metadata-1.11.0, forked-1.3.0 collected 3 items testcase/test_setup_teardown.py::TestSetupTeardown::test_01_login 在每個類執(zhí)行前的初始化操作。比如:創(chuàng)建日志對象,創(chuàng)建數(shù)據(jù)庫的連接,創(chuàng)建接口的請求對象 登錄 PASSED testcase/test_setup_teardown.py::TestSetupTeardown::test_02_browse 瀏覽網(wǎng)頁 PASSED testcase/test_setup_teardown.py::TestSetupTeardown::test_03_exit 退出 PASSED 在每個類執(zhí)行后的掃尾工作。比如:銷毀日志對象,銷毀數(shù)據(jù)庫的連接,銷毀接口的請求對象。============================================================================== 3 passed in 0.01s ==============================================================================

2、使用@pytest.fixture()裝飾器來實現(xiàn)部分用例的前后置

@pytest.fixture(scope=‘function’, params=’’, autouse=’’, ids=’’, name=’’)

  • scope表示的是被@pytest.fixture標(biāo)記的方法的作用域。function(默認(rèn))、class、module、package/session
  • params:參數(shù)化(支持:列表[],元祖(),字典列表[{},{},{}],字典元組({},{},{}))
  • autouse=True:自動執(zhí)行,默認(rèn)False
  • ids:當(dāng)使用params參數(shù)化時,給每一個值設(shè)置一個變量名。意義不大
  • name:給被@pytest.fixture標(biāo)記的方法取一個別名

2.1、scope是function

手動調(diào)用前后置方法,好處是想讓哪些方法執(zhí)行前后置就讓哪些方法執(zhí)行前后置
用例:

# -*- coding: UTF-8 -*- import pytest@pytest.fixture(scope='function') def my_fixture():print('這是前置的方法,可以實現(xiàn)部分以及全部用例的前置')yield # 通過yield將前后置方法分隔print('\n這是后置的方法,可以實現(xiàn)部分以及全部用例的后置')class TestSetupTeardown:def test_01(self):print('沒有前置方法')def test_02(self, my_fixture): # 調(diào)用處print('有前置方法')

命令行操作:

% pytest ============================================================================= test session starts ============================================================================= platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.9', 'Platform': 'Darwin-20.2.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'xdist': '2.3.0', 'html': '3.1.1', 'ordering': '0.6', 'rerunfailures': '10.1', 'metadata': '1.11.0', 'forked': '1.3.0'}} rootdir: /Users/liyabin01/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/test_fixture.py plugins: xdist-2.3.0, html-3.1.1, ordering-0.6, rerunfailures-10.1, metadata-1.11.0, forked-1.3.0 collected 2 items testcase/test_fixture.py::TestSetupTeardown::test_01 沒有前置方法 PASSED testcase/test_fixture.py::TestSetupTeardown::test_02 這是前置的方法,可以實現(xiàn)部分以及全部用例的前置 有前置方法 PASSED 這是后置的方法,可以實現(xiàn)部分以及全部用例的后置============================================================================== 2 passed in 0.01s ==============================================================================

自動調(diào)用前后置方法,好處是方法可以自動執(zhí)行,但是不能控制哪些執(zhí)行哪些不執(zhí)行,全部作用域命中的方法都要執(zhí)行
用例:

# -*- coding: UTF-8 -*- import pytest @pytest.fixture(scope='function', autouse=True) # 調(diào)用處 def my_fixture():print('這是前置的方法,可以實現(xiàn)部分以及全部用例的前置')yieldprint('\n這是后置的方法,可以實現(xiàn)部分以及全部用例的后置') class TestSetupTeardown:def test_01(self):print('沒有前置方法')def test_02(self):print('有前置方法')

命令行操作,會發(fā)現(xiàn)test_01方法也加了前后置處理

% pytest ============================================================================= test session starts ============================================================================= platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.9', 'Platform': 'Darwin-20.2.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'xdist': '2.3.0', 'html': '3.1.1', 'ordering': '0.6', 'rerunfailures': '10.1', 'metadata': '1.11.0', 'forked': '1.3.0'}} rootdir: /Users/liyabin01/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/test_fixture.py plugins: xdist-2.3.0, html-3.1.1, ordering-0.6, rerunfailures-10.1, metadata-1.11.0, forked-1.3.0 collected 2 items testcase/test_fixture.py::TestSetupTeardown::test_01 這是前置的方法,可以實現(xiàn)部分以及全部用例的前置 沒有前置方法 PASSED 這是后置的方法,可以實現(xiàn)部分以及全部用例的后置testcase/test_fixture.py::TestSetupTeardown::test_02 這是前置的方法,可以實現(xiàn)部分以及全部用例的前置 有前置方法 PASSED 這是后置的方法,可以實現(xiàn)部分以及全部用例的后置============================================================================== 2 passed in 0.01s ==============================================================================

2.2、scope是class

fixture為class級別的時候,如果一個class里面有多個用例,都調(diào)用了此fixture,那么此fixture只在該class里所有用例開始前執(zhí)行一次
(1)驗證執(zhí)行次數(shù)
用例:

# -*- coding: UTF-8 -*- import pytest@pytest.fixture(scope="class") def first():print("\n獲取用戶名,scope為class級別只運行一次")a = "yoyo"return aclass TestCase():def test_1(self, first):'''用例傳fixture'''print("測試賬號:%s" % first)assert first == "yoyo"def test_2(self, first):'''用例傳fixture'''print("測試賬號:%s" % first)assert first == "yoyo"

命令行操作:

% pytest testcase/test_fixture.py::TestCase::test_1 獲取用戶名,scope為class級別只運行一次 測試賬號:yoyo PASSED testcase/test_fixture.py::TestCase::test_2 測試賬號:yoyo PASSED

(2)驗證作用域
用例:

# -*- coding: UTF-8 -*- import pytest @pytest.fixture(scope='class', autouse=True) def my_fixture():print('\n這是前置的類方法,可以實現(xiàn)部分以及全部用例的前置')yieldprint('\n這是后置的類方法,可以實現(xiàn)部分以及全部用例的后置') class TestSetupTeardown1:def test_01(self):print('我是測試方法1') class TestSetupTeardown2:def test_02(self):print('我是測試方法2')

命令行操作:

% pytest ============================================================================= test session starts ============================================================================= platform darwin -- Python 3.7.9, pytest-6.2.4, py-1.10.0, pluggy-0.13.1 -- /Users/xxx/opt/anaconda3/envs/py37/bin/python cachedir: .pytest_cache metadata: {'Python': '3.7.9', 'Platform': 'Darwin-20.2.0-x86_64-i386-64bit', 'Packages': {'pytest': '6.2.4', 'py': '1.10.0', 'pluggy': '0.13.1'}, 'Plugins': {'xdist': '2.3.0', 'html': '3.1.1', 'ordering': '0.6', 'rerunfailures': '10.1', 'metadata': '1.11.0', 'forked': '1.3.0'}} rootdir: /Users/liyabin01/PycharmProjects/pytestDemo, configfile: pytest.ini, testpaths: testcase/test_fixture.py plugins: xdist-2.3.0, html-3.1.1, ordering-0.6, rerunfailures-10.1, metadata-1.11.0, forked-1.3.0 collected 2 items testcase/test_fixture.py::TestSetupTeardown1::test_01 這是前置的類方法,可以實現(xiàn)部分以及全部用例的前置 我是測試方法1 PASSED 這是后置的類方法,可以實現(xiàn)部分以及全部用例的后置testcase/test_fixture.py::TestSetupTeardown2::test_02 這是前置的類方法,可以實現(xiàn)部分以及全部用例的前置 我是測試方法2 PASSED 這是后置的類方法,可以實現(xiàn)部分以及全部用例的后置============================================================================== 2 passed in 0.01s ==============================================================================

2.3、params

簡單用例:

# -*- coding: UTF-8 -*- import pytest@pytest.fixture(scope='function', params=['成龍', '甄子丹', '李小龍']) def get_data(request):return request.paramclass TestFixture:def test_01(self):print('我是吳奇隆')def test_02(self, get_data):print('我是{}'.format(get_data))

命令行操作:

% pytest testcase/test_fixture.py::TestFixture::test_01 我是吳奇隆 PASSED testcase/test_fixture.py::TestFixture::test_02[\u6210\u9f99] 我是成龍 PASSED testcase/test_fixture.py::TestFixture::test_02[\u7504\u5b50\u4e39] 我是甄子丹 PASSED testcase/test_fixture.py::TestFixture::test_02[\u674e\u5c0f\u9f99] 我是李小龍 PASSED

可以觀察到test_02方法調(diào)用了三次,固定寫法。

3、通過conftest.py和@pytest.fixture()結(jié)合實現(xiàn)全局的前置應(yīng)用(比如:項目的全局登錄,模塊的全局處理)

1.conftest.py文件是單獨存放的一個夾具(@pytest.fixture())配置文件,名稱不能更改。
2.用處可以在不同的py文件中使用同一個fixture函數(shù)
3.原則上conftest.py需要和運行的用例放在同一層。并且不需要做任何的import導(dǎo)入操作。
總結(jié):

  • setup/teardown,setup_class/teardown_class:它是作用域所有用例或者所有類
  • @pytest.fixture():它的作用是既可以部分也可以全部前后置
  • conftest.py和@pytest.fixture()結(jié)合使用,作用域全局的前后置。
    用例:
    pytest.ini
[pytest] addopts = -vs testpaths = testcase/ python_files = test_*.py python_classes = Test* python_functions = test

項目目錄結(jié)構(gòu):

conftest.py

import pytest@pytest.fixture(scope='function') # conftest.py def all_fixture():print('\n全局的前置')yieldprint('\n全局的后置')@pytest.fixture(scope='function') # product/conftest.py def product_fixture():print('\nproduct的前置')yieldprint('\nproduct的后置')@pytest.fixture(scope='function') # user/conftest.py def user_fixture():print('\nuser的前置')yieldprint('\nuser的后置')

test_product.py/test_user.py

import pytestclass TestProduct:def test_product(self, all_fixture, product_fixture):print('我是一個product~')class TestUser:def test_user(self, user_fixture):print('我是一個user~')

命令行操作:

% pytest ============================================================================= test session starts =============================================================================collected 2 items testcase/product/test_product.py::TestProduct::test_product 全局的前置product的前置 我是一個product~ PASSED product的后置全局的后置testcase/user/test_user.py::TestUser::test_user user的前置 我是一個user~ PASSED user的后置============================================================================== 2 passed in 0.02s ==============================================================================

上述執(zhí)行結(jié)果可以看出,@pytest.fixture的生效順序是函數(shù)中調(diào)用的順序
def test_product(self, all_fixture, product_fixture)

總結(jié)

以上是生活随笔為你收集整理的Pytest前后置处理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。