前言
①當我們的自動化代碼完成之后,通常期望可以在不同的環境進行測試,此時可以將項目系統的URL單獨拿出來,並且可以通過pytest.ini配置文件和支持pytest命令行方式執行。
② pytest-base-url 是一個簡單的pytest插件,它通過命令行或配置文件提供可選的基本 URL。
③在測試用例里直接傳 base_url 參數,當做fixture函數使用(此時就會調用pytets.ini里面的 base_url 地址或者pytest命令行方式執行時的 base_url 參數)
④ base_url 是pytest中的內置fixture函數。
⑤參考文檔:https://pypi.org/project/pytest-base-url/
環境
①Python 2.7、3.6、PyPy 或 PyPy3
②py.test 2.7 或更新版本
安裝
pip install pytest-base-url
使用
直接在用例里面使用 base_url 參數,當成一個fixture函數使用
# -*- encoding:utf-8 -*- def test_example(base_url): assert base_url == 'http://www.example.com'
①pytest命令行方式不添加 --base-url 參數運行
②pytest命令行方式執行並加上 --base-url 參數
pytest -vs --base-url=http://www.example.com
運行結果:
③pytest.ini配置文件中添加 base_url 地址
# pytest.ini文件內容 [pytest] base_url = http://www.example.com
運行結果:(這樣在pytest命令行方式執行用例的時候就可以不用帶上 --base-url 參數)