Skip to content

Commit

Permalink
TST: test for warnings using pytest.warns() (#56974)
Browse files Browse the repository at this point in the history
* TST: test for warnings using tm.assert_produces_warning()

Fix tests that used `pytest.raises()` to check for warnings, to use
`tm.assert_produces_warning()` instead.  This is more correct, and it
fixes running the test suite without `-Werror`.  This is how Gentoo runs
it, to avoid sudden test failures due to new deprecation warnings from
upgraded dependencies.

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
mgorny and pre-commit-ci[bot] authored Jan 23, 2024
1 parent 213e38e commit 85b7445
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
8 changes: 5 additions & 3 deletions pandas/tests/io/xml/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,16 +1361,18 @@ def test_stylesheet_with_etree(kml_cta_rail_lines, xsl_flatten_doc):

@pytest.mark.parametrize("val", ["", b""])
def test_empty_stylesheet(val):
pytest.importorskip("lxml")
lxml_etree = pytest.importorskip("lxml.etree")

msg = (
"Passing literal xml to 'read_xml' is deprecated and "
"will be removed in a future version. To read from a "
"literal string, wrap it in a 'StringIO' object."
)
kml = os.path.join("data", "xml", "cta_rail_lines.kml")

with pytest.raises(FutureWarning, match=msg):
read_xml(kml, stylesheet=val)
with pytest.raises(lxml_etree.XMLSyntaxError):
with tm.assert_produces_warning(FutureWarning, match=msg):
read_xml(kml, stylesheet=val)


# ITERPARSE
Expand Down
6 changes: 4 additions & 2 deletions pandas/tests/tslibs/test_to_offset.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
to_offset,
)

import pandas._testing as tm


@pytest.mark.parametrize(
"freq_input,expected",
Expand Down Expand Up @@ -194,7 +196,7 @@ def test_to_offset_lowercase_frequency_deprecated(freq_depr):
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed in a "
f"future version, please use '{freq_depr.upper()[1:]}' instead."

with pytest.raises(FutureWarning, match=depr_msg):
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
to_offset(freq_depr)


Expand All @@ -214,5 +216,5 @@ def test_to_offset_uppercase_frequency_deprecated(freq_depr):
depr_msg = f"'{freq_depr[1:]}' is deprecated and will be removed in a "
f"future version, please use '{freq_depr.lower()[1:]}' instead."

with pytest.raises(FutureWarning, match=depr_msg):
with tm.assert_produces_warning(FutureWarning, match=depr_msg):
to_offset(freq_depr)

0 comments on commit 85b7445

Please sign in to comment.