-
Notifications
You must be signed in to change notification settings - Fork 6
integrates pytest-metadata and extract verbose to own argument #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
d882182
f9c87b7
2b2b74e
0876e25
8fb69f5
68b1f39
2ca98c4
04754d4
d0718e9
a1cff60
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
[pytest] | ||
markers = | ||
emoji: tests which are skipped if pytest-emoji is not installed. | ||
metadata: tests which are skipped if pytest-metadata is not installed. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,11 +20,14 @@ class Outcome(enum.Enum): | |
class MarkdownPlugin: | ||
"""Plugin for generating Markdown reports.""" | ||
|
||
def __init__(self, config, report_path, emojis_enabled: bool = False) -> None: | ||
def __init__(self, config, report_path, | ||
emojis_enabled: bool = False, | ||
metadata_enabled: bool = False) -> None: | ||
self.config = config | ||
self.report_path = report_path | ||
self.report = "" | ||
self.emojis_enabled = emojis_enabled | ||
self.metadata_enabled = metadata_enabled | ||
|
||
self.reports: Dict[Outcome, List] = collections.defaultdict(list) | ||
|
||
|
@@ -37,7 +40,7 @@ def _retrieve_emojis(self): | |
def emoji(short, verbose): | ||
"""Return the short or verbose emoji based on self.config.""" | ||
|
||
if self.config.option.verbose > 0: | ||
if self.config.option.md_verbose > 0: | ||
return verbose | ||
|
||
return short | ||
|
@@ -151,6 +154,25 @@ def create_summary(self) -> str: | |
|
||
return summary + "\n\n" + outcome_text | ||
|
||
def create_metadata(self) -> str: | ||
""" Create environment section for the Markdown report.""" | ||
outcome_text = "" | ||
|
||
def _generate_md(items, indentation=0): | ||
nonlocal outcome_text | ||
for key, value in items: | ||
if isinstance(value, dict): | ||
outcome_text += f'{" "*indentation}* {key}\n' | ||
_generate_md(value.items(), indentation+2) | ||
else: | ||
outcome_text += f'{" "*indentation}* {key}: {value}\n' | ||
|
||
_generate_md(self.config._metadata.items()) | ||
|
||
summary = "## Metadata" | ||
|
||
return summary + "\n\n" + outcome_text | ||
|
||
def create_results(self) -> str: | ||
"""Create results for the individual tests for the Markdown report.""" | ||
|
||
|
@@ -217,7 +239,11 @@ def pytest_sessionfinish(self, session) -> None: | |
self.report += f"{project_link}\n" | ||
self.report += f"{summary}\n" | ||
|
||
if self.config.option.verbose > 0: | ||
if self.metadata_enabled: | ||
metadata = self.create_metadata() | ||
self.report += f"{metadata}" | ||
|
||
if self.config.option.md_verbose: | ||
results = self.create_results() | ||
self.report += f"{results}" | ||
|
||
|
@@ -236,6 +262,18 @@ def pytest_addoption(parser): | |
default=None, | ||
help="create markdown report file at given path.", | ||
) | ||
group.addoption( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to add metadata to the Markdown report based on whether pytest-metadata is enabled rather than adding an additional CLI option. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might lead unexpected "backwards compatible break" behaviour. User might want to use pytest-metadata and pytest-md and expects that generated markdown is still the same even new version is deployed. This is a bit similar thing that verbose usage earlier (#18), right ? PR makes possible to export metadata to markdown or not regardless pytest-metadata plugin installation. Maybe it's okay even it's always "on". In my case I want to generate markdown document and post it to PR comment. Currently markdown document is a bit annoying long - that's why I implemented also collapse metadata/individual test results here. |
||
"--md-metadata", | ||
action="store_true", | ||
dest="md_metadata", | ||
help="Add environment section to report", | ||
) | ||
group.addoption( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd honestly prefer if we stick to the current approach and use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a bit inconsistent propose against to metadata propose that would make breaks ;) But currently I can survive with original approach without extra CLI option since I did collapsed verbose section in my fork ... :) What's final decision? :) reject original issue #18 or accept it... ? |
||
"--md-verbose", | ||
action="store_true", | ||
dest="md_verbose", | ||
help="individual test report", | ||
) | ||
|
||
|
||
def pytest_configure(config) -> None: | ||
|
@@ -254,10 +292,17 @@ def emojis_enabled() -> bool: | |
|
||
return config.option.emoji is True | ||
|
||
def metadata_enabled() -> bool: | ||
""" Check if pytest-metadata is installed and enabled """ | ||
if not config.pluginmanager.hasplugin('metadata'): | ||
return False | ||
return config.option.md_metadata | ||
|
||
config._md = MarkdownPlugin( | ||
config, | ||
report_path=pathlib.Path(mdpath).expanduser().resolve(), | ||
emojis_enabled=emojis_enabled(), | ||
metadata_enabled=metadata_enabled() | ||
) | ||
|
||
config.pluginmanager.register(config._md, "md_plugin") | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from typing import Pattern | ||
|
||
|
||
def test_generate_report(testdir, cli_options, report_path, report_content): | ||
"""Check the contents of a generated Markdown report.""" | ||
# run pytest with the following CLI options | ||
|
@@ -7,5 +10,10 @@ def test_generate_report(testdir, cli_options, report_path, report_content): | |
# as we have at least one failure | ||
assert result.ret == 1 | ||
|
||
report = report_path.read_text() | ||
|
||
# Check the generated Markdown report | ||
assert report_path.read_text() == report_content | ||
if isinstance(report_content, Pattern): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's create a new test for when pytest-metadata is enabled. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is already test with pytest-metadata is installed and enabled. Did you mean disabled but still installed ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added test with emoji and metadata enabled. Did you mean that ? |
||
assert report_content.match(report) | ||
else: | ||
assert report == report_content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please try to avoid recursion for the sake of comprehensibility. This is how pytest-html generates the metadata section https://github.com/pytest-dev/pytest-html/blob/master/pytest_html/plugin.py#L558