Repository: modin-project/modin

Issue:
Implement pytest fixtures for reducing usage of "try-catch" block
Example:

```python
try:
     setup_excel_file(filename=unique_filename)
     ...
finally:
     teardown_test_files([unique_filename])
```


Discussion:

Resolving patch:
diff --git a/modin/conftest.py b/modin/conftest.py
--- a/modin/conftest.py
+++ b/modin/conftest.py
@@ -15,6 +15,7 @@
 import sys
 import pytest
 import pandas
+from pandas.util._decorators import doc
 import numpy as np
 import pyarrow as pa
 import pyarrow.parquet as pq
@@ -51,6 +52,7 @@ def _saving_make_api_url(token, _make_api_url=modin.utils._make_api_url):
 from modin.pandas.test.utils import (  # noqa: E402
     _make_csv_file,
     get_unique_filename,
+    make_default_file,
     teardown_test_files,
     NROWS,
     IO_OPS_DATA_DIR,
@@ -285,6 +287,14 @@ def pytest_runtest_call(item):
             )
 
 
+_doc_pytest_fixture = """
+Pytest fixture factory that makes temp {file_type} files for testing.
+
+Yields:
+    Function that generates {file_type} files
+"""
+
+
 @pytest.fixture(scope="class")
 def TestReadCSVFixture():
     filenames = []
@@ -329,11 +339,8 @@ def TestReadCSVFixture():
 
 
 @pytest.fixture
+@doc(_doc_pytest_fixture, file_type="csv")
 def make_csv_file():
-    """Pytest fixture factory that makes temp csv files for testing.
-    Yields:
-        Function that generates csv files
-    """
     filenames = []
 
     yield _make_csv_file(filenames)
@@ -342,6 +349,23 @@ def make_csv_file():
     teardown_test_files(filenames)
 
 
+def create_fixture(file_type):
+    @doc(_doc_pytest_fixture, file_type=file_type)
+    def fixture():
+        func, filenames = make_default_file(file_type=file_type)
+        yield func
+        teardown_test_files(filenames)
+
+    return fixture
+
+
+for file_type in ("json", "html", "excel", "feather", "stata", "hdf", "pickle", "fwf"):
+
+    fixture = create_fixture(file_type)
+    fixture.__name__ = f"make_{file_type}_file"
+    globals()[fixture.__name__] = pytest.fixture(fixture)
+
+
 @pytest.fixture
 def make_parquet_file():
     """Pytest fixture factory that makes a parquet file/dir for testing.
@@ -353,7 +377,8 @@ def make_parquet_file():
 
     def _make_parquet_file(
         filename,
-        row_size=NROWS,
+        nrows=NROWS,
+        ncols=2,
         force=True,
         directory=False,
         partitioned_columns=[],
@@ -362,32 +387,31 @@ def _make_parquet_file(
 
         Args:
             filename: The name of test file, that should be created.
-            row_size: Number of rows for the dataframe.
+            nrows: Number of rows for the dataframe.
+            ncols: Number of cols for the dataframe.
             force: Create a new file/directory even if one already exists.
             directory: Create a partitioned directory using pyarrow.
             partitioned_columns: Create a partitioned directory using pandas.
             Will be ignored if directory=True.
         """
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        if os.path.exists(filename) and not force:
-            pass
-        elif directory:
-            if os.path.exists(filename):
-                shutil.rmtree(filename)
+        if force or not os.path.exists(filename):
+            df = pandas.DataFrame(
+                {f"col{x + 1}": np.arange(nrows) for x in range(ncols)}
+            )
+            if directory:
+                if os.path.exists(filename):
+                    shutil.rmtree(filename)
+                else:
+                    os.makedirs(filename)
+                table = pa.Table.from_pandas(df)
+                pq.write_to_dataset(table, root_path=filename)
+            elif len(partitioned_columns) > 0:
+                df.to_parquet(filename, partition_cols=partitioned_columns)
             else:
-                os.mkdir(filename)
-            table = pa.Table.from_pandas(df)
-            pq.write_to_dataset(table, root_path=filename)
-        elif len(partitioned_columns) > 0:
-            df.to_parquet(filename, partition_cols=partitioned_columns)
-        else:
-            df.to_parquet(filename)
-
-        filenames.append(filename)
+                df.to_parquet(filename)
+            filenames.append(filename)
 
-    # Return function that generates csv files
+    # Return function that generates parquet files
     yield _make_parquet_file
 
     # Delete parquet file that was created


Test patch:
diff --git a/modin/pandas/test/data/test_data.fwf b/modin/pandas/test/data/test_data.fwf
new file mode 100644
--- /dev/null
+++ b/modin/pandas/test/data/test_data.fwf
@@ -0,0 +1,20 @@
+ACW000116041961TAVG -142  k  183  k  419  k  720  k 1075  k 1546  k 1517  k 1428  k 1360  k 1121  k  457  k  -92  k
+ACW000116041962TAVG   60  k   32  k -207  k  582  k  855  k 1328  k 1457  k 1340  k 1110  k  941  k  270  k -179  k
+ACW000116041963TAVG -766  k -606  k -152  k  488  k 1171  k 1574  k 1567  k 1543  k 1279  k  887  k  513  k -161  k
+ACW000116041964TAVG    9  k -138  k    2  k  685  k 1166  k 1389  k 1453  k 1504  k 1168  k  735  k  493  k   59  k
+ACW000116041965TAVG   -9  k -158  k  -15  k  537  k  934  k 1447  k 1434  k 1424  k 1324  k  921  k  -22  k -231  k
+ACW000116041966TAVG -490  k -614  k  108  k  246  k 1082  k 1642  k 1620  k 1471  k 1195  k  803  k  329  k    2  k
+ACW000116041967TAVG -270  k   36  k  397  k  481  k 1052  k 1373  k 1655  k 1598  k 1318  k  997  k  559  k  -96  k
+ACW000116041968TAVG -306  k -183  k  220  k  714  k  935  k 1635  k 1572  k 1718  k 1331  k  781  k  180  k  -56  k
+ACW000116041969TAVG -134  k -494  k -185  k  497  k  962  k 1634  k 1687  k 1773  k 1379  k  932  k  321  k -275  k
+ACW000116041970TAVG -483  k -704  k  -75  k  261  k 1093  k 1724  k 1470  k 1609  k 1163  k  836  k  300  k   73  k
+ACW000116041971TAVG   -6  k   83  k  -40  k  472  k 1180  k 1411  k 1700  k 1600  k 1165  k  908  k  361  k  383  k
+ACW000116041972TAVG -377  k   -4  k  250  k  556  k 1117  k 1444  k 1778  k 1545  k 1073  k  797  k  481  k  404  k
+ACW000116041973TAVG   61  k  169  k  453  k  472  k 1075  k 1545  k 1866  k 1579  k 1199  k  563  k  154  k   11  k
+ACW000116041974TAVG  191  k  209  k  339  k  748  k 1094  k 1463  k 1498  k 1541  k 1319  k  585  k  428  k  335  k
+ACW000116041975TAVG  346  k   88  k  198  k  488  k 1165  k 1483  k 1756  k 1906  k 1374  k  845  k  406  k  387  k
+ACW000116041976TAVG -163  k  -62  k -135  k  502  k 1128  k 1461  k 1822  k 1759  k 1136  k  715  k  458  k -205  k
+ACW000116041977TAVG -192  k -279  k  234  k  332  k 1128  k 1566  k 1565  k 1556  k 1126  k  949  k  421  k  162  k
+ACW000116041978TAVG   55  k -354  k   66  k  493  k 1155  k 1552  k 1564  k 1555  k 1061  k  932  k  688  k -464  k
+ACW000116041979TAVG -618  k -632  k   35  k  474  k  993  k 1566  k 1484  k 1483  k 1229  k  647  k  412  k  -40  k
+ACW000116041980TAVG -340  k -500  k  -35  k  524  k 1071  k 1534  k 1655  k 1502  k 1269  k  660  k  138  k  125  k
\ No newline at end of file
diff --git a/modin/pandas/test/test_io.py b/modin/pandas/test/test_io.py
--- a/modin/pandas/test/test_io.py
+++ b/modin/pandas/test/test_io.py
@@ -90,121 +90,11 @@ def assert_files_eq(path1, path2):
             return False
 
 
-def setup_json_file(filename, row_size=NROWS, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        df.to_json(filename)
-
-
-def setup_json_lines_file(filename, row_size=NROWS, col_size=2, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {f"col{x + 1}": np.arange(row_size) for x in range(col_size)}
-        )
-        df.to_json(filename, lines=True, orient="records")
-
-
-def setup_html_file(filename, row_size=NROWS, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        df.to_html(filename)
-
-
 def setup_clipboard(row_size=NROWS):
     df = pandas.DataFrame({"col1": np.arange(row_size), "col2": np.arange(row_size)})
     df.to_clipboard()
 
 
-def setup_excel_file(filename, row_size=NROWS, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        df.to_excel(filename)
-
-
-def setup_feather_file(filename, row_size=NROWS, ncols=2, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {f"col{x + 1}": np.arange(row_size) for x in range(ncols)}
-        )
-        df.to_feather(filename)
-
-
-def setup_hdf_file(filename, row_size=NROWS, force=True, format=None):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        df.to_hdf(filename, key="df", format=format)
-
-
-def setup_stata_file(filename, row_size=NROWS, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        df.to_stata(filename)
-
-
-def setup_pickle_file(filename, row_size=NROWS, force=True):
-    if os.path.exists(filename) and not force:
-        pass
-    else:
-        df = pandas.DataFrame(
-            {"col1": np.arange(row_size), "col2": np.arange(row_size)}
-        )
-        df.to_pickle(filename)
-
-
-def setup_fwf_file(filename, force=True, fwf_data=None):
-    if not force and os.path.exists(filename):
-        return
-
-    if fwf_data is None:
-        fwf_data = """ACW000116041961TAVG -142  k  183  k  419  k  720  k 1075  k 1546  k 1517  k 1428  k 1360  k 1121  k  457  k  -92  k
-ACW000116041962TAVG   60  k   32  k -207  k  582  k  855  k 1328  k 1457  k 1340  k 1110  k  941  k  270  k -179  k
-ACW000116041963TAVG -766  k -606  k -152  k  488  k 1171  k 1574  k 1567  k 1543  k 1279  k  887  k  513  k -161  k
-ACW000116041964TAVG    9  k -138  k    2  k  685  k 1166  k 1389  k 1453  k 1504  k 1168  k  735  k  493  k   59  k
-ACW000116041965TAVG   -9  k -158  k  -15  k  537  k  934  k 1447  k 1434  k 1424  k 1324  k  921  k  -22  k -231  k
-ACW000116041966TAVG -490  k -614  k  108  k  246  k 1082  k 1642  k 1620  k 1471  k 1195  k  803  k  329  k    2  k
-ACW000116041967TAVG -270  k   36  k  397  k  481  k 1052  k 1373  k 1655  k 1598  k 1318  k  997  k  559  k  -96  k
-ACW000116041968TAVG -306  k -183  k  220  k  714  k  935  k 1635  k 1572  k 1718  k 1331  k  781  k  180  k  -56  k
-ACW000116041969TAVG -134  k -494  k -185  k  497  k  962  k 1634  k 1687  k 1773  k 1379  k  932  k  321  k -275  k
-ACW000116041970TAVG -483  k -704  k  -75  k  261  k 1093  k 1724  k 1470  k 1609  k 1163  k  836  k  300  k   73  k
-ACW000116041971TAVG   -6  k   83  k  -40  k  472  k 1180  k 1411  k 1700  k 1600  k 1165  k  908  k  361  k  383  k
-ACW000116041972TAVG -377  k   -4  k  250  k  556  k 1117  k 1444  k 1778  k 1545  k 1073  k  797  k  481  k  404  k
-ACW000116041973TAVG   61  k  169  k  453  k  472  k 1075  k 1545  k 1866  k 1579  k 1199  k  563  k  154  k   11  k
-ACW000116041974TAVG  191  k  209  k  339  k  748  k 1094  k 1463  k 1498  k 1541  k 1319  k  585  k  428  k  335  k
-ACW000116041975TAVG  346  k   88  k  198  k  488  k 1165  k 1483  k 1756  k 1906  k 1374  k  845  k  406  k  387  k
-ACW000116041976TAVG -163  k  -62  k -135  k  502  k 1128  k 1461  k 1822  k 1759  k 1136  k  715  k  458  k -205  k
-ACW000116041977TAVG -192  k -279  k  234  k  332  k 1128  k 1566  k 1565  k 1556  k 1126  k  949  k  421  k  162  k
-ACW000116041978TAVG   55  k -354  k   66  k  493  k 1155  k 1552  k 1564  k 1555  k 1061  k  932  k  688  k -464  k
-ACW000116041979TAVG -618  k -632  k   35  k  474  k  993  k 1566  k 1484  k 1483  k 1229  k  647  k  412  k  -40  k
-ACW000116041980TAVG -340  k -500  k  -35  k  524  k 1071  k 1534  k 1655  k 1502  k 1269  k  660  k  138  k  125  k"""
-
-    with open(filename, "w") as f:
-        f.write(fwf_data)
-
-
 def eval_to_file(modin_obj, pandas_obj, fn, extension, **fn_kwargs):
     """Helper function to test `to_<extension>` methods.
 
@@ -1133,14 +1023,11 @@ def test_read_csv_file_handle(self, read_mode, make_csv_file):
         unique_filename = get_unique_filename()
         make_csv_file(filename=unique_filename)
 
-        try:
-            with open(unique_filename, mode=read_mode) as buffer:
-                df_pandas = pandas.read_csv(buffer)
-                buffer.seek(0)
-                df_modin = pd.read_csv(buffer)
-                df_equals(df_modin, df_pandas)
-        finally:
-            teardown_test_files([unique_filename])
+        with open(unique_filename, mode=read_mode) as buffer:
+            df_pandas = pandas.read_csv(buffer)
+            buffer.seek(0)
+            df_modin = pd.read_csv(buffer)
+            df_equals(df_modin, df_pandas)
 
     def test_unnamed_index(self):
         def get_internal_df(df):
@@ -1352,12 +1239,17 @@ def test_read_parquet_without_metadata(self):
                 "C": ["c"] * 2000,
             }
         )
-        pandas_df.to_csv(csv_fname, index=False)
-        # read into pyarrow table and write it to a parquet file
-        t = csv.read_csv(csv_fname)
-        parquet.write_table(t, parquet_fname)
+        try:
+            pandas_df.to_csv(csv_fname, index=False)
+            # read into pyarrow table and write it to a parquet file
+            t = csv.read_csv(csv_fname)
+            parquet.write_table(t, parquet_fname)
 
-        df_equals(pd.read_parquet(parquet_fname), pandas.read_parquet(parquet_fname))
+            df_equals(
+                pd.read_parquet(parquet_fname), pandas.read_parquet(parquet_fname)
+            )
+        finally:
+            teardown_test_files([parquet_fname, csv_fname])
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
@@ -1394,18 +1286,13 @@ def test_read_parquet_2462(self):
 
 class TestJson:
     @pytest.mark.parametrize("lines", [False, True])
-    def test_read_json(self, lines):
-        unique_filename = get_unique_filename(extension="json")
-        try:
-            setup_json_file(filename=unique_filename)
-            eval_io(
-                fn_name="read_json",
-                # read_json kwargs
-                path_or_buf=unique_filename,
-                lines=lines,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_json(self, make_json_file, lines):
+        eval_io(
+            fn_name="read_json",
+            # read_json kwargs
+            path_or_buf=make_json_file(lines=lines),
+            lines=lines,
+        )
 
     def test_read_json_categories(self):
         eval_io(
@@ -1450,118 +1337,87 @@ def test_to_json(self):
             ),
         ],
     )
-    def test_read_json_file_handle(self, read_mode):
-        unique_filename = get_unique_filename(extension="json")
-        try:
-            setup_json_file(filename=unique_filename)
-            with open(unique_filename, mode=read_mode) as buf:
-                df_pandas = pandas.read_json(buf)
-                buf.seek(0)
-                df_modin = pd.read_json(buf)
-                df_equals(df_pandas, df_modin)
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_json_file_handle(self, make_json_file, read_mode):
+        with open(make_json_file(), mode=read_mode) as buf:
+            df_pandas = pandas.read_json(buf)
+            buf.seek(0)
+            df_modin = pd.read_json(buf)
+            df_equals(df_pandas, df_modin)
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_json_metadata(self):
-        unique_filename = get_unique_filename(extension="json")
-        try:
-            setup_json_lines_file(unique_filename, col_size=80)
-
-            # `lines=True` is for triggering Modin implementation,
-            # `orient="records"` should be set if `lines=True`
-            df = pd.read_json(unique_filename, lines=True, orient="records")
-            parts_width_cached = df._query_compiler._modin_frame._column_widths_cache
-            num_splits = len(df._query_compiler._modin_frame._partitions[0])
-            parts_width_actual = [
-                len(df._query_compiler._modin_frame._partitions[0][i].get().columns)
-                for i in range(num_splits)
-            ]
-
-            assert parts_width_cached == parts_width_actual
+    def test_read_json_metadata(self, make_json_file):
+        # `lines=True` is for triggering Modin implementation,
+        # `orient="records"` should be set if `lines=True`
+        df = pd.read_json(
+            make_json_file(ncols=80, lines=True), lines=True, orient="records"
+        )
+        parts_width_cached = df._query_compiler._modin_frame._column_widths_cache
+        num_splits = len(df._query_compiler._modin_frame._partitions[0])
+        parts_width_actual = [
+            len(df._query_compiler._modin_frame._partitions[0][i].get().columns)
+            for i in range(num_splits)
+        ]
 
-        finally:
-            teardown_test_files([unique_filename])
+        assert parts_width_cached == parts_width_actual
 
 
 class TestExcel:
     @check_file_leaks
-    def test_read_excel(self):
-        unique_filename = get_unique_filename(extension="xlsx")
-        try:
-            setup_excel_file(filename=unique_filename)
-            eval_io(
-                fn_name="read_excel",
-                # read_excel kwargs
-                io=unique_filename,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_excel(self, make_excel_file):
+        eval_io(
+            fn_name="read_excel",
+            # read_excel kwargs
+            io=make_excel_file(),
+        )
 
     @check_file_leaks
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_excel_engine(self):
-        unique_filename = get_unique_filename(extension="xlsx")
-        try:
-            setup_excel_file(filename=unique_filename)
-            eval_io(
-                fn_name="read_excel",
-                modin_warning=UserWarning,
-                # read_excel kwargs
-                io=unique_filename,
-                engine="openpyxl",
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_excel_engine(self, make_excel_file):
+        eval_io(
+            fn_name="read_excel",
+            modin_warning=UserWarning,
+            # read_excel kwargs
+            io=make_excel_file(),
+            engine="openpyxl",
+        )
 
     @check_file_leaks
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_excel_index_col(self):
-        unique_filename = get_unique_filename(extension="xlsx")
-        try:
-            setup_excel_file(filename=unique_filename)
-
-            eval_io(
-                fn_name="read_excel",
-                modin_warning=UserWarning,
-                # read_excel kwargs
-                io=unique_filename,
-                index_col=0,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_excel_index_col(self, make_excel_file):
+        eval_io(
+            fn_name="read_excel",
+            modin_warning=UserWarning,
+            # read_excel kwargs
+            io=make_excel_file(),
+            index_col=0,
+        )
 
     @check_file_leaks
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_excel_all_sheets(self):
-        unique_filename = get_unique_filename(extension="xlsx")
-        try:
-            setup_excel_file(filename=unique_filename)
+    def test_read_excel_all_sheets(self, make_excel_file):
+        unique_filename = make_excel_file()
 
-            pandas_df = pandas.read_excel(unique_filename, sheet_name=None)
-            modin_df = pd.read_excel(unique_filename, sheet_name=None)
+        pandas_df = pandas.read_excel(unique_filename, sheet_name=None)
+        modin_df = pd.read_excel(unique_filename, sheet_name=None)
 
-            assert isinstance(pandas_df, (OrderedDict, dict))
-            assert isinstance(modin_df, type(pandas_df))
+        assert isinstance(pandas_df, (OrderedDict, dict))
+        assert isinstance(modin_df, type(pandas_df))
+        assert pandas_df.keys() == modin_df.keys()
 
-            assert pandas_df.keys() == modin_df.keys()
-
-            for key in pandas_df.keys():
-                df_equals(modin_df.get(key), pandas_df.get(key))
-        finally:
-            teardown_test_files([unique_filename])
+        for key in pandas_df.keys():
+            df_equals(modin_df.get(key), pandas_df.get(key))
 
     @pytest.mark.xfail(
         Engine.get() != "Python",
@@ -1607,22 +1463,19 @@ def test_read_excel_sheet_name(self, sheet_name):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="TypeError: Expected list, got type - issue #3284",
     )
-    def test_ExcelFile(self):
-        unique_filename = get_unique_filename(extension="xlsx")
-        try:
-            setup_excel_file(filename=unique_filename)
+    def test_ExcelFile(self, make_excel_file):
+        unique_filename = make_excel_file()
 
-            modin_excel_file = pd.ExcelFile(unique_filename)
-            pandas_excel_file = pandas.ExcelFile(unique_filename)
+        modin_excel_file = pd.ExcelFile(unique_filename)
+        pandas_excel_file = pandas.ExcelFile(unique_filename)
 
+        try:
             df_equals(modin_excel_file.parse(), pandas_excel_file.parse())
-
             assert modin_excel_file.io == unique_filename
             assert isinstance(modin_excel_file, pd.ExcelFile)
+        finally:
             modin_excel_file.close()
             pandas_excel_file.close()
-        finally:
-            teardown_test_files([unique_filename])
 
     @pytest.mark.xfail(strict=False, reason="Flaky test, defaults to pandas")
     def test_to_excel(self):
@@ -1652,21 +1505,15 @@ def test_to_excel(self):
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_excel_empty_frame(self):
-        unique_filename = get_unique_filename(extension="xlsx")
-        try:
-            setup_excel_file(filename=unique_filename)
-
-            eval_io(
-                fn_name="read_excel",
-                modin_warning=UserWarning,
-                # read_excel kwargs
-                io=unique_filename,
-                usecols=[0],
-                index_col=0,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_excel_empty_frame(self, make_excel_file):
+        eval_io(
+            fn_name="read_excel",
+            modin_warning=UserWarning,
+            # read_excel kwargs
+            io=make_excel_file(),
+            usecols=[0],
+            index_col=0,
+        )
 
 
 class TestHdf:
@@ -1675,18 +1522,13 @@ class TestHdf:
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_hdf(self, format):
-        unique_filename = get_unique_filename(extension="hdf")
-        try:
-            setup_hdf_file(filename=unique_filename, format=format)
-            eval_io(
-                fn_name="read_hdf",
-                # read_hdf kwargs
-                path_or_buf=unique_filename,
-                key="df",
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_hdf(self, make_hdf_file, format):
+        eval_io(
+            fn_name="read_hdf",
+            # read_hdf kwargs
+            path_or_buf=make_hdf_file(format=format),
+            key="df",
+        )
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
@@ -1836,13 +1678,8 @@ def test_to_sql(self, make_sql_connection, index):
 
 class TestHtml:
     @pytest.mark.xfail(reason="read_html is not yet implemented properly - issue #1296")
-    def test_read_html(self):
-        unique_filename = get_unique_filename(extension="html")
-        try:
-            setup_html_file(filename=unique_filename)
-            eval_io(fn_name="read_html", io=unique_filename)
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_html(self, make_html_file):
+        eval_io(fn_name="read_html", io=make_html_file())
 
     def test_to_html(self):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
@@ -1853,7 +1690,7 @@ def test_to_html(self):
 
 
 class TestFwf:
-    def test_fwf_file(self):
+    def test_fwf_file(self, make_fwf_file):
         fwf_data = (
             "id8141  360.242940  149.910199 11950.7\n"
             "id1594  444.953632  166.985655 11788.4\n"
@@ -1861,18 +1698,11 @@ def test_fwf_file(self):
             "id1230  413.836124  184.375703 11916.8\n"
             "id1948  502.953953  173.237159 12468.3\n"
         )
+        unique_filename = make_fwf_file(fwf_data=fwf_data)
 
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename, fwf_data=fwf_data)
-
-            colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)]
-            df = pd.read_fwf(
-                unique_filename, colspecs=colspecs, header=None, index_col=0
-            )
-            assert isinstance(df, pd.DataFrame)
-        finally:
-            teardown_test_files([unique_filename])
+        colspecs = [(0, 6), (8, 20), (21, 33), (34, 43)]
+        df = pd.read_fwf(unique_filename, colspecs=colspecs, header=None, index_col=0)
+        assert isinstance(df, pd.DataFrame)
 
     @pytest.mark.parametrize(
         "kwargs",
@@ -1905,20 +1735,16 @@ def test_fwf_file(self):
             },
         ],
     )
-    def test_fwf_file_colspecs_widths(self, kwargs):
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename)
+    def test_fwf_file_colspecs_widths(self, make_fwf_file, kwargs):
+        unique_filename = make_fwf_file()
 
-            modin_df = pd.read_fwf(unique_filename, **kwargs)
-            pandas_df = pd.read_fwf(unique_filename, **kwargs)
+        modin_df = pd.read_fwf(unique_filename, **kwargs)
+        pandas_df = pd.read_fwf(unique_filename, **kwargs)
 
-            df_equals(modin_df, pandas_df)
-        finally:
-            teardown_test_files([unique_filename])
+        df_equals(modin_df, pandas_df)
 
     @pytest.mark.parametrize("usecols", [["a"], ["a", "b", "d"], [0, 1, 3]])
-    def test_fwf_file_usecols(self, usecols):
+    def test_fwf_file_usecols(self, make_fwf_file, usecols):
         fwf_data = (
             "a       b           c          d\n"
             "id8141  360.242940  149.910199 11950.7\n"
@@ -1927,78 +1753,63 @@ def test_fwf_file_usecols(self, usecols):
             "id1230  413.836124  184.375703 11916.8\n"
             "id1948  502.953953  173.237159 12468.3\n"
         )
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=make_fwf_file(fwf_data=fwf_data),
+            usecols=usecols,
+        )
 
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename, fwf_data=fwf_data)
-
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                usecols=usecols,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_fwf_file_chunksize(self, make_fwf_file):
+        unique_filename = make_fwf_file()
 
-    def test_fwf_file_chunksize(self):
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename)
-
-            # Tests __next__ and correctness of reader as an iterator
-            rdf_reader = pd.read_fwf(unique_filename, chunksize=5)
-            pd_reader = pandas.read_fwf(unique_filename, chunksize=5)
+        # Tests __next__ and correctness of reader as an iterator
+        rdf_reader = pd.read_fwf(unique_filename, chunksize=5)
+        pd_reader = pandas.read_fwf(unique_filename, chunksize=5)
 
-            for modin_df, pd_df in zip(rdf_reader, pd_reader):
-                df_equals(modin_df, pd_df)
+        for modin_df, pd_df in zip(rdf_reader, pd_reader):
+            df_equals(modin_df, pd_df)
 
-            # Tests that get_chunk works correctly
-            rdf_reader = pd.read_fwf(unique_filename, chunksize=1)
-            pd_reader = pandas.read_fwf(unique_filename, chunksize=1)
+        # Tests that get_chunk works correctly
+        rdf_reader = pd.read_fwf(unique_filename, chunksize=1)
+        pd_reader = pandas.read_fwf(unique_filename, chunksize=1)
 
-            modin_df = rdf_reader.get_chunk(1)
-            pd_df = pd_reader.get_chunk(1)
+        modin_df = rdf_reader.get_chunk(1)
+        pd_df = pd_reader.get_chunk(1)
 
-            df_equals(modin_df, pd_df)
+        df_equals(modin_df, pd_df)
 
-            # Tests that read works correctly
-            rdf_reader = pd.read_fwf(unique_filename, chunksize=1)
-            pd_reader = pandas.read_fwf(unique_filename, chunksize=1)
+        # Tests that read works correctly
+        rdf_reader = pd.read_fwf(unique_filename, chunksize=1)
+        pd_reader = pandas.read_fwf(unique_filename, chunksize=1)
 
-            modin_df = rdf_reader.read()
-            pd_df = pd_reader.read()
+        modin_df = rdf_reader.read()
+        pd_df = pd_reader.read()
 
-            df_equals(modin_df, pd_df)
-        finally:
-            teardown_test_files([unique_filename])
+        df_equals(modin_df, pd_df)
 
     @pytest.mark.parametrize("nrows", [13, None])
-    def test_fwf_file_skiprows(self, nrows):
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename)
+    def test_fwf_file_skiprows(self, make_fwf_file, nrows):
+        unique_filename = make_fwf_file()
 
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                skiprows=2,
-                nrows=nrows,
-            )
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=unique_filename,
+            skiprows=2,
+            nrows=nrows,
+        )
 
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                usecols=[0, 4, 7],
-                skiprows=[2, 5],
-                nrows=nrows,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=unique_filename,
+            usecols=[0, 4, 7],
+            skiprows=[2, 5],
+            nrows=nrows,
+        )
 
-    def test_fwf_file_index_col(self):
+    def test_fwf_file_index_col(self, make_fwf_file):
         fwf_data = (
             "a       b           c          d\n"
             "id8141  360.242940  149.910199 11950.7\n"
@@ -2007,34 +1818,22 @@ def test_fwf_file_index_col(self):
             "id1230  413.836124  184.375703 11916.8\n"
             "id1948  502.953953  173.237159 12468.3\n"
         )
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=make_fwf_file(fwf_data=fwf_data),
+            index_col="c",
+        )
 
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename, fwf_data=fwf_data)
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                index_col="c",
-            )
-        finally:
-            teardown_test_files([unique_filename])
-
-    def test_fwf_file_skipfooter(self):
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename)
-
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                skipfooter=2,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_fwf_file_skipfooter(self, make_fwf_file):
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=make_fwf_file(),
+            skipfooter=2,
+        )
 
-    def test_fwf_file_parse_dates(self):
+    def test_fwf_file_parse_dates(self, make_fwf_file):
         dates = pandas.date_range("2000", freq="h", periods=10)
         fwf_data = "col1 col2        col3 col4"
         for i in range(10, 20):
@@ -2044,25 +1843,21 @@ def test_fwf_file_parse_dates(self):
                 col3=str(i),
                 col4=str(dates[i - 10].time()),
             )
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename, fwf_data=fwf_data)
+        unique_filename = make_fwf_file(fwf_data=fwf_data)
 
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                parse_dates=[["col2", "col4"]],
-            )
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=unique_filename,
+            parse_dates=[["col2", "col4"]],
+        )
 
-            eval_io(
-                fn_name="read_fwf",
-                # read_fwf kwargs
-                filepath_or_buffer=unique_filename,
-                parse_dates={"time": ["col2", "col4"]},
-            )
-        finally:
-            teardown_test_files([unique_filename])
+        eval_io(
+            fn_name="read_fwf",
+            # read_fwf kwargs
+            filepath_or_buffer=unique_filename,
+            parse_dates={"time": ["col2", "col4"]},
+        )
 
     @pytest.mark.parametrize(
         "read_mode",
@@ -2077,34 +1872,24 @@ def test_fwf_file_parse_dates(self):
             ),
         ],
     )
-    def test_read_fwf_file_handle(self, read_mode):
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename)
-
-            with open(unique_filename, mode=read_mode) as buffer:
-                df_pandas = pandas.read_fwf(buffer)
-                buffer.seek(0)
-                df_modin = pd.read_fwf(buffer)
-                df_equals(df_modin, df_pandas)
-        finally:
-            teardown_test_files([unique_filename])
-
-    def test_read_fwf_empty_frame(self):
+    def test_read_fwf_file_handle(self, make_fwf_file, read_mode):
+        with open(make_fwf_file(), mode=read_mode) as buffer:
+            df_pandas = pandas.read_fwf(buffer)
+            buffer.seek(0)
+            df_modin = pd.read_fwf(buffer)
+            df_equals(df_modin, df_pandas)
+
+    def test_read_fwf_empty_frame(self, make_fwf_file):
         kwargs = {
             "usecols": [0],
             "index_col": 0,
         }
-        unique_filename = get_unique_filename(extension="txt")
-        try:
-            setup_fwf_file(filename=unique_filename)
+        unique_filename = make_fwf_file()
 
-            modin_df = pd.read_fwf(unique_filename, **kwargs)
-            pandas_df = pandas.read_fwf(unique_filename, **kwargs)
+        modin_df = pd.read_fwf(unique_filename, **kwargs)
+        pandas_df = pandas.read_fwf(unique_filename, **kwargs)
 
-            df_equals(modin_df, pandas_df)
-        finally:
-            teardown_test_files([unique_filename])
+        df_equals(modin_df, pandas_df)
 
 
 class TestGbq:
@@ -2127,17 +1912,12 @@ def test_to_gbq(self):
 
 
 class TestStata:
-    def test_read_stata(self):
-        unique_filename = get_unique_filename(extension="stata")
-        try:
-            setup_stata_file(filename=unique_filename)
-            eval_io(
-                fn_name="read_stata",
-                # read_stata kwargs
-                filepath_or_buffer=unique_filename,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_stata(self, make_stata_file):
+        eval_io(
+            fn_name="read_stata",
+            # read_stata kwargs
+            filepath_or_buffer=make_stata_file(),
+        )
 
     def test_to_stata(self):
         modin_df, pandas_df = create_test_dfs(TEST_DATA)
@@ -2151,20 +1931,12 @@ class TestFeather:
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
         reason="The reason of tests fail in `cloud` mode is unknown for now - issue #3264",
     )
-    def test_read_feather(self):
-        unique_filename = get_unique_filename(extension="feather")
-        try:
-            # change the number of columns only if you know what you are doing;
-            # for details see https://github.com/modin-project/modin/pull/3465
-            setup_feather_file(filename=unique_filename, ncols=8)
-
-            eval_io(
-                fn_name="read_feather",
-                # read_feather kwargs
-                path=unique_filename,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_feather(self, make_feather_file):
+        eval_io(
+            fn_name="read_feather",
+            # read_feather kwargs
+            path=make_feather_file(),
+        )
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
@@ -2201,18 +1973,12 @@ def test_to_clipboard(self):
 
 
 class TestPickle:
-    def test_read_pickle(self):
-        unique_filename = get_unique_filename(extension="pkl")
-        try:
-            setup_pickle_file(filename=unique_filename)
-
-            eval_io(
-                fn_name="read_pickle",
-                # read_pickle kwargs
-                filepath_or_buffer=unique_filename,
-            )
-        finally:
-            teardown_test_files([unique_filename])
+    def test_read_pickle(self, make_pickle_file):
+        eval_io(
+            fn_name="read_pickle",
+            # read_pickle kwargs
+            filepath_or_buffer=make_pickle_file(),
+        )
 
     @pytest.mark.xfail(
         condition="config.getoption('--simulate-cloud').lower() != 'off'",
diff --git a/modin/pandas/test/utils.py b/modin/pandas/test/utils.py
--- a/modin/pandas/test/utils.py
+++ b/modin/pandas/test/utils.py
@@ -1280,3 +1280,62 @@ def rotate_decimal_digits_or_symbols(value):
         tens = value // 10
         ones = value % 10
         return tens + ones * 10
+
+
+def make_default_file(file_type: str):
+    """Helper function for pytest fixtures."""
+    filenames = []
+
+    def _create_file(filenames, filename, force, nrows, ncols, func: str, func_kw=None):
+        """
+        Helper function that creates a dataframe before writing it to a file.
+
+        Eliminates the duplicate code that is needed before of output functions calls.
+
+        Notes
+        -----
+        Importantly, names of created files are added to `filenames` variable for
+        their further automatic deletion. Without this step, files created by
+        `pytest` fixtures will not be deleted.
+        """
+        if force or not os.path.exists(filename):
+            df = pandas.DataFrame(
+                {f"col{x + 1}": np.arange(nrows) for x in range(ncols)}
+            )
+            getattr(df, func)(filename, **func_kw if func_kw else {})
+            filenames.append(filename)
+
+    file_type_to_extension = {
+        "excel": "xlsx",
+        "fwf": "txt",
+        "pickle": "pkl",
+    }
+    extension = file_type_to_extension.get(file_type, file_type)
+
+    def _make_default_file(filename=None, nrows=NROWS, ncols=2, force=True, **kwargs):
+        if filename is None:
+            filename = get_unique_filename(extension=extension)
+
+        if file_type == "json":
+            lines = kwargs.get("lines")
+            func_kw = {"lines": lines, "orient": "records"} if lines else {}
+            _create_file(filenames, filename, force, nrows, ncols, "to_json", func_kw)
+        elif file_type in ("html", "excel", "feather", "stata", "pickle"):
+            _create_file(filenames, filename, force, nrows, ncols, f"to_{file_type}")
+        elif file_type == "hdf":
+            func_kw = {"key": "df", "format": kwargs.get("format")}
+            _create_file(filenames, filename, force, nrows, ncols, "to_hdf", func_kw)
+        elif file_type == "fwf":
+            if force or not os.path.exists(filename):
+                fwf_data = kwargs.get("fwf_data")
+                if fwf_data is None:
+                    with open("modin/pandas/test/data/test_data.fwf", "r") as fwf_file:
+                        fwf_data = fwf_file.read()
+                with open(filename, "w") as f:
+                    f.write(fwf_data)
+                filenames.append(filename)
+        else:
+            raise ValueError(f"Unsupported file type: {file_type}")
+        return filename
+
+    return _make_default_file, filenames