Skip to content

Commit 9f3dc31

Browse files
committed
Add an option to exclude specific test outcomes from the report: #8
Signed-off-by: Tsuyoshi Hombashi <[email protected]>
1 parent 4a6f35b commit 9f3dc31

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

pytest_md_report/_const.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from enum import Enum, unique
22
from textwrap import dedent
3+
from typing import List
34

45
from pytablewriter.writer.text import MarkdownFlavor
56
from tcolorpy import AnsiFGColor
@@ -44,6 +45,7 @@ class Default:
4445
MARGIN = 1
4546
MARKDOWN_FLAVOR = MarkdownFlavor.COMMON_MARK
4647
ZEROS = ZerosRender.NUMBER
48+
EXCLUDE_RESULTS: List[str] = []
4749

4850
class FGColor:
4951
SUCCESS = "light_green"
@@ -158,6 +160,19 @@ class Option(Enum):
158160
default=Default.MARKDOWN_FLAVOR.value,
159161
),
160162
)
163+
MD_EXCLUDE_OUTCOMES = (
164+
f"{OPTION_PREFIX}-exclude-outcomes",
165+
dedent(
166+
"""\
167+
List of test outcomes to exclude from the report.
168+
When specifying as an environment variable, pass a comma-separated string
169+
(e.g. 'passed,skipped').
170+
Defaults to '{default}'.
171+
"""
172+
).format(
173+
default=Default.EXCLUDE_RESULTS,
174+
),
175+
)
161176

162177
@property
163178
def cmdoption_str(self) -> str:

pytest_md_report/plugin.py

+50-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
from collections import defaultdict
3-
from typing import Any, Dict, Mapping, Optional, Sequence, Tuple
3+
from typing import Any, Dict, List, Mapping, Optional, Sequence, Tuple
44

55
from _pytest.config import Config
66
from _pytest.config.argparsing import Parser
@@ -100,6 +100,13 @@ def pytest_addoption(parser: Parser) -> None:
100100
help=Option.MD_REPORT_FLAVOR.help_msg
101101
+ HelpMsg.EXTRA_MSG_TEMPLATE.format(Option.MD_REPORT_FLAVOR.envvar_str),
102102
)
103+
group.addoption(
104+
Option.MD_EXCLUDE_OUTCOMES.cmdoption_str,
105+
nargs="+",
106+
default=[],
107+
help=Option.MD_EXCLUDE_OUTCOMES.help_msg
108+
+ HelpMsg.EXTRA_MSG_TEMPLATE.format(Option.MD_EXCLUDE_OUTCOMES.envvar_str),
109+
)
103110

104111
parser.addini(
105112
Option.MD_REPORT.inioption_str,
@@ -157,6 +164,12 @@ def pytest_addoption(parser: Parser) -> None:
157164
default=None,
158165
help=Option.MD_REPORT_FLAVOR.help_msg,
159166
)
167+
parser.addini(
168+
Option.MD_EXCLUDE_OUTCOMES.inioption_str,
169+
type="args",
170+
default=[],
171+
help=Option.MD_EXCLUDE_OUTCOMES.help_msg,
172+
)
160173

161174

162175
def is_make_md_report(config: Config) -> bool:
@@ -298,6 +311,33 @@ def retrieve_md_flavor(config: Config) -> MarkdownFlavor:
298311
return normalize_md_flavor(str(md_flavor))
299312

300313

314+
def retrieve_exclude_outcomes(config: Config) -> List[str]:
315+
def norm_names(names: Sequence[Any]) -> List[str]:
316+
return [str(name).lower().strip() for name in names]
317+
318+
exclude_outcomes = config.option.md_report_exclude_outcomes
319+
320+
if not exclude_outcomes:
321+
exclude_outcomes = os.environ.get(Option.MD_EXCLUDE_OUTCOMES.envvar_str)
322+
if exclude_outcomes:
323+
return norm_names(exclude_outcomes.split(","))
324+
325+
if not exclude_outcomes:
326+
exclude_outcomes = config.getini(Option.MD_EXCLUDE_OUTCOMES.inioption_str)
327+
328+
if not exclude_outcomes:
329+
return Default.EXCLUDE_RESULTS
330+
331+
if isinstance(exclude_outcomes, list):
332+
# list will be passed via pytest config file
333+
return norm_names(exclude_outcomes)
334+
elif isinstance(exclude_outcomes, str):
335+
# comma-separated string (e.g. passed,skipped) will be passed via the command line option
336+
return norm_names(exclude_outcomes.split(","))
337+
338+
raise TypeError(f"Unexpected type {type(exclude_outcomes)} for exclude_outcomes")
339+
340+
301341
def retrieve_color_policy(config: Config) -> ColorPolicy:
302342
color_policy = config.option.md_report_color
303343

@@ -417,11 +457,17 @@ def make_md_report(
417457
color_policy: ColorPolicy,
418458
apply_ansi_escape: bool,
419459
md_flavor: MarkdownFlavor,
460+
exclude_outcomes: List[str],
420461
) -> str:
421462
verbosity_level = retrieve_verbosity_level(config)
422463

423464
outcomes = ["passed", "failed", "error", "skipped", "xfailed", "xpassed"]
465+
outcomes = [key for key in outcomes if key not in exclude_outcomes]
424466
outcomes = [key for key in outcomes if total_stats.get(key, 0) > 0]
467+
468+
if not outcomes:
469+
return ""
470+
425471
results_per_testfunc = extract_pytest_stats(
426472
reporter=reporter, outcomes=outcomes, verbosity_level=verbosity_level
427473
)
@@ -505,6 +551,7 @@ def pytest_unconfigure(config: Config) -> None:
505551
output_filepath = retrieve_output_filepath(config)
506552
color_policy = retrieve_color_policy(config)
507553
md_flavor = retrieve_md_flavor(config)
554+
exclude_outcomes = retrieve_exclude_outcomes(config)
508555

509556
is_output_term = is_tee or not output_filepath
510557
is_output_file = is_not_null_string(output_filepath)
@@ -522,6 +569,7 @@ def pytest_unconfigure(config: Config) -> None:
522569
color_policy=term_color_policy,
523570
apply_ansi_escape=apply_ansi_escape_to_term,
524571
md_flavor=md_flavor,
572+
exclude_outcomes=exclude_outcomes,
525573
)
526574
reporter._tw.write(term_report)
527575

@@ -544,6 +592,7 @@ def pytest_unconfigure(config: Config) -> None:
544592
color_policy=file_color_policy,
545593
apply_ansi_escape=apply_ansi_escape_to_file,
546594
md_flavor=md_flavor,
595+
exclude_outcomes=exclude_outcomes,
547596
)
548597

549598
if file_report:

tests/test_plugin.py

+26
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,29 @@ def test_pytest_md_report_flavor(testdir):
242242
| $$\textcolor{#f5f543}{\tt{TOTAL}}$$ | $$\textcolor{#23d18b}{\tt{1}}$$ | $$\textcolor{#f5f543}{\tt{1}}$$ | $$\textcolor{#f5f543}{\tt{2}}$$ |
243243
"""
244244
)
245+
246+
247+
def test_pytest_md_report_exclude_outcomes(testdir):
248+
testdir.makepyfile(PYFILE_MIX_TESTS)
249+
expected = dedent(
250+
"""\
251+
| filepath | failed | error | xfailed | SUBTOTAL |
252+
| ----------------------------------------- | -----: | ----: | ------: | -------: |
253+
| test_pytest_md_report_exclude_outcomes.py | 1 | 1 | 1 | 3 |
254+
| TOTAL | 1 | 1 | 1 | 6 |
255+
"""
256+
)
257+
output_filepath = testdir.tmpdir.join("report.md")
258+
testdir.runpytest(
259+
"--md-report",
260+
"--md-report-exclude-outcomes",
261+
"passed",
262+
"skipped",
263+
"xpassed",
264+
"--md-report-output",
265+
output_filepath,
266+
)
267+
with open(output_filepath) as f:
268+
report = f.read()
269+
print(report)
270+
assert report == expected

0 commit comments

Comments
 (0)