詳解pytest傳遞參數(shù)的幾種方式
測(cè)試類(lèi)內(nèi)部,屬性傳遞
import pytest class Test_Case: t = 0 def test_c(self): self.t = self.t + 1 assert self.t == 1 def test_d(self): self.t = self.t + 1 assert self.t == 1 # t是測(cè)試類(lèi)的屬性,可以為所有測(cè)試方法共享該值,該值是固定不變的
global方式傳遞
import pytest s = {} class Test_Case: def test_b(self): global s s['name'] = 'hello' print(s['name']) assert s['name'] == 'hello' def test_c(self): global s s['age'] = 18 print(s) assert s['age'] == 18 # global聲明的變量可以在整個(gè)測(cè)試類(lèi)中共享,值是可變的,global可以去掉,效果相同
@pytest.mark.parametrize()
import pytest class Test_Case: @pytest.mark.parametrize("x", [1, 2, 3, 4]) # 傳遞單個(gè)值 def test_b(self, x): assert x != 5 @pytest.mark.parametrize("x,y", [(1, 2), (3, 4), (2, 3), (4, 6)]) # 多參數(shù),傳遞元組 def test_c(self, x, y): print(x + y) assert x + y != 5 @pytest.mark.parametrize("x,y", [{1, 2}, {3, 4}, {2, 3}, {4, 6}]) # 多參數(shù)傳遞集合 def test_d(self, x, y): print(x + y) assert x + y != 6 @pytest.mark.parametrize("x", [{"a": 1, "b": 2}, {"a": 1, "c": 4}]) # 傳遞字典 def test_e(self, x): print(x) assert x["a"] == 1 @pytest.mark.parametrize("x,y", [({"a": 1, "b": 2}, {"a": 3, "c": 4})]) # 多參數(shù)傳遞字典 def test_f(self, x, y): assert x["a"] == 1 @pytest.mark.parametrize("x", [{"a": 1, "b": 2}]) # 裝飾器疊加,傳遞多參數(shù) @pytest.mark.parametrize("y", [{"a": 1, "b": 2}]) def test_g(self, x, y): assert y["a"] == 1 @pytest.mark.parametrize( "test_input,expected", [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.xfail)], ) # xfail標(biāo)記 def test_h(self, test_input, expected): assert eval(test_input) == expected @pytest.mark.parametrize( "test_input,expected", [("3+5", 8), ("2+4", 6), pytest.param("6*9", 42, marks=pytest.mark.skip)], ) # skip標(biāo)記 def test_i(self, test_input, expected): assert eval(test_input) == expected
fixtrue傳遞
import pytest class Test_Case: @pytest.fixture def get_d(self): # 通過(guò)fixture值傳遞 return [1, 2, 3] def test_a(self, get_d): x = get_d[0] assert x == 1
import pytest class Test_Case: # params = 'hello'等同于params = ['h','e','l','l','o'] @pytest.fixture(params='hello') def get_c(self, request): print(request.param) return request.param def test_c(self, get_c): name = get_c assert name == 'h' @pytest.fixture(params=[1, 2], ids=['hello', 'name']) # 可以通過(guò)pytest -k <ids>執(zhí)行指定的用例 def get_d(self, request): return request.param def test_d(self, get_d): name = get_d assert name == 2 @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)]) def data_set(self, request): return request.param def test_f(self): pass
import pytest #fixture嵌套傳遞 class Test_Case: @pytest.fixture(params=[0, 1, pytest.param(2, marks=pytest.mark.skip)]) def data_set(self, request): return request.param @pytest.fixture() def data_s(self, data_set): print(data_set) return data_set def test_g(self, data_s): assert data_s == 1
# yield傳遞 import pytest class Test_Case: @pytest.fixture def s(self): c = 'test' yield c def test_name(self, s): assert s == "test"
配置文件傳遞
# test_case.py import pytest import _case.constant as d class Test_Case: def test_g(self): d.data = 2 assert d.data == 2 def test_h(self): assert d.data == 2 # _case.constant.py data = 1 # 和global使用類(lèi)似,多個(gè)測(cè)試文件共享值,但是多個(gè)文件共享該值時(shí),會(huì)收到測(cè)試文件的執(zhí)行順序影響 # global只能在一個(gè)測(cè)試文件中共享值
conftest.py
# conftest.py最好是在項(xiàng)目根目錄或者測(cè)試文件所在目錄 import pytest @pytest.fixture(scope='session') def say(): return 'hello' # test_case.py import pytest class Test_Case: def test_g(self, say): assert say == 'hello'
命令行參數(shù)傳參
# conftest.py 全局變量使用 import pytest def pytest_addoption(parser): parser.addoption("--file", default="test") @pytest.fixture def file_name(request): return request.config.getoption("--file") # test_case.py import pytest class Test_Case: def test_name(self, file_name): assert file_name == "test" # test_case.py或者直接在測(cè)試文件中通過(guò)pytestconfig獲取,示例如下 def test_name(self, pytestconfig): print(pytestconfig.getoption('file')) assert pytestconfig.getoption("file") == "test"
鉤子函數(shù)傳參
# conftest.py import pytest def pytest_addoption(parser): parser.addoption("--file", default="test") def pytest_generate_tests(metafunc): file = metafunc.config.getoption('--file') metafunc.parametrize("case_data", [file]) # test_case.py import pytest class Test_Case: def test_g(self, case_data): assert case_data == 'test'
到此這篇關(guān)于詳解pytest傳遞參數(shù)的幾種方式的文章就介紹到這了,更多相關(guān)pytest傳遞參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法
這篇文章主要介紹了Python實(shí)現(xiàn)自動(dòng)登錄百度空間的方法,涉及Python的http請(qǐng)求發(fā)送、獲取響應(yīng)、cookie操作等相關(guān)技巧,需要的朋友可以參考下2017-06-06python+POP3實(shí)現(xiàn)批量下載郵件附件
這篇文章主要為大家詳細(xì)介紹了python+POP3實(shí)現(xiàn)批量下載郵件附件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Python lambda 匿名函數(shù)優(yōu)點(diǎn)和局限性深度總結(jié)
這篇文章主要為大家介紹了Python lambda 匿名函數(shù)的優(yōu)點(diǎn)和局限性深度總結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08python判斷設(shè)備是否聯(lián)網(wǎng)的方法
這篇文章主要為大家詳細(xì)介紹了python判斷設(shè)備是否聯(lián)網(wǎng)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06python調(diào)用HEG工具批量處理MODIS數(shù)據(jù)的方法及注意事項(xiàng)
這篇文章主要介紹了python調(diào)用HEG工具批量處理MODIS數(shù)據(jù)的方法,本文給大家提到了注意事項(xiàng),通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02python3+telnetlib實(shí)現(xiàn)簡(jiǎn)單自動(dòng)測(cè)試示例詳解
telnetlib 模塊提供一個(gè)實(shí)現(xiàn)Telnet協(xié)議的類(lèi) Telnet,本文重點(diǎn)給大家介紹python3+telnetlib實(shí)現(xiàn)簡(jiǎn)單自動(dòng)測(cè)試示例詳解,需要的朋友可以參考下2021-08-08Python實(shí)現(xiàn)統(tǒng)計(jì)mp4/avi視頻的時(shí)長(zhǎng)
moviepy是一個(gè)用于處理視頻和音頻的Python庫(kù),它提供了一組功能豐富的工具,所以本文將利用它實(shí)現(xiàn)統(tǒng)計(jì)mp4/avi視頻的時(shí)長(zhǎng),希望對(duì)大家有所幫助2023-07-07ITK 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例
這篇文章主要介紹了ITK 實(shí)現(xiàn)多張圖像轉(zhuǎn)成單個(gè)nii.gz或mha文件案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07