question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

allure.environment is missing in allure.py

See original GitHub issue

I’m submitting a …

  • bug report
  • feature request
  • support request => Please do not submit support request here, see note at the top of this template.

What is the current behavior?

Based on documentation here to apply some environment configurations we should use allure.environment(report=‘Allure report’, browser=u’Я.Браузер’) syntax. But in allure.py environment is missing. Before we have to use some environment.properties file in report folder to apply variables to the report. New functionality is missing. Content of allure.py:

from allure_commons._allure import label
from allure_commons._allure import severity
from allure_commons._allure import tag
from allure_commons._allure import epic, feature, story
from allure_commons._allure import link
from allure_commons._allure import issue, testcase
from allure_commons._allure import Dynamic as dynamic
from allure_commons._allure import step
from allure_commons._allure import attach
from allure_commons.types import Severity as severity_level
from allure_commons.types import AttachmentType as attachment_type


__all__ = [
    'label',
    'severity',
    'tag',
    'epic'
    'feature',
    'story',

    'link',
    'issue',
    'testcase',

    'step'

    'dynamic'

    'severity_level',

    'attach',
    'attachment_type'
]

If the current behavior is a bug, please provide the steps to reproduce and if possible a minimal demo of the problem

What is the expected behavior?

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Allure version: 2.2.1
  • Test framework: pytest@3.5
  • Allure adaptor: allure-pytest@2.1.0b1

Other information

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:12
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

8reactions
shpakercommented, Mar 6, 2020

My implementation of the 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)

Example of usage:

@fixture(autouse=True)
def cenpprop(add_allure_environment_property: Callable) -> None:
    add_allure_environment_property('foo', 3)
    add_allure_environment_property('bar', 'baz')
7reactions
rrudakovcommented, Jul 16, 2018

As workaround it’s possible to create environment.xml and put it to alluredir after all tests executions finished. For pytest I have written next code:

# conftest.py
@pytest.fixture(scope="session")
def allure_env(tmpdir_factory):
    """Provide access to environment file."""
    env = tmpdir_factory.mktemp("allure").join("environment.xml")
    environment = lxml.etree.Element("environment")
    with open(env, "a") as env_xml:
        env_xml.write(lxml.etree.tounicode(environment, pretty_print=True))

    return str(env)


@pytest.fixture(scope="session", autouse=True)
def write_allure_env(request, allure_env):
    """Copy environment to alluredir."""
    yield

    alluredir = request.config.getoption("--alluredir")
    if os.path.isdir(alluredir):
        copyfile(allure_env, os.path.join(alluredir, "environment.xml"))

# common.py
import lxml

def set_env(allure_env: str, name: str, val: str) -> None:
    """Add entry to environment.xml."""
    parser = lxml.etree.XMLParser(remove_blank_text=True)
    tree = lxml.etree.parse(allure_env, parser)
    env = tree.getroot()

    exist = tree.xpath(
        f"/environment/parameter[key[text()='{name}'] "
        f"and value[text()='{val}']]"
    )

    if not exist:
        parameter = lxml.etree.SubElement(env, "parameter")
        name_node = lxml.etree.SubElement(parameter, "key")
        name_node.text = name
        value_node = lxml.etree.SubElement(parameter, "value")
        value_node.text = val

        with open(allure_env, "w") as env_xml:
            env_xml.write(lxml.etree.tounicode(env, pretty_print=True))

# test_something.py
import common

def test_example(request, allure_env):
    """Example test function."""
    env = request.config.getoption("--env")
    if env == "test":
        common.set_env(allure_env, "Environment", "Test")
    else:
        common.set_env(allure_env, "Environment", "Production")

Hope this will be helplul)

Read more comments on GitHub >

github_iconTop Results From Across the Web

allure.environment is missing in allure.py · Issue #96 - GitHub
properties file in report folder to apply variables to the report. New functionality is missing. Content of allure.py: from allure_commons.
Read more >
Recommend workaround for the missing environmental ...
For people using Jenkins Allure plugin: We run our jobs on Windows machines (Windows Batch Command), passing some build parameters to a python...
Read more >
Allure Framework
Integrate your favorite testing framework with Allure. Supported frameworks are grouped by language: Java, Python, JavaScript, Ruby, Groovy, ...
Read more >
allure-framework/allure-core - Gitter
Hi guys! In our environment tests are executed from Jenkins in parallel on several windows versions. Is it possible to aggregate results from...
Read more >
pytest-allure-adaptor - Python Package Health Analysis - Snyk
Plugin for py.test to generate allure xml reports For more information about how to use ... Looks like pytest-allure-adaptor is missing a security...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found