Skip to content

Commit

Permalink
ENH: Consider warnings as errors when running pytest
Browse files Browse the repository at this point in the history
Consider warnings as errors when running `pytest`:
- Add the `warnings-as-errors` flag to `pytest` to consider warnings as
  errors.
- Use an environment variable `NIFREEZE_WERRORS` in GHA workflow files
  to mark the corresponding workflows as treating warnings as errors.
- Process the `pytest` session end to mark it as failed if warnings were
  present.

Implementation drawn from DIPY.

Filter two warnings that are being raised by the current tests:
- Filter warnings related to GPR parameter optimal values being close to
  the boundary values raised by `scikit-learn`.
- Filter warnings related to the b0 threshold value being updated in
  gradient table slicing raised by DIPY.
  • Loading branch information
jhlegarreta committed Dec 22, 2024
1 parent 6b6ba70 commit 57398b0
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ env:
TEST_DATA_HOME: /home/runner/nifreeze-tests/
ITK_GLOBAL_DEFAULT_NUMBER_OF_THREADS: 4
ANTSPATH: /usr/share/miniconda/bin/
NIFREEZE_WERRORS: 1

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
Expand Down
8 changes: 7 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,13 @@ norecursedirs = [".*", "_*"]
addopts = "-v --doctest-modules"
doctest_optionflags = "ALLOW_UNICODE NORMALIZE_WHITESPACE ELLIPSIS"
env = "PYTHONHASHSEED=0"
filterwarnings = ["ignore::DeprecationWarning"]
filterwarnings = [
"ignore::DeprecationWarning",
# DIPY
"ignore:Updating b0_threshold to.*:UserWarning",
# scikit-learn
"ignore:The optimal value found for dimension.*:sklearn.exceptions.ConvergenceWarning",
]


[tool.coverage.run]
Expand Down
32 changes: 32 additions & 0 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,35 @@ def datadir():
def repodata():
"""Return the path to this repository's test data folder."""
return _datadir


def pytest_addoption(parser):
parser.addoption(
"--warnings-as-errors",
action="store_true",
help="Consider all uncaught warnings as errors.",
)


@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
have_werrors = os.getenv("NIFREEZE_WERRORS", False)
have_werrors = session.config.getoption("--warnings-as-errors", False) or have_werrors
if have_werrors:
# Check if there were any warnings during the test session
reporter = session.config.pluginmanager.get_plugin("terminalreporter")
if reporter.stats.get("warnings", None):
session.exitstatus = 2


@pytest.hookimpl
def pytest_terminal_summary(terminalreporter, exitstatus, config):
have_werrors = os.getenv("NIFREEZE_WERRORS", False)
have_werrors = config.getoption("--warnings-as-errors", False) or have_werrors
have_warnings = terminalreporter.stats.get("warnings", None)
if have_warnings and have_werrors:
terminalreporter.ensure_newline()
terminalreporter.section("Werrors", sep="=", red=True, bold=True)
terminalreporter.line(
f"Warnings as errors: Activated.\n{len(have_warnings)} warnings were raised and treated as errors.\n"
)

0 comments on commit 57398b0

Please sign in to comment.