我的项目实现是从CLI捕获详细信息以确定环境,在该环境中生成令牌并返回令牌和应用程序URL。
这是conftest.py文件中的代码
def pytest_addoption(parser):
parser.addoption('--env',
dest='testenv',
choices=["qa","aws","prod"],
default='qa',
help='Specify environment: "qa", "aws", "prod".')
@pytest.fixture(scope='session')
#def conftest_setup(request):
# env = request.config.getoption("--env")
def conftest_setup(request):
env = request.config.getoption("--env")
print(env)
if (env =='aws'):
url='AWSURL'
elif ( env =='prod'):
url='prodURL'
else:
url='QAURL'
token = requests.post(auth=HTTPBasicAuth(clientID, secret),headers=tokenheaders, data=payload)
auth = token.json()['access_token']
return auth,url
test_service.py有
auth1=''
url1=''
def initialCall(conftest_setup):
auth1=conftest_setup[1] # Pretty sure this is wrong, but couldnt get a way to retrieve this
url1=conftest_setup[2]
# Now I want to use the auth1 and url1 obtained from above method to the below method
def response():
print("auth is " ,auth1)
print("URL is " ,url1)
headers = {'content-type': 'application/json',
'Authorization' : auth1}
response=
(requests.post(url1,data=json.dumps(data1),headers=headers)).json()
当前,我收到此异常
request = <FixtureRequest for <Function
> ???
test\test_service_1.py:41:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\software\python\lib\site-packages\pytest_bdd\scenario.py:195: in _execute_scenario
_execute_step_function(request, scenario, step, step_func)
..\..\software\python\lib\site-packages\pytest_bdd\scenario.py:136: in _execute_step_function
step_func(**kwargs)
..\..\software\python\lib\site-packages\pytest_bdd\steps.py:164: in step_func
result = request.getfixturevalue(func.__name__)
..\..\software\python\lib\site-packages\_pytest\fixtures.py:478: in getfixturevalue
return self._get_active_fixturedef(argname).cached_result[0]
..\..\software\python\lib\site-packages\_pytest\fixtures.py:501: in _get_active_fixturedef
self._compute_fixture_value(fixturedef)
..\..\software\python\lib\site-packages\_pytest\fixtures.py:586: in _compute_fixture_value
fixturedef.execute(request=subrequest)
..\..\software\python\lib\site-packages\_pytest\fixtures.py:895: in execute
return hook.pytest_fixture_setup(fixturedef=self, request=request)
..\..\software\python\lib\site-packages\pluggy\hooks.py:289: in __call__
return self._hookexec(self, self.get_hookimpls(), kwargs)
..\..\software\python\lib\site-packages\pluggy\manager.py:68: in _hookexec
return self._inner_hookexec(hook, methods, kwargs)
..\..\software\python\lib\site-packages\pluggy\manager.py:62: in <lambda>
firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
..\..\software\python\lib\site-packages\_pytest\fixtures.py:937: in pytest_fixture_setup
result = call_fixture_func(fixturefunc, request, kwargs)
..\..\software\python\lib\site-packages\_pytest\fixtures.py:794: in call_fixture_func
res = fixturefunc(**kwargs)
***test\test_service_1.py:40: in testws_response
response=(requests.post(url1,data=json.dumps(data1),headers=headers)).json()***
..\..\software\python\lib\site-packages\requests\api.py:116: in post
return request('post', url, data=data, json=json, **kwargs)
..\..\software\python\lib\site-packages\requests\api.py:60: in request
return session.request(method=method, url=url, **kwargs)
..\..\software\python\lib\site-packages\requests\sessions.py:519: in request
prep = self.prepare_request(req)
..\..\software\python\lib\site-packages\requests\sessions.py:462: in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
..\..\software\python\lib\site-packages\requests\models.py:313: in prepare
self.prepare_url(url, params)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <PreparedRequest [POST]>, url = '', params = OrderedDict()
def prepare_url(self, url, params):
"""Prepares the given HTTP URL."""
#: Accept objects that have string representations.
#: We're unable to blindly call unicode/str functions
#: as this will include the bytestring indicator (b'')
#: on python 3.x.
#: https://github.com/requests/requests/pull/2238
if isinstance(url, bytes):
url = url.decode('utf8')
else:
url = unicode(url) if is_py2 else str(url)
# Remove leading whitespaces from url
url = url.lstrip()
# Don't do any URL preparation for non-HTTP schemes like `mailto`,
# `data` etc to work around exceptions from `url_parse`, which
# handles RFC 3986 only.
if ':' in url and not url.lower().startswith('http'):
self.url = url
return
# Support for unicode domain names and paths.
try:
scheme, auth, host, port, path, query, fragment = parse_url(url)
except LocationParseError as e:
raise InvalidURL(*e.args)
if not scheme:
error = ("Invalid URL {0!r}: No schema supplied. Perhaps you meant http://{0}?")
error = error.format(to_native_string(url, 'utf8'))
> raise MissingSchema(error)
E requests.exceptions.MissingSchema: Invalid URL '': No schema supplied. Perhaps you meant http://?
..\..\software\python\lib\site-packages\requests\models.py:387: MissingSchema
在conftest.py文件中未观察到print(env)的控制台输出 auth为None 网址为无 在test_service中观察到打印语句
当我通过pytest -s --env ='qa'时,CLI执行会引发错误,但对于pytest -s --env = qa是可接受的。这样就捕获了CLI参数。
请帮助我在整个测试文件中从conftest.py捕获身份验证和网址。
还有,我可以使用conftest.py返回的auth,url来在多个测试文件中使用吗?
答案 0 :(得分:0)
非常感谢@hoefling提供了宝贵的见解。
我在以下位置添加/更新了灯具,并将auth1和url1声明为initialcall方法内部的全局变量。现在可以正常工作了。
test_service.py有
@pytest.fixture(autouse=True, scope='session')
def initialCall(conftest_setup):
global auth1,IBAN_API1
auth1 = conftest_setup[0]
IBAN_API1=conftest_setup[1]
conftest.py有
@pytest.fixture(autouse=True,scope='session')
def conftest_setup(request):