Pytest插件之pytest-base-url切换测试环境
安装
pip install pytest-base-url
应用场景
-
利用参数--base-url或者配置(pytest.ini中base_url)来切换url
用法:命令行参数
-
测试代码:类似于fixture
#test_baseurl.py
import requests
def test_baseurl(base_url): #注意跟命令行的差别,下划线还是-
assert requests.get(base_url).status_code == 200 -
命令行
pytest --base-url https://www.songqinnet.com/ test_baseurl.py
-
一种典型的错误
from selenium import webdriver
import pytest
def test_a01(base_url):
driver = webdriver.Chrome()
driver.get(base_url)
from time import sleep
sleep(5)
#错误的做法,参数合并到一起
if __name__ == '__main__':
pytest.main(['-sv','--base-url https://cn.bing.com',__file__])
#报这个错
#INTERNALERROR> OSError: [WinError 123] 文件名、目录名或卷标语法不正确。: 'D:\\pythonProject\\AutoTest\\PublicLessson\\pytest常用插件实例演示\\test_baseurl\\--base-url https:\\cn.bing.com'
#正确的做法,参数分开写,当然在命令行执行是ok的,不会有二义性
if __name__ == '__main__':
pytest.main(['-sv','--base-url','https://www.baidu.com',__file__])
用法:pytest.ini
-
pytest.ini示例
[pytest]
base_url = https://www.songqinnet.com/ -
不要加引号
源码解读
pytest-base-url插件极其简单,是用来学习插件编写的非常好的DEMO
-
D:\Python39\Lib\site-packages\pytest_base_url\plugin.py
-
从源码可以看出
-
base_url就是个fixture(前文提到类似fixture),看了源码你能肯定了
-
_verify_url是自动生效的
-
pytest_addoption中加了一些参数,是的多数插件都会去加一些命令行参数
-
--base-url
-
--verify-base-url
-
-
# This Source Code Form is subject to the terms of the Mozilla Public标签:__,插件,url,--,pytest,base,config From: https://www.cnblogs.com/wuxianfeng023/p/16783761.html
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
import os
import pytest
@pytest.fixture(scope="session")
def base_url(request):
"""Return a base URL"""
config = request.config
base_url = config.getoption("base_url")
if base_url is not None:
return base_url
@pytest.fixture(scope="session", autouse=True)
def _verify_url(request, base_url):
"""Verifies the base URL"""
verify = request.config.option.verify_base_url
if base_url and verify:
# Lazy load requests to reduce cost for tests that don't use the plugin
import requests
from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
session = requests.Session()
retries = Retry(backoff_factor=0.1, status_forcelist=[500, 502, 503, 504])
session.mount(base_url, HTTPAdapter(max_retries=retries))
session.get(base_url)
def pytest_configure(config):
if hasattr(config, "workerinput"):
return # don't run configure on xdist worker nodes
base_url = config.getoption("base_url") or config.getini("base_url")
if base_url is not None:
config.option.base_url = base_url
if hasattr(config, "_metadata"):
config._metadata["Base URL"] = base_url
def pytest_report_header(config, startdir):
base_url = config.getoption("base_url")
if base_url:
return "baseurl: {0}".format(base_url)
def pytest_addoption(parser):
parser.addini("base_url", help="base url for the application under test.")
parser.addoption(
"--base-url",
metavar="url",
default=os.getenv("PYTEST_BASE_URL", None),
help="base url for the application under test.",
)
parser.addoption(
"--verify-base-url",
action="store_true",
default=not os.getenv("VERIFY_BASE_URL", "false").lower() == "false",
help="verify the base url.",
)