前言
- def pytest_addoption(parser):
- parser.addoption(
- "--anjing",
- action="store",
- default="dev",
- help="通过'anjing'添加自定义命令行参数名称"
- )
name
action
default
help
- # conftest.py文件
- # coding:utf-8
- import pytest
-
- def pytest_addoption(parser):
- parser.addoption(
- "--anjing", action="store", default="anjing", help="将'anjing'添加到pytest的配置参数中"
- )
- parser.addoption(
- "--env", action="store", default="dev", help="env:表示命令行参数内容,不填写默认输出default的值内容"
- )
- @pytest.fixture()
- def anjing(request):
- return request.config.getoption("--anjing")
-
- @pytest.fixture()
- def env(request):
- return request.config.getoption("--env")
- # coding:utf-8
- def test_01(anjing):
- if anjing == 'test':
- print('命令行传参成功!')
- else:
- print('命令行取默认值!')
-
- def test_02(env):
- if env == 'test':
- print('传参成功!')
- else:
- print('传参失败!')

- # conftest.py文件
- # coding:utf-8
- import pytest
- def pytest_addoption(parser):
- parser.addoption(
- "--url",
- action="store",
- default="http://apis.juhe.cn/simpleWeather/query",
- help="将'anjing'添加到pytest的配置参数中"
- )
-
- @pytest.fixture()
- def anjing(request):
- return request.config.getoption("--url")
- # test_01.py文件
- # coding:utf-8
- import requests
- def test_01(anjing):
- data = {
- 'city': "上海",
- 'key': '331eab8f3481f37868378fcdc76cb7cd'
- }
- r = requests.post(anjing, data=data)
- result = r.json()['reason']
- assert result == '查询成功!'
