国产亚洲精品福利在线无卡一,国产精久久一区二区三区,亚洲精品无码国模,精品久久久久久无码专区不卡

當(dāng)前位置: 首頁(yè) > news >正文

建站abc做的網(wǎng)站穩(wěn)定營(yíng)銷(xiāo)方案包括哪些內(nèi)容

建站abc做的網(wǎng)站穩(wěn)定,營(yíng)銷(xiāo)方案包括哪些內(nèi)容,手機(jī)設(shè)計(jì)圖軟件app,青島城陽(yáng)網(wǎng)站建設(shè)上篇博文介紹過(guò),Pytest是目前比較成熟功能齊全的測(cè)試框架,使用率肯定也不斷攀升。 在實(shí)際工作中,許多測(cè)試用例都是類(lèi)似的重復(fù),一個(gè)個(gè)寫(xiě)最后代碼會(huì)顯得很冗余。這里,我們來(lái)了解一下pytest.mark.parametrize裝飾器&…

上篇博文介紹過(guò),Pytest是目前比較成熟功能齊全的測(cè)試框架,使用率肯定也不斷攀升。

在實(shí)際工作中,許多測(cè)試用例都是類(lèi)似的重復(fù),一個(gè)個(gè)寫(xiě)最后代碼會(huì)顯得很冗余。這里,我們來(lái)了解一下@pytest.mark.parametrize裝飾器,可以很好解決上述問(wèn)題。

源代碼分析

  1. def parametrize(self,argnames, argvalues, indirect=False, ids=None, scope=None):

  2.   """ Add new invocations to the underlying test function using the list

  3.     of argvalues for the given argnames. Parametrization is performed

  4.   during the collection phase. If you need to setup expensive resources

  5. see about setting indirect to do it rather at test setup time.  # 使用給定argnames的argValue列表向基礎(chǔ)測(cè)試函數(shù)添加新的調(diào)用,在收集階段執(zhí)行參數(shù)化。

  6. :arg argnames: a comma-separated string denoting one or more argument

  7. names, or a list/tuple of argument strings.  # 參數(shù)名:使用逗號(hào)分隔的字符串,列表或元祖,表示一個(gè)或多個(gè)參數(shù)名

  8. :arg argvalues: The list of argvalues determines how often a

  9. test is invoked with different argument values. If only one

  10. argname was specified argvalues is a list of values. If N

  11. argnames were specified, argvalues must be a list of N-tuples,

  12. where each tuple-element specifies a value for its respective

  13. argname.  # 參數(shù)值:只有一個(gè)argnames,argvalues則是值列表。有N個(gè)argnames時(shí),每個(gè)元祖對(duì)應(yīng)一組argnames,所有元祖組合成一個(gè)列表

  14. :arg indirect: The list of argnames or boolean. A list of arguments'

  15. names (self,subset of argnames). If True the list contains all names from

  16. the argnames. Each argvalue corresponding to an argname in this list will

  17. be passed as request.param to its respective argname fixture

  18. function so that it can perform more expensive setups during the

  19. setup phase of a test rather than at collection time.

  20. :arg ids: list of string ids, or a callable.

  21. If strings, each is corresponding to the argvalues so that they are

  22. part of the test id. If None is given as id of specific test, the

  23. automatically generated id for that argument will be used.

  24. If callable, it should take one argument (self,a single argvalue) and return

  25. a string or return None. If None, the automatically generated id for that

  26. argument will be used.

  27. If no ids are provided they will be generated automatically from

  28. the argvalues.  # ids:字符串列表,可以理解成標(biāo)題,與用例個(gè)數(shù)保持一致

  29. :arg scope: if specified it denotes the scope of the parameters.

  30. The scope is used for grouping tests by parameter instances.

  31. It will also override any fixture-function defined scope, allowing

  32. to set a dynamic scope using test context or configuration.  

  33.   # 如果指定,則表示參數(shù)的范圍。作用域用于按參數(shù)實(shí)例對(duì)測(cè)試進(jìn)行分組。

  34.    它還將覆蓋任何fixture函數(shù)定義的范圍,允許使用測(cè)試上下文或配置設(shè)置動(dòng)態(tài)范圍。

  35. """

argnames

釋義:參數(shù)名稱(chēng)。

格式:字符串"arg1,arg2,arg3"。

aegvalues

釋義:參數(shù)值列表。

格式:必須是列表,如[val1,val2,val3]。

  • 單個(gè)參數(shù),里面是值的列表,如@pytest.mark.parametrize("name",["Jack","Locus","Bill"]);

  • 多個(gè)參數(shù),需要用元祖來(lái)存放值,一個(gè)元祖對(duì)應(yīng)一組參數(shù)的值,如@pytest.mark.parametrize("user,age",[("user1",15),("user2",24),("user3",25)])。

ids

釋義:可以理解為用例的id。

格式:字符串列表,如["case1","case2","case3"]。

indirect

釋義:當(dāng)indirect=True時(shí),若傳入的argnames是fixture函數(shù)名,此時(shí)fixture函數(shù)名將成為一個(gè)可執(zhí)行的函數(shù),argvalues作為fixture的參數(shù),執(zhí)行fixture函數(shù),最終結(jié)果再存入request.param。

當(dāng)indirect=False時(shí),fixture函數(shù)只作為一個(gè)參數(shù)名給測(cè)試收集階段調(diào)用。

備注:這里可以將the setup phase(測(cè)試設(shè)置階段)理解為配置 conftest.py 階段,將the collection phase(測(cè)試收集階段)理解為用例執(zhí)行階段。

裝飾測(cè)試類(lèi)

  1. import pytest

  2. data = [

  3. (2,2,4),

  4. (3,4,12)

  5. ]

  6. def add(a,b):

  7. return a * b

  8. @pytest.mark.parametrize('a,b,expect',data)

  9. class TestParametrize(object):

  10. def test_parametrize_1(self,a,b,expect):

  11. print('\n測(cè)試函數(shù)1測(cè)試數(shù)據(jù)為\n{}-{}'.format(a,b))

  12. assert add(a,b) == expect

  13. def test_parametrize_2(self,a,b,expect):

  14. print('\n測(cè)試函數(shù)2測(cè)試數(shù)據(jù)為\n{}-{}'.format(a,b))

  15. assert add(a,b) == expect

  16. if __name__ == "__main__":

  17. pytest.main(["-s","test_07.py"])

 
  1. ============================= test session starts =============================

  2. platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

  3. rootdir: D:\AutoCode

  4. plugins: html-3.1.1, metadata-1.11.0

  5. collecting ... collected 4 items

  6. test_07.py::TestParametrize::test_parametrize_1[2-2-4]

  7. 測(cè)試函數(shù)1測(cè)試數(shù)據(jù)為

  8. 2-2

  9. PASSED

  10. test_07.py::TestParametrize::test_parametrize_1[3-4-12]

  11. 測(cè)試函數(shù)1測(cè)試數(shù)據(jù)為

  12. 3-4

  13. PASSED

  14. test_07.py::TestParametrize::test_parametrize_2[2-2-4]

  15. 測(cè)試函數(shù)2測(cè)試數(shù)據(jù)為

  16. 2-2

  17. PASSED

  18. test_07.py::TestParametrize::test_parametrize_2[3-4-12]

  19. 測(cè)試函數(shù)2測(cè)試數(shù)據(jù)為

  20. 3-4

  21. PASSED

  22. ============================== 4 passed in 0.12s ==============================

  23. Process finished with exit code 0

由以上代碼可以看到,當(dāng)裝飾器裝飾測(cè)試類(lèi)時(shí),定義的數(shù)據(jù)集合會(huì)被傳遞給類(lèi)的所有方法。

裝飾測(cè)試函數(shù)

單個(gè)數(shù)據(jù)

  1. import pytest

  2. data = ["Rose","white"]

  3. @pytest.mark.parametrize("name",data)

  4. def test_parametrize(name):

  5. print('\n列表中的名字為\n{}'.format(name))

  6. if __name__ == "__main__":

  7. pytest.main(["-s","test_07.py"])

 
  1. ============================= test session starts =============================

  2. platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

  3. rootdir: D:\AutoCode

  4. plugins: html-3.1.1, metadata-1.11.0

  5. collected 2 items

  6. test_07.py

  7. 列表中的名字為

  8. Rose

  9. .

  10. 列表中的名字為

  11. white

  12. .

  13. ============================== 2 passed in 0.09s ==============================

  14. Process finished with exit code 0

當(dāng)測(cè)試用例只需要一個(gè)參數(shù)時(shí),我們存放數(shù)據(jù)的列表無(wú)序嵌套序列,@pytest.mark.parametrize("name", data)?裝飾器的第一個(gè)參數(shù)也只需要一個(gè)變量接收列表中的每個(gè)元素,第二個(gè)參數(shù)傳遞存儲(chǔ)數(shù)據(jù)的列表,那么測(cè)試用例需要使用同名的字符串接收測(cè)試數(shù)據(jù)(實(shí)例中的name)且列表有多少個(gè)元素就會(huì)生成并執(zhí)行多少個(gè)測(cè)試用例。

一組數(shù)據(jù)

  1. import pytest

  2. data = [

  3. [1, 2, 3],

  4. [4, 5, 9]

  5. ] # 列表嵌套列表

  6. # data_tuple = [

  7. # (1, 2, 3),

  8. # (4, 5, 9)

  9. # ] # 列表嵌套元組

  10. @pytest.mark.parametrize('a, b, expect', data)

  11. def test_parametrize_1(a, b, expect): # 一個(gè)參數(shù)接收一個(gè)數(shù)據(jù)

  12. print('\n測(cè)試數(shù)據(jù)為\n{},{},{}'.format(a, b, expect))

  13. actual = a + b

  14. assert actual == expect

  15. @pytest.mark.parametrize('value', data)

  16. def test_parametrize_2(value): # 一個(gè)參數(shù)接收一組數(shù)據(jù)

  17. print('\n測(cè)試數(shù)據(jù)為\n{}'.format(value))

  18. actual = value[0] + value[1]

  19. assert actual == value[2]

  20. if __name__ == "__main__":

  21. pytest.main(["-s","test_07.py"])

 
  1. ============================= test session starts =============================

  2. platform win32 -- Python 3.8.0, pytest-6.2.5, py-1.11.0, pluggy-1.0.0

  3. rootdir: D:\AutoCode

  4. plugins: html-3.1.1, metadata-1.11.0

  5. collected 4 items

  6. test_07.py

  7. 測(cè)試數(shù)據(jù)為

  8. 1,2,3

  9. .

  10. 測(cè)試數(shù)據(jù)為

  11. 4,5,9

  12. .

  13. 測(cè)試數(shù)據(jù)為

  14. [1, 2, 3]

  15. .

  16. 測(cè)試數(shù)據(jù)為

  17. [4, 5, 9]

  18. .

  19. ============================== 4 passed in 0.09s ==============================

  20. Process finished with exit code 0

當(dāng)測(cè)試用例需要多個(gè)數(shù)據(jù)時(shí),我們可以使用嵌套序列(嵌套元組&嵌套列表)的列表來(lái)存放測(cè)試數(shù)據(jù)。

裝飾器@pytest.mark.parametrize()可以使用單個(gè)變量接收數(shù)據(jù),也可以使用多個(gè)變量接收,同樣,測(cè)試用例函數(shù)也需要與其保持一致。

當(dāng)使用單個(gè)變量接收時(shí),測(cè)試數(shù)據(jù)傳遞到測(cè)試函數(shù)內(nèi)部時(shí)為列表中的每一個(gè)元素或者小列表,需要使用索引的方式取得每個(gè)數(shù)據(jù)。當(dāng)使用多個(gè)變量接收數(shù)據(jù)時(shí),那么每個(gè)變量分別接收小列表或元組中的每個(gè)元素列表嵌套多少個(gè)多組小列表或元組,測(cè)生成多少條測(cè)試用例。

組合數(shù)據(jù)

  1. import pytest

  2. data_1 = [1,2,3]

  3. data_2 = ['a','b']

  4. @pytest.mark.parametrize('a',data_1)

  5. @pytest.mark.parametrize('b',data_2)

  6. def test_parametrize_1(a,b):

  7. print(f'笛卡爾積測(cè)試結(jié)果為:{a},')

  8. if __name__ == '__main__':

  9. pytest.main(["-vs","test_06.py"])

?通過(guò)測(cè)試結(jié)果,我們不難分析,一個(gè)測(cè)試函數(shù)還可以同時(shí)被多個(gè)參數(shù)化裝飾器裝飾,那么多個(gè)裝飾器中的數(shù)據(jù)會(huì)進(jìn)行交叉組合的方式傳遞給測(cè)試函數(shù),進(jìn)而生成n * n個(gè)測(cè)試用例。

標(biāo)記用例

  1. import pytest

  2. @pytest.mark.parametrize("test_input,expected",[

  3. ("3+5",8),

  4. ("2+4",6),

  5. pytest.param("6 * 9",42,marks=pytest.mark.xfail),

  6. pytest.param("6 * 6",42,marks=pytest.mark.skip)

  7. ])

  8. def test_mark(test_input,expected):

  9. assert eval(test_input) == expected

  10. if __name__ == '__main__':

  11. pytest.main(["-vs","test_06.py"])

?輸出結(jié)果顯示收集到4個(gè)用例,兩個(gè)通過(guò),一個(gè)被跳過(guò),一個(gè)標(biāo)記失敗:

  • 當(dāng)我們不想執(zhí)行某組測(cè)試數(shù)據(jù)時(shí),我們可以標(biāo)記skip或skipif;

  • 當(dāng)我們預(yù)期某組數(shù)據(jù)會(huì)執(zhí)行失敗時(shí),我們可以標(biāo)記為xfail等。

嵌套字典

  1. import pytest

  2. data = (

  3. {

  4. 'user': "name1",

  5. 'pwd': 123

  6. },

  7. {

  8. 'user': "name2",

  9. 'pwd': 456

  10. }

  11. )

  12. @pytest.mark.parametrize('dic',data)

  13. def test_parametrize(dic):

  14. print('\n測(cè)試數(shù)據(jù)為\n{}'.format(dic))

  15. if __name__ == '__main__':

  16. pytest.main(["-vs","test_06.py"])

增加測(cè)試結(jié)果可讀性

參數(shù)化裝飾器有一個(gè)額外的參數(shù)ids,可以標(biāo)識(shí)每一個(gè)測(cè)試用例,自定義測(cè)試數(shù)據(jù)結(jié)果的顯示,為了增加可讀性,我們可以標(biāo)記每一個(gè)測(cè)試用例使用的測(cè)試數(shù)據(jù)是什么,適當(dāng)?shù)脑黾右恍┱f(shuō)明。

在使用前你需要知道,ids參數(shù)應(yīng)該是一個(gè)字符串列表,必須和數(shù)據(jù)對(duì)象列表的長(zhǎng)度保持一致。

  1. import pytest

  2. data_1 = [

  3. (1, 2, 3),

  4. (4, 5, 9)

  5. ]

  6. ids = ["a:{} + b:{} = expect:{}".format(a, b, expect) for a, b, expect in data_1]

  7. def add(a, b):

  8. return a + b

  9. @pytest.mark.parametrize('a, b, expect', data_1, ids=ids)

  10. class TestParametrize(object):

  11. def test_parametrize_1(self, a, b, expect):

  12. print('\n測(cè)試函數(shù)1測(cè)試數(shù)據(jù)為\n{}-{}'.format(a, b))

  13. assert add(a, b) == expect

  14. def test_parametrize_2(self, a, b, expect):

  15. print('\n測(cè)試函數(shù)2數(shù)據(jù)為\n{}-{}'.format(a, b))

  16. assert add(a, b) == expect

  17. if __name__ == '__main__':

  18. pytest.main(["-v","test_06.py"])

  • 不加ids參數(shù)的返回結(jié)果:

  • 加ids參數(shù)的返回結(jié)果:

我們可以看到帶ids參數(shù)的返回結(jié)果中的用例都被一個(gè)列表明確的標(biāo)記了,而且通過(guò)這種標(biāo)記可以更加直觀的看出來(lái),每個(gè)測(cè)試用例使用的數(shù)據(jù)名稱(chēng)及測(cè)試內(nèi)容。

行動(dòng)吧,在路上總比一直觀望的要好,未來(lái)的你肯定會(huì)感 謝現(xiàn)在拼搏的自己!如果想學(xué)習(xí)提升找不到資料,沒(méi)人答疑解惑時(shí),請(qǐng)及時(shí)加入扣群: 320231853,里面有各種軟件測(cè)試+開(kāi)發(fā)資料和技術(shù)可以一起交流學(xué)習(xí)哦。

最后感謝每一個(gè)認(rèn)真閱讀我文章的人,禮尚往來(lái)總是要有的,雖然不是什么很值錢(qián)的東西,如果你用得到的話可以直接拿走:

?

這些資料,對(duì)于【軟件測(cè)試】的朋友來(lái)說(shuō)應(yīng)該是最全面最完整的備戰(zhàn)倉(cāng)庫(kù),這個(gè)倉(cāng)庫(kù)也陪伴上萬(wàn)個(gè)測(cè)試工程師們走過(guò)最艱難的路程,希望也能幫助到你!

http://m.aloenet.com.cn/news/44593.html

相關(guān)文章:

  • 購(gòu)物平臺(tái)排行榜2020淄博seo培訓(xùn)
  • 貴州網(wǎng)站建設(shè)公司網(wǎng)絡(luò)營(yíng)銷(xiāo)專(zhuān)業(yè)的就業(yè)方向
  • java 做網(wǎng)站 模板在哪可以下新聞 最新消息
  • 高校網(wǎng)站建設(shè)研究意義楓林seo工具
  • 做網(wǎng)站 寫(xiě)文章怎樣加視頻深圳網(wǎng)絡(luò)推廣
  • 個(gè)門(mén)戶(hù)網(wǎng)站站長(zhǎng)之家ip查詢(xún)工具
  • 網(wǎng)站二次開(kāi)發(fā)是什么意思谷歌在線瀏覽器入口
  • 天津網(wǎng)站制作費(fèi)用競(jìng)價(jià)防惡意點(diǎn)擊
  • 怎么注冊(cè)英文網(wǎng)站域名a5站長(zhǎng)網(wǎng)網(wǎng)站交易
  • 武漢網(wǎng)上商城網(wǎng)站建設(shè)南通做網(wǎng)站推廣的公司
  • 鹽城網(wǎng)站開(kāi)發(fā)英文谷歌優(yōu)化
  • 鄭州做網(wǎng)站優(yōu)化電話seo基礎(chǔ)入門(mén)
  • 百度怎么開(kāi)戶(hù)做網(wǎng)站百度推廣平臺(tái)首頁(yè)
  • 湖南高端網(wǎng)站建設(shè)什么是搜索引擎優(yōu)化?
  • 陜西省經(jīng)營(yíng)性網(wǎng)站備案青島網(wǎng)站推廣關(guān)鍵詞
  • 北大青鳥(niǎo)學(xué)費(fèi)一覽表浙江seo推廣
  • 網(wǎng)站怎么做外部?jī)?yōu)化北京網(wǎng)站營(yíng)銷(xiāo)與推廣
  • 自己做的網(wǎng)站有什么用軟文推廣收費(fèi)
  • 網(wǎng)站開(kāi)發(fā)學(xué)習(xí)視頻seo推廣知識(shí)
  • 套別人的網(wǎng)站模板嗎百度推廣登錄入口官網(wǎng)網(wǎng)
  • 福州企業(yè)制作網(wǎng)站友點(diǎn)企業(yè)網(wǎng)站管理系統(tǒng)
  • 房地產(chǎn)型網(wǎng)站建設(shè)搜索引擎的使用方法和技巧
  • 東莞網(wǎng)站營(yíng)銷(xiāo)網(wǎng)絡(luò)營(yíng)銷(xiāo)推廣公司
  • 自己做的網(wǎng)站視頻播放不了營(yíng)銷(xiāo)云
  • 網(wǎng)站平臺(tái)怎么做的好天津seo網(wǎng)絡(luò)營(yíng)銷(xiāo)
  • app網(wǎng)站建設(shè)思路有免費(fèi)做網(wǎng)站的嗎
  • 麻將網(wǎng)站怎么做的衡水seo營(yíng)銷(xiāo)
  • 衡陽(yáng)網(wǎng)站建設(shè)icp備seo優(yōu)化網(wǎng)站推廣全域營(yíng)銷(xiāo)獲客公司
  • 網(wǎng)站的空間是服務(wù)器嗎營(yíng)銷(xiāo)網(wǎng)站建設(shè)哪家快
  • 賣(mài)手機(jī)網(wǎng)站開(kāi)發(fā)的必要性百度推廣怎么樣才有效果