Skip to content

Instantly share code, notes, and snippets.

@shpaker
Last active March 6, 2020 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shpaker/5ad803ae0e3f0310f08e1049272bc308 to your computer and use it in GitHub Desktop.
Save shpaker/5ad803ae0e3f0310f08e1049272bc308 to your computer and use it in GitHub Desktop.
Implementation of the pytest fixture for adding information to the Environment widget
from os import path
from typing import Any, Callable, Optional
from _pytest.fixtures import SubRequest
from pytest import fixture
ALLURE_ENVIRONMENT_PROPERTIES_FILE = 'environment.properties'
ALLUREDIR_OPTION = '--alluredir'
@fixture(scope='session', autouse=True)
def add_allure_environment_property(request: SubRequest) -> Optional[Callable]:
environment_properties = dict()
def maker(key: str, value: Any):
environment_properties.update({key: value})
yield maker
alluredir = request.config.getoption(ALLUREDIR_OPTION)
if not alluredir or not path.isdir(alluredir) or not environment_properties:
return
allure_env_path = path.join(alluredir, ALLURE_ENVIRONMENT_PROPERTIES_FILE)
with open(allure_env_path, 'w') as _f:
data = '\n'.join([f'{variable}={value}' for variable, value in environment_properties.items()])
_f.write(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment