TST: Cleanup Excel tests to make it easier to add and test additional writers
State: closed


Labels:

Comments:
@jmcnamara - this should make it _much_ easier for you to add and test xlsxwriter. Just follow the pattern for OpenpyxlTests and XlwtTests and you should be good.

For those interested: this doesn't add or remove tests, just makes it _much_ easier to add new writers and new writer tests.

and preserves skip output:

```

#16 pandas.io.tests.test_excel.OpenpyxlTests.test_bool: openpyxl not installed, skipping
#17 pandas.io.tests.test_excel.OpenpyxlTests.test_colaliases: openpyxl not installed, skipping
#18 pandas.io.tests.test_excel.OpenpyxlTests.test_excel_010_hemstring: openpyxl not installed, skipping
#19 pandas.io.tests.test_excel.OpenpyxlTests.test_excel_roundtrip_datetime: openpyxl not installed, skipping
#20 pandas.io.tests.test_excel.OpenpyxlTests.test_excel_roundtrip_indexname: openpyxl not installed, skipping
#21 pandas.io.tests.test_excel.OpenpyxlTests.test_excel_sheet_by_name_raise: openpyxl not installed, skipping
#22 pandas.io.tests.test_excel.OpenpyxlTests.test_int64: openpyxl not installed, skipping
#23 pandas.io.tests.test_excel.OpenpyxlTests.test_mixed: openpyxl not installed, skipping
#24 pandas.io.tests.test_excel.OpenpyxlTests.test_roundtrip: openpyxl not installed, skipping
#25 pandas.io.tests.test_excel.OpenpyxlTests.test_roundtrip_indexlabels: openpyxl not installed, skipping
#26 pandas.io.tests.test_excel.OpenpyxlTests.test_sheets: openpyxl not installed, skipping
#27 pandas.io.tests.test_excel.OpenpyxlTests.test_to_excel_float_format: openpyxl not installed, skipping
#28 pandas.io.tests.test_excel.OpenpyxlTests.test_to_excel_multiindex: openpyxl not installed, skipping
#29 pandas.io.tests.test_excel.OpenpyxlTests.test_to_excel_multiindex_dates: openpyxl not installed, skipping
#30 pandas.io.tests.test_excel.OpenpyxlTests.test_to_excel_periodindex: openpyxl not installed, skipping
#31 pandas.io.tests.test_excel.OpenpyxlTests.test_to_excel_styleconverter: openpyxl not installed, skipping
#32 pandas.io.tests.test_excel.OpenpyxlTests.test_to_excel_unicode_filename: openpyxl not installed, skipping
#33 pandas.io.tests.test_excel.OpenpyxlTests.test_tsframe: openpyxl not installed, skipping
--------------------------------------------------------------------------------
#34 pandas.io.tests.test_excel.XlwtTests.test_bool: xlwt not installed, skipping
#35 pandas.io.tests.test_excel.XlwtTests.test_colaliases: xlwt not installed, skipping
#36 pandas.io.tests.test_excel.XlwtTests.test_excel_010_hemstring: xlwt not installed, skipping
#37 pandas.io.tests.test_excel.XlwtTests.test_excel_roundtrip_datetime: xlwt not installed, skipping
#38 pandas.io.tests.test_excel.XlwtTests.test_excel_roundtrip_indexname: xlwt not installed, skipping
#39 pandas.io.tests.test_excel.XlwtTests.test_excel_sheet_by_name_raise: xlwt not installed, skipping
#40 pandas.io.tests.test_excel.XlwtTests.test_int64: xlwt not installed, skipping
#41 pandas.io.tests.test_excel.XlwtTests.test_mixed: xlwt not installed, skipping
#42 pandas.io.tests.test_excel.XlwtTests.test_roundtrip: xlwt not installed, skipping
#43 pandas.io.tests.test_excel.XlwtTests.test_roundtrip_indexlabels: xlwt not installed, skipping
#44 pandas.io.tests.test_excel.XlwtTests.test_sheets: xlwt not installed, skipping
#45 pandas.io.tests.test_excel.XlwtTests.test_to_excel_float_format: xlwt not installed, skipping
#46 pandas.io.tests.test_excel.XlwtTests.test_to_excel_multiindex: xlwt not installed, skipping
#47 pandas.io.tests.test_excel.XlwtTests.test_to_excel_multiindex_dates: xlwt not installed, skipping
#48 pandas.io.tests.test_excel.XlwtTests.test_to_excel_periodindex: xlwt not installed, skipping
#49 pandas.io.tests.test_excel.XlwtTests.test_to_excel_styleconverter: xlwt not installed, skipping
#50 pandas.io.tests.test_excel.XlwtTests.test_to_excel_unicode_filename: xlwt not installed, skipping
#51 pandas.io.tests.test_excel.XlwtTests.test_tsframe: xlwt not installed, skipping
```

@jtratner That looks good. It will make adding tests for new engines very clean and straightforward.

@jtratner How do you set the writer engine from a subclass? Should it be set up via `set_option()` in a constructor and reset to the previous value in the destructor?

Or am I missing something more direct.

@jmcnamara oh...very good point. wasn't thinking, give me a second, I'll put in a patch for you so you can get it to work.

@jmcnamara you can just add it to your PR...not going to do a separate pull request.

You can run the following to get the patch below:

``` git
git remote add jtratner https://www.github.com/jtratner/pandas.git
git fetch jtratner
git checkout <insert-your-branch>
git cherry-pick 37db6b58eecb742d98b507224c8390d3faa9a994
```

Or you can copy it in yourself by saving this to a file and using `git apply` (less likely to work though if you've made changes to the file)

``` patch
diff --git a/pandas/io/tests/test_excel.py b/pandas/io/tests/test_excel.py
index 0053602..047f10c 100644
--- a/pandas/io/tests/test_excel.py
+++ b/pandas/io/tests/test_excel.py
@@ -16,6 +16,7 @@ from pandas.io.excel import (
     register_writer
 )
 from pandas.util.testing import ensure_clean
+from pandas.core.config import set_option, get_option
 import pandas.util.testing as tm
 import pandas as pd

@@ -277,6 +278,12 @@ class ExcelWriterBase(SharedItems):
     def setUp(self):
         self.check_skip()
         super(ExcelWriterBase, self).setUp()
+        self.option_name = 'io.excel.%s.writer' % self.ext
+        self.prev_engine = get_option(self.option_name)
+        set_option(self.option_name, self.engine_name)
+
+    def tearDown(self):
+        set_option(self.option_name, self.prev_engine)

     def test_excel_sheet_by_name_raise(self):
         _skip_if_no_xlrd()
@@ -790,6 +797,7 @@ class ExcelWriterBase(SharedItems):

 class OpenpyxlTests(ExcelWriterBase, unittest.TestCase):
     ext = 'xlsx'
+    engine_name = 'openpyxl'
     check_skip = staticmethod(_skip_if_no_openpyxl)

     def test_to_excel_styleconverter(self):
@@ -820,6 +828,7 @@ class OpenpyxlTests(ExcelWriterBase, unittest.TestCase):

 class XlwtTests(ExcelWriterBase, unittest.TestCase):
     ext = 'xls'
+    engine_name = 'xlwt'
     check_skip = staticmethod(_skip_if_no_xlwt)

     def test_to_excel_styleconverter(self):
```

@jmcnamara so obviously, once you integrate, just set `engine_name = 'xlsxwriter'` and you should be good.

@jtratner Got it Jeff. Thanks. 

Your refactoring work on this and `excel.py` really make adding new engines very easy.