pytest文档48-切换 base_url 测试环境(pytest-base-url)
前言
当我们自动化代码写完成之后,期望能在不同的环境测试,这时候应该把 base_url 单独拿出来,能通过配置文件和支持命令行参数执行。
pytest-base-url 是 pytest 里面提供的一个管理 base-url 的一个非常实用的插件,参考文档https://pypi.org/project/pytest-base-url/
环境准备
先安装 pytest-base-url 插件
pip install pytest-base-url
使用案例
直接在用例里面使用 base_url参数 当成一个fixture使用
# test_demo.py
import requests
# 作者-上海悠悠 QQ交流群:717225969
# blog地址 https://www.cnblogs.com/yoyoketang/
def test_example(base_url):
assert 200 == requests.get(base_url).status_code
命令行执行的时候加上 --base-url
参数
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%]
============= 1 passed in 0.73 seconds ============
pytest.ini 配置文件
也可以在 pytest.ini 配置文件中添加 base_url 地址
# pytest.ini文件内容
[pytest]
base_url = http://www.example.com
这样在命令行执行时候就可以不用带上 --base-url
参数
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%]
============ 1 passed in 1.72 seconds ==========
网易云完整视频课程《pytest+yaml 框架使用与开发》https://study.163.com/course/courseMain.htm?courseId=1213419817&share=2&shareId=480000002230338
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?