pytest08 在pytest中配置環境變數

2022-08-21 21:48:10 字數 1070 閱讀 2348

首先import os, 寫入系統當前目錄的環境變數

然後用os.envtiron方法來獲取。

可以寫到conftest.py中,放到根目錄下,當成全域性變數

#命令列引數設定

parser.addoption(

"--cmdhost",

action="store",

default="",

help="my option: type1 or type2"

)設定完成後可以在終端 pytest -h 來檢視是否寫入

@pytest.fixture(scope='session',autouse=true)

def host(request):

'''獲取命令列引數,給到環境變數'''

os.environ['host'] = request.config.getoption('--cmdhost')

---case---

#autouse=true:用例執行時自動載入,不需要傳入
@pytest.fixture(scope='session',autouse=true)

def test_case():

url = os.environ['host'] + path

print url

--------

這種只能放到命令列去pytest執行, 在pycharm中if__main()方法執行是失敗的,只有pytest用例才會生效

所以自己去選擇用這種還是用之前的ini配置......

-----------------------多環境配置---------------------

Pytest08 指定fixture作用範圍

fixture的指定範圍包括函式 scope function scope module scope class scope session 分別對應 函式級別,模組級別,類級別,會話級別 用個例子感受下他們的執行順序已經銷毀順序.import pytest pytest.fixture scope...

pytest04 pytest常用外掛程式

02 生成測試報告 外掛程式 安裝 pip install pytest html 生成測試報告 pytest html 測試報告路徑 html03 在pytest.ini中加入報告生成命令 addopts s html test hello.html 直接執行 pytest即可 如果這裡加上了 s...

pytest (五) pytest中的斷言

pytest 的斷言報告,也很豐富,和詳情,比如 import pytest def test set comparison set1 set 1308 set2 set 8035 assert set1 set2 執行一下 有時候,我們需要對一些異常丟擲作斷言,可以用pytest.raises 比...