Skip to content

Commit

Permalink
Do not make use of pytest.warns(None)
Browse files Browse the repository at this point in the history
pytest.warns(None) is an anti-pattern, and there are easier ways to
accomplish both cases with how the code is leveraging pytest.warns().
Firstly, it can match on a specific warning and will assert that the
warning is raised -- and it doesn't need to be used if we want to assert
code raises no warnings, the standard library can do that directly.
Furthermore, pytest.warns(None) is an error in Pytest 8 and above.
  • Loading branch information
s-t-e-v-e-n-k committed Dec 16, 2024
1 parent e666b1a commit 7273f04
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions jenkinsapi_tests/unittests/test_build.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import warnings
import requests
import pytest
import pytz
Expand Down Expand Up @@ -258,14 +259,12 @@ def fake_get_data(cls, tree=None, params=None):
monkeypatch.setattr(Build, "get_data", fake_get_data)

with pytest.raises(requests.HTTPError) as excinfo:
with pytest.warns(None) as record:
with pytest.warns(
UserWarning,
match="Make sure the Environment Injector plugin is installed."
):
build.get_env_vars()
assert "404" == str(excinfo.value)
assert len(record) == 1
expected = UserWarning(
"Make sure the Environment Injector " "plugin is installed."
)
assert str(record[0].message) == str(expected)


def test_build_env_vars_other_exception(monkeypatch, build):
Expand All @@ -275,10 +274,10 @@ def fake_get_data(cls, tree=None, params=None):
monkeypatch.setattr(Build, "get_data", fake_get_data)

with pytest.raises(Exception) as excinfo:
with pytest.warns(None) as record:
with warnings.catch_warnings():
warnings.simplefilter("error")
build.get_env_vars()
assert "" == str(excinfo.value)
assert len(record) == 0


def test_build_get_status(build) -> None:
Expand Down

0 comments on commit 7273f04

Please sign in to comment.