Add allure.environment API (fixes #96) - #923
Open
AdamerGitHub wants to merge 2 commits into
Open
Conversation
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Go throw #96
Context
Currently the only way to provide environment information for a report is to create the
environment.propertiesfile in the results directory manually, after the test run. This is what the documentation suggests, and it is what users have been doing in custom fixtures since #96 was opened in 2016.Note that the API existed in allure-pytest 1.x and was documented in its README with three usage scenarios: a call from
pytest_configure, from a session-scoped fixture, and from a test body. It was not carried over to 2.x.This PR adds
allure.environment()back, implemented for all the integrations with runtime API support, following the structure of #904.A mapping may be passed as well, which allows keys that are not valid python identifiers:
Values from all the calls made during a test run are merged, the latest call wins for a key that is set more than once. The data is written into
environment.propertiesin the results directory, in the format Allure Report already reads.Implementation
allure.environment()inallure-python-commons, exposed through theadd_environmentuser hook and thereport_environmentreporting hook.AllureReporterandAllureLifecyclesupport the new hook.AllureFileLoggeraccumulates the values and rewritesenvironment.propertieson every call, so the file stays valid even if a run is interrupted.AllureMemoryLoggerkeeps the same mapping for tests.Environmentkeyword inAllureLibrary..propertiesfiles.AllureReportin allure-python-commons-test reads the file back. The name of the file is duplicated there instead of being imported fromallure_commons, since the package intentionally depends on pyhamcrest only.Tests
Acceptance tests for every affected integration, plus unit tests for the file format (merging, escaping, non-ascii values). The pytest examples are executable and are covered through
run_docpath_examples, as with the other features.Verified manually as well: the resulting file is displayed in the Environment section of a report generated by Allure 2.45.0, including non-ascii values.
Questions
Three points where I would appreciate your opinion — I did not want to decide them on my own:
Encoding. The file is written in UTF-8, and only the structural characters are escaped. This works with Allure 2.45.0 (checked) and should be the right choice for Allure 3, but
java.util.Propertiesassumes ISO-8859-1, so an older Allure 2 may mis-render non-ascii values. The alternative is to escape everything above ASCII as\uXXXX, which in turn may not be understood by the javascript parser.Parallel runs. The file name is fixed, and under pytest-xdist every worker owns its own
AllureFileLoggerwriting into the same directory. In a three-test run with-n 2the contribution of one of the workers was lost entirely. I do not see a good fix that does not involve a new result file format, so this is currently a documented limitation.The earliest call site. A call from
pytest_configureis silentlyignored, because the listener is registered inside allure's own
pytest_configure, and conftest hooks run before it.pytest_sessionstartis the earliest hook that works. This is not specific to this PR: I checked,
and
allure.global_attach()andallure.global_error()behave the same way.It is, however, a difference from allure-pytest 1.x, where
pytest_configurewas the documented entry point for
allure.environment(). Fixing it wouldmean buffering the calls made before the plugins are registered, which
affects the whole family of global APIs, so it looks like a separate change
to me. Happy to open an issue for it if you think it is worth fixing.
Checklist