pytest文件10 命令列傳參

2022-05-18 02:53:06 字數 2685 閱讀 6144

命令列引數是根據命令列選項將不同的值傳遞給測試函式,比如平常在cmd執行"pytest --html=report.html",這裡面的」--html=report.html「就是從命令列傳入的引數

對應的引數名稱是html,引數值是report.html

1.首先需要在conftest.py新增命令列選項,命令列傳入引數」--cmdopt「, 用例如果需要用到從命令列傳入的引數,就呼叫cmdopt函式:

#

content of conftest.py

import

pytest

defpytest_addoption(parser):

parser.addoption(

"--cmdopt

", action="

store

", default="

type1

", help="

my option: type1 or type2")

@pytest.fixture

defcmdopt(request):

return request.config.getoption("

--cmdopt

")

2.測試用例編寫案例

#

content of test_sample.py

import

pytest

deftest_answer(cmdopt):

if cmdopt == "

type1":

print("

first")

elif cmdopt == "

type2":

print("

second")

assert 0 #

to see what was printed

if__name__ == "

__main__":

pytest.main(["-s

", "

test_case1.py

"])

cmd開啟,輸入指令啟動,也可以在pycharm裡面右鍵執行上面**

$ pytest -s test_sample.py

執行結果:

>pytest -s

*************************==== test session starts *************************====test_sample.py first

f******************************==== failures ***********************************

_________________________________ test_answer _________________________________

cmdopt = '

type1

'def

test_answer(cmdopt):

if cmdopt == "

type1":

print("

first")

elif cmdopt == "

type2":

print("

second")

> assert 0 #

to see what was printed

e assert

0test_case1.py:8: assertionerror

*************************= 1 failed in 0.05 seconds *************************==

1.如果不帶引數執行,那麼傳預設的default="type1",接下來在命令列帶上引數去執行

$ pytest -s test_sample.py --cmdopt=type2

test_sample.py second

f******************************==== failures ***********************************

_________________________________ test_answer _________________________________

cmdopt = '

type2

'def

test_answer(cmdopt):

if cmdopt == "

type1":

print("

first")

elif cmdopt == "

type2":

print("

second")

> assert 0 #

to see what was printed

e assert

0test_case1.py:8: assertionerror

*************************= 1 failed in 0.05 seconds *************************==

2.命令列傳引數有兩種寫法,還有一種分成2個引數也可以的,引數和名稱用空格隔開

$ pytest -s test_case1.py --cmdopt type2

python 命令列傳參

1.簡單的寫法 通過匯入模組sys import sys if name main argc len sys.ar arg0 sys.ar 0 2.稍微複雜一點的引數可以使用getopt模組 import sys import getopt if name main opt,args getopt....

方法 命令列傳參

設計方法的原則 方法的本意是功能塊,就是實現某個功能的語句塊的集合。我們設計方法的時候,最好保持方法的原子性,就是乙個方法只完成1個功能,這樣有利於我們後期的擴充套件 例 package 包 public class ww 加法 public static int add int a,int b 方...

命令列 Pytest之命令列執行

基於一套 實現流水線的驗證方案,針對每個流水線傳不同的使用者進行驗證,具體的使用者可以選擇每個集群裡面活躍度比較高使用者來進行測試。正如前面說的,在saas化的模式下,底層服務它是共享的機制,但是每個集群它是不同的,這種不同在於它提供的db層面和計算能力,因此需要被驗證。使用pyhton中的argp...