Skip to content

Commit b491329

Browse files
simonjayhawkinsWillAyd
authored andcommitted
TST/CLN: engine fixture for tests/io/excel/test_readers.py (#27139)
1 parent ddec4eb commit b491329

File tree

2 files changed

+33
-44
lines changed

2 files changed

+33
-44
lines changed

pandas/tests/io/excel/test_readers.py

+24-29
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,29 @@ def ignore_xlrd_time_clock_warning():
3131
yield
3232

3333

34+
@pytest.fixture(params=[
35+
# Add any engines to test here
36+
pytest.param('xlrd', marks=td.skip_if_no('xlrd')),
37+
pytest.param('openpyxl', marks=td.skip_if_no('openpyxl')),
38+
pytest.param(None, marks=td.skip_if_no('xlrd')),
39+
])
40+
def engine(request):
41+
"""
42+
A fixture for Excel reader engines.
43+
"""
44+
return request.param
45+
46+
3447
class TestReaders:
3548

36-
@pytest.fixture(autouse=True, params=[
37-
# Add any engines to test here
38-
pytest.param('xlrd', marks=pytest.mark.skipif(
39-
not td.safe_import("xlrd"), reason="no xlrd")),
40-
pytest.param('openpyxl', marks=pytest.mark.skipif(
41-
not td.safe_import("openpyxl"), reason="no openpyxl")),
42-
pytest.param(None, marks=pytest.mark.skipif(
43-
not td.safe_import("xlrd"), reason="no xlrd")),
44-
])
45-
def cd_and_set_engine(self, request, datapath, monkeypatch, read_ext):
49+
@pytest.fixture(autouse=True)
50+
def cd_and_set_engine(self, engine, datapath, monkeypatch, read_ext):
4651
"""
4752
Change directory and set engine for read_excel calls.
4853
"""
49-
if request.param == 'openpyxl' and read_ext == '.xls':
54+
if engine == 'openpyxl' and read_ext == '.xls':
5055
pytest.skip()
51-
func = partial(pd.read_excel, engine=request.param)
56+
func = partial(pd.read_excel, engine=engine)
5257
monkeypatch.chdir(datapath("io", "data"))
5358
monkeypatch.setattr(pd, 'read_excel', func)
5459

@@ -726,23 +731,15 @@ def test_read_excel_squeeze(self, read_ext):
726731

727732
class TestExcelFileRead:
728733

729-
@pytest.fixture(autouse=True, params=[
730-
# Add any engines to test here
731-
pytest.param('xlrd', marks=pytest.mark.skipif(
732-
not td.safe_import("xlrd"), reason="no xlrd")),
733-
pytest.param('openpyxl', marks=pytest.mark.skipif(
734-
not td.safe_import("openpyxl"), reason="no openpyxl")),
735-
pytest.param(None, marks=pytest.mark.skipif(
736-
not td.safe_import("xlrd"), reason="no xlrd")),
737-
])
738-
def cd_and_set_engine(self, request, datapath, monkeypatch, read_ext):
734+
@pytest.fixture(autouse=True)
735+
def cd_and_set_engine(self, engine, datapath, monkeypatch, read_ext):
739736
"""
740737
Change directory and set engine for ExcelFile objects.
741738
"""
742-
if request.param == 'openpyxl' and read_ext == '.xls':
739+
if engine == 'openpyxl' and read_ext == '.xls':
743740
pytest.skip()
744741

745-
func = partial(pd.ExcelFile, engine=request.param)
742+
func = partial(pd.ExcelFile, engine=engine)
746743
monkeypatch.chdir(datapath("io", "data"))
747744
monkeypatch.setattr(pd, 'ExcelFile', func)
748745

@@ -830,20 +827,18 @@ def test_sheet_name(self, read_ext, df_ref):
830827
tm.assert_frame_equal(df1_parse, df_ref, check_names=False)
831828
tm.assert_frame_equal(df2_parse, df_ref, check_names=False)
832829

833-
def test_excel_read_buffer(self, read_ext):
830+
def test_excel_read_buffer(self, engine, read_ext):
834831
pth = 'test1' + read_ext
835-
engine = pd.ExcelFile.keywords['engine'] # TODO: fixturize
836832
expected = pd.read_excel(pth, 'Sheet1', index_col=0, engine=engine)
837833

838834
with open(pth, 'rb') as f:
839835
with pd.ExcelFile(f) as xls:
840836
actual = pd.read_excel(xls, 'Sheet1', index_col=0)
841837

842-
tm.assert_frame_equal(expected, actual)
838+
tm.assert_frame_equal(expected, actual)
843839

844-
def test_reader_closes_file(self, read_ext):
840+
def test_reader_closes_file(self, engine, read_ext):
845841
f = open('test1' + read_ext, 'rb')
846-
engine = pd.ExcelFile.keywords['engine'] # TODO: fixturize
847842
with pd.ExcelFile(f) as xlsx:
848843
# parses okay
849844
pd.read_excel(xlsx, 'Sheet1', index_col=0, engine=engine)

pandas/tests/io/excel/test_writers.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ def test_read_excel_parse_dates(self, ext):
224224
class _WriterBase:
225225

226226
@pytest.fixture(autouse=True)
227-
def set_engine_and_path(self, request, engine, ext):
227+
def set_engine_and_path(self, engine, ext):
228228
"""Fixture to set engine and open file for use in each test case
229229
230230
Rather than requiring `engine=...` to be provided explicitly as an
@@ -252,14 +252,10 @@ class and any subclasses, on account of the `autouse=True`
252252

253253
@td.skip_if_no('xlrd')
254254
@pytest.mark.parametrize("engine,ext", [
255-
pytest.param('openpyxl', '.xlsx', marks=pytest.mark.skipif(
256-
not td.safe_import('openpyxl'), reason='No openpyxl')),
257-
pytest.param('openpyxl', '.xlsm', marks=pytest.mark.skipif(
258-
not td.safe_import('openpyxl'), reason='No openpyxl')),
259-
pytest.param('xlwt', '.xls', marks=pytest.mark.skipif(
260-
not td.safe_import('xlwt'), reason='No xlwt')),
261-
pytest.param('xlsxwriter', '.xlsx', marks=pytest.mark.skipif(
262-
not td.safe_import('xlsxwriter'), reason='No xlsxwriter'))
255+
pytest.param('openpyxl', '.xlsx', marks=td.skip_if_no('openpyxl')),
256+
pytest.param('openpyxl', '.xlsm', marks=td.skip_if_no('openpyxl')),
257+
pytest.param('xlwt', '.xls', marks=td.skip_if_no('xlwt')),
258+
pytest.param('xlsxwriter', '.xlsx', marks=td.skip_if_no('xlsxwriter'))
263259
])
264260
class TestExcelWriter(_WriterBase):
265261
# Base class for test cases to run with different Excel writers.
@@ -1198,12 +1194,10 @@ def test_raise_when_saving_timezones(self, engine, ext, dtype,
11981194
class TestExcelWriterEngineTests:
11991195

12001196
@pytest.mark.parametrize('klass,ext', [
1201-
pytest.param(_XlsxWriter, '.xlsx', marks=pytest.mark.skipif(
1202-
not td.safe_import('xlsxwriter'), reason='No xlsxwriter')),
1203-
pytest.param(_OpenpyxlWriter, '.xlsx', marks=pytest.mark.skipif(
1204-
not td.safe_import('openpyxl'), reason='No openpyxl')),
1205-
pytest.param(_XlwtWriter, '.xls', marks=pytest.mark.skipif(
1206-
not td.safe_import('xlwt'), reason='No xlwt'))
1197+
pytest.param(_XlsxWriter, '.xlsx', marks=td.skip_if_no('xlsxwriter')),
1198+
pytest.param(
1199+
_OpenpyxlWriter, '.xlsx', marks=td.skip_if_no('openpyxl')),
1200+
pytest.param(_XlwtWriter, '.xls', marks=td.skip_if_no('xlwt'))
12071201
])
12081202
def test_ExcelWriter_dispatch(self, klass, ext):
12091203
with ensure_clean(ext) as path:

0 commit comments

Comments
 (0)