pytest自定義命令行參數(shù)的實(shí)現(xiàn)
實(shí)際使用場景:pytest運(yùn)行用例的時(shí)候,啟動(dòng)mitmdump進(jìn)程試試抓包,pytest命令行啟動(dòng)的時(shí)候,傳入mitmdump需要的參數(shù)(1)抓包生成的文件地址 (2)mitm的proxy設(shè)置
# 在pytest的固定文件中conftest.py中 def pytest_addoption(parser): """ 自定義pytest的命令行參數(shù),@pytest.fixture配合下面的方法一起用 :param parser: :return: """ parser.addoption("--mitm_path", action="store", default="", type=str, help="--mitm_path:mitmproxy生成的cvs文件名稱") parser.addoption("--mitm_proxy", action="store", default="127.0.0.1:8080", type=str, help="--mitm_proxy:mitmproxy設(shè)置代理") @pytest.fixture(scope="session", autouse=True) def set_env_mitm_path(request): """ 將--mitm_path從命令行中獲取放入環(huán)境變量中,給mitmdump工具用 :param request: :return: """ mitm_value = request.config.getoption("--mitm_path") os.environ['mitm_path'] = mitm_value print('\n --mitm_path參數(shù)值:', mitm_value) return mitm_value @pytest.fixture(scope="session", autouse=True) def set_env_mitm_proxy(request): """ 將--mitm_proxy從命令行中獲取放入環(huán)境變量中,給mitmdump工具用 :param request: :return: """ mitm_proxy = request.config.getoption("--mitm_proxy") os.environ['mitm_proxy'] = mitm_proxy print('\n --mitm_proxy參數(shù)值:', mitm_proxy) return mitm_proxy @pytest.fixture(scope="session") def setup_mitmdump(): """ pytest啟動(dòng),cmd啟動(dòng)一個(gè)mitmdump的進(jìn)程 :return: """ if not os.environ.get("mitm_path"): # 命令行沒有傳入mitm_path的值,給默認(rèn)值一個(gè) caller = os.environ.get('PYTEST_CURRENT_TEST').split(':')[-1].split(' ')[0] mitm_path = "./testdata/" + caller + ".csv" os.environ["mitm_path"] = mitm_path cmd = r"mitmdump -p {}".format(os.environ.get("mitm_proxy") if os.environ.get("mitm_proxy") else '8080') process = subprocess.Popen(cmd, creationflags=subprocess.CREATE_NEW_CONSOLE) time.sleep(1) yield time.sleep(6) print("stop mitm") process.kill()
測(cè)試文件
import csv import os import time import pytest import requests class TestDemo: @pytest.mark.usefixtures("setup_mitmdump") @pytest.mark.parametrize( "name,assert_word", [ pytest.param("1", "smart", id="第一個(gè)"), pytest.param("2", "smart", id="第二個(gè)") ] ) def test_001(self, name, assert_word): print("我是用例test_%s" % name) url = "http://httpbin.org/get" params = {} headers = {"content-type": "application/json; charset=UTF-8"} proxies = {'http': 'http://127.0.0.1:%s' % os.environ.get("mitm_proxy")} # ip地址 option = requests.get(url=url, headers=headers, params=params, proxies=proxies) time.sleep(10)
運(yùn)行效果
pytest -s test1.py --mitm_path=D:/hf.csv
pytest -s test1.py --mitm_path=D:/hf.csv --mitm_proxy 8989
到此這篇關(guān)于pytest自定義命令行參數(shù)的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)pytest 命令行參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python根據(jù)詞頻信息(xlsx、csv文件)繪制詞云圖全過程(wordcloud)
這篇文章主要給大家介紹了關(guān)于Python根據(jù)詞頻信息(xlsx、csv文件)繪制詞云圖的相關(guān)資料,wordcloud是基于Python開發(fā)的詞云生成庫,功能強(qiáng)大使用簡單,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-06-06淺談Python3中datetime不同時(shí)區(qū)轉(zhuǎn)換介紹與踩坑
最近的項(xiàng)目需要根據(jù)用戶所屬時(shí)區(qū)制定一些特定策略,學(xué)習(xí)、應(yīng)用了若干python3的時(shí)區(qū)轉(zhuǎn)換相關(guān)知識(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08跟老齊學(xué)Python之玩轉(zhuǎn)字符串(2)
上一篇文章章中已經(jīng)講到連接兩個(gè)字符串的一種方法,本文繼續(xù)講訴連接字符串的方法2,字符串復(fù)制,字符串長度,字符大小寫的轉(zhuǎn)換,希望對(duì)大家有所幫助。2014-09-09