pytest-切換環(huán)境(pytest-base-url)

pytest-切換環(huán)境(pytest-base-url)

當(dāng)我們自動(dòng)化代碼寫完成之后,期望能在不同的環(huán)境測(cè)試,這時(shí)候應(yīng)該把 base_url 單獨(dú)拿出來,能通過配置文件和支持命令行參數(shù)執(zhí)行。

pytest-base-url 是 pytest 里面提供的一個(gè)管理 base-url 的一個(gè)非常實(shí)用的插件,參考文檔https://pypi.org/project/pytest-base-url/

環(huán)境準(zhǔn)備

先安裝 pytest-base-url 插件

pip install pytest-base-url

使用案例

直接在用例里面使用 base_url參數(shù) 當(dāng)成一個(gè)fixture使用

# test_demo.py

import requests

def test_example(base_url):

? ? assert 200 == requests.get(base_url).status_code

命令行執(zhí)行的時(shí)候加上 --base-url 參數(shù)

pytest —base-url http://www.example.com

D:\soft\web_base>pytest --base-url http://www.example.com

=========== test session starts ==============

platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1

baseurl: http://www.example.com

rootdir: D:\soft\web_base

plugins: allure-pytest-2.8.6, base-url-1.4.2

collected 1 item

test_demo.py .? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [100%]

pytest.ini 配置文件

也可以在 pytest.ini 配置文件中添加 base_url 地址

# pytest.ini文件內(nèi)容

[pytest]

base_url = http://www.example.com

這樣在命令行執(zhí)行時(shí)候就可以不用帶上 --base-url 參數(shù)

D:\soft\web_base>pytest

============== test session starts ================

platform win32 -- Python 3.6.0, pytest-4.5.0, py-1.5.4, pluggy-0.13.1

baseurl: http://www.example.com

rootdir: D:\soft\web_base, inifile: pytest.ini

plugins: allure-pytest-2.8.6,? base-url-1.4.2

collected 1 item

test_demo.py .? ? ? ? ? [100%]

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

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