Skip to content

Commit b5b70c7

Browse files
committed
TST: refactored html tests
1 parent 632a61d commit b5b70c7

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

pandas/tests/io/test_html.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,22 @@
2828
HERE = os.path.dirname(__file__)
2929

3030

31+
def pytest_generate_tests(metafunc):
32+
# Defers evaluation of the fixture until after collection.
33+
# https://docs.pytest.org/en/latest/example/parametrize.html\
34+
# deferring-the-setup-of-parametrized-resources
35+
if 'html_file' in metafunc.fixturenames:
36+
paths = glob.glob(
37+
os.path.join(HERE, 'data', 'html_encoding', '*.html')
38+
)
39+
metafunc.parametrize("html_file", paths, indirect=True)
40+
41+
42+
@pytest.fixture
43+
def html_file(request, datapath):
44+
return datapath(request.param)
45+
46+
3147
def assert_framelist_equal(list1, list2, *args, **kwargs):
3248
assert len(list1) == len(list2), ('lists are not of equal size '
3349
'len(list1) == {0}, '
@@ -838,22 +854,22 @@ def test_displayed_only(self, displayed_only, exp0, exp1):
838854
else:
839855
assert len(dfs) == 1 # Should not parse hidden table
840856

841-
@pytest.mark.parametrize("f", glob.glob(
842-
os.path.join(HERE, 'data', 'html_encoding', '*.html')))
843-
def test_encode(self, f):
844-
_, encoding = os.path.splitext(os.path.basename(f))[0].split('_')
857+
def test_encode(self, html_file):
858+
_, encoding = os.path.splitext(
859+
os.path.basename(html_file)
860+
)[0].split('_')
845861

846862
try:
847-
with open(f, 'rb') as fobj:
863+
with open(html_file, 'rb') as fobj:
848864
from_string = self.read_html(fobj.read(), encoding=encoding,
849865
index_col=0).pop()
850866

851-
with open(f, 'rb') as fobj:
867+
with open(html_file, 'rb') as fobj:
852868
from_file_like = self.read_html(BytesIO(fobj.read()),
853869
encoding=encoding,
854870
index_col=0).pop()
855871

856-
from_filename = self.read_html(f, encoding=encoding,
872+
from_filename = self.read_html(html_file, encoding=encoding,
857873
index_col=0).pop()
858874
tm.assert_frame_equal(from_string, from_file_like)
859875
tm.assert_frame_equal(from_string, from_filename)

pandas/tests/io/test_packers.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,8 @@ def test_msgpacks_legacy(self, current_packers_data, all_packers_data,
928928
# GH12142 0.17 files packed in P2 can't be read in P3
929929
if (compat.PY3 and version.startswith('0.17.') and
930930
legacy_packer.split('.')[-4][-1] == '2'):
931-
pytest.skip("Files packed in Py2 can't be read in Py3.")
931+
msg = "Files packed in Py2 can't be read in Py3 ({})"
932+
pytest.skip(msg.format(version))
932933
try:
933934
with catch_warnings(record=True):
934935
self.compare(current_packers_data, all_packers_data,

test_foo.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import pytest
2+
3+
CALL_COUNT = 0
4+
5+
6+
@pytest.fixture(scope="module")
7+
def fixture(request, datapath):
8+
global CALL_COUNT
9+
CALL_COUNT += 1
10+
11+
return request.param
12+
13+
14+
def pytest_generate_tests(metafunc):
15+
if "fixture" in metafunc.fixturenames:
16+
metafunc.parametrize("fixture", ["foo"], indirect=True, scope="module")
17+
18+
19+
@pytest.mark.parametrize("param", ["bar", "zaz"])
20+
def test_1(fixture, param):
21+
global CALL_COUNT
22+
assert CALL_COUNT == 1

0 commit comments

Comments
 (0)