Skip to content

Commit

Permalink
feat: allow disabling warnings when using brownie test
Browse files Browse the repository at this point in the history
  • Loading branch information
iamdefinitelyahuman committed May 2, 2020
1 parent ef67355 commit d8cff8e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 8 deletions.
1 change: 1 addition & 0 deletions brownie/_cli/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
--durations [num] show slowest setup/test durations (num=0 for all)
--exitfirst -x Exit instantly on first error or failed test
--verbose -v Increase verbosity
--disable-warnings -w Disable all warnings
Help Options:
--fixtures Show a list of available fixtures
Expand Down
40 changes: 32 additions & 8 deletions brownie/test/managers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,24 @@ def _make_nodemap(self, ids):
self.node_map.setdefault(path, []).append(test)

def pytest_sessionstart(self):
# remove PytestAssertRewriteWarning from terminalreporter warnings
"""
Called after the `Session` object has been created and before performing
collection and entering the run test loop.
Removes `PytestAssertRewriteWarning` warnings from the terminalreporter.
This prevents warnings that "the `brownie` library was already imported and
so related assertions cannot be rewritten". The warning is not relevant
for end users who are performing tests with brownie, not on brownie,
so we suppress it to avoid confusion.
Removal of pytest warnings must be handled in this hook because session
information is passed between xdist workers and master prior to test execution.
"""
reporter = self.config.pluginmanager.get_plugin("terminalreporter")
if "warnings" in reporter.stats:
warnings = reporter.stats["warnings"]
warnings = [i for i in warnings if "PytestAssertRewriteWarning" not in i.message]
if not warnings:
del reporter.stats["warnings"]
else:
reporter.stats["warnings"] = warnings
warnings = reporter.stats.pop("warnings", [])
warnings = [i for i in warnings if "PytestAssertRewriteWarning" not in i.message]
if warnings and not self.config.getoption("--disable-warnings"):
reporter.stats["warnings"] = warnings

def pytest_report_teststatus(self, report):
if report.when == "setup":
Expand All @@ -125,6 +134,21 @@ def pytest_report_teststatus(self, report):
return "xpassed", "X", "XPASS"
return report.outcome, convert_outcome(report.outcome), report.outcome.upper()

def pytest_terminal_summary(self, terminalreporter):
"""
Add a section to terminal summary reporting.
When the `--disable-warnings` flag is active, removes all raised warnings
prior to outputting the final console report.
Arguments
---------
terminalreporter : `_pytest.terminal.TerminalReporter`
the internal terminal reporter object
"""
if self.config.getoption("--disable-warnings") and "warnings" in terminalreporter.stats:
del terminalreporter.stats["warnings"]

def _sessionfinish_coverage(self, coverage_eval):
if CONFIG.argv["coverage"]:
output._print_coverage_totals(self.project._build, coverage_eval)
Expand Down

0 comments on commit d8cff8e

Please sign in to comment.