Skip to content

Commit 5204a17

Browse files
authored
Fix test function for empty ICGEM gdf file (#345)
The test for checking if proper errors and warnings were raised when reading an empty ICGEM gdf file was failing in my local system due to a change in the Numpy warning for the empty file. Took the opportunity to refactor the test using pytest.raises and pytest.warns to capture the errors and warnings.
1 parent dc5cf30 commit 5204a17

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

harmonica/tests/test_icgem.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111

1212
import numpy as np
1313
import numpy.testing as npt
14-
from pytest import raises
14+
import pytest
15+
from pytest import raises, warns
1516

1617
from .. import load_icgem_gdf
1718

@@ -280,14 +281,20 @@ def test_corrupt_area(tmpdir):
280281
load_icgem_gdf(corrupt)
281282

282283

283-
def test_empty_file(tmpdir, recwarn):
284-
"Empty ICGEM file"
284+
@pytest.fixture(name="empty_fname")
285+
def fixture_empty_fname(tmpdir):
286+
"""
287+
Return the path to a temporary empty file
288+
"""
285289
empty_fname = str(tmpdir.join("empty.gdf"))
286290
with open(empty_fname, "w") as gdf_file:
287291
gdf_file.write("")
288-
with raises(IOError):
292+
return empty_fname
293+
294+
295+
def test_empty_file(empty_fname):
296+
"Empty ICGEM file"
297+
error = raises(IOError, match=r"Couldn't read \w+ field from gdf file header")
298+
warn = warns(UserWarning, match=r"loadtxt: input contained no data")
299+
with error, warn:
289300
load_icgem_gdf(empty_fname)
290-
assert len(recwarn) == 1
291-
warning = recwarn.pop()
292-
assert warning.category == UserWarning
293-
assert "Empty input file" in str(warning.message)

0 commit comments

Comments
 (0)