pytest-09-參數(shù)化parametrize+命令行傳參

1、參數(shù)化parametrize

(1)測試用例參數(shù)化使用裝飾器 pytest.mark.parametrize

參數(shù)化
參數(shù)化執(zhí)行結(jié)果

(2)參數(shù)組合:獲取多個參數(shù)化參數(shù)的所有組合

參數(shù)組合實例
參數(shù)的所有組合

2、命令行傳參

根據(jù)命令行選項將不同的值傳給測試函數(shù)

(1)conftest.py添加命令選項,命令行傳入?yún)?shù)'--cmdfun'(需要建立cmdfun函數(shù)后才能調(diào)用)

import pytest

# --------添加命令行選項-------------

def pytest_addoption(parser):

? ? parser.addoption(

? ? ? ? '--cmdopt', action='store', default='type1', help='my option: type1 or type2'

? ? )

@pytest.fixture

def cmdopt(request):

? ? return request.config.getoption('--cmdopt')

(2)創(chuàng)建test_sample.py添加用例

import pytest

def test_answer(cmdopt):

? ? if cmdopt == 'type1':

? ? ? ? print('first')

? ? elif cmdopt == 'type2':

? ? ? ? print('second')

? ? assert 0

if __name__ == '__main__':

? ? pytest.main(['-s', 'test_sample.py'])

(3)啟動

pycharm中直接執(zhí)行即可

不帶參數(shù)啟動:pytest -s test_sample.py 默認啟動第一個

帶參數(shù)啟動:pytest -s test_sample.py --cmdopt=type2?

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容