[test] Handle both unittest2 and unittest in Python > 2.6
authorDenis Laxalde <denis.laxalde@logilab.fr>
Thu, 20 Mar 2014 09:06:41 +0100
changeset 400 24351d8749a9
parent 399 38e5c912fb86
child 401 b0e71c88b824
[test] Handle both unittest2 and unittest in Python > 2.6 Closes #230316.
test/test_alignment.py
test/test_blocking.py
test/test_dataio.py
test/test_distances.py
test/test_filters.py
test/test_minhashing.py
test/test_ner.py
test/test_normalize.py
test/test_preprocessors.py
test/test_tokenizer.py
--- a/test/test_alignment.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_alignment.py	Thu Mar 20 09:06:41 2014 +0100
@@ -16,7 +16,11 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
-import unittest2
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 import random
 random.seed(6) ### Make sure tests are repeatable
 from os import path
@@ -30,7 +34,7 @@
 TESTDIR = path.dirname(__file__)
 
 
-class AlignerTestCase(unittest2.TestCase):
+class AlignerTestCase(unittest.TestCase):
 
     def test_align(self):
         refset = [['V1', 'label1', (6.14194444444, 48.67)],
@@ -149,7 +153,7 @@
             self.assertIn(m, matched_wo_distance)
 
 
-class PipelineAlignerTestCase(unittest2.TestCase):
+class PipelineAlignerTestCase(unittest.TestCase):
 
     def test_pipeline_align_pairs(self):
         refset = [['V1', 'aaa', (6.14194444444, 48.67)],
@@ -180,5 +184,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_blocking.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_blocking.py	Thu Mar 20 09:06:41 2014 +0100
@@ -15,7 +15,12 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
-import unittest2
+
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 from os import path
 from functools import partial
 import random
@@ -58,7 +63,7 @@
                  ('a7', 'b6'),)
 
 
-class BaseBlockingTest(unittest2.TestCase):
+class BaseBlockingTest(unittest.TestCase):
 
     def test_baseblocking_blocks(self):
         blocking = KeyBlocking(ref_attr_index=1, target_attr_index=1,
@@ -125,7 +130,7 @@
             self.assertIn(pair, pairs)
 
 
-class KeyBlockingTest(unittest2.TestCase):
+class KeyBlockingTest(unittest.TestCase):
 
     def test_keyblocking_blocks(self):
         blocking = KeyBlocking(ref_attr_index=1, target_attr_index=1,
@@ -166,7 +171,7 @@
             self.assertIn(pair, pairs)
 
 
-class NGramBlockingTest(unittest2.TestCase):
+class NGramBlockingTest(unittest.TestCase):
 
     def test_ngram_blocks(self):
         blocking = NGramBlocking(ref_attr_index=1, target_attr_index=1)
@@ -201,7 +206,7 @@
         self.assertEqual(len(pairs), len(true_pairs))
 
 
-class SortedNeighborhoodBlockingTest(unittest2.TestCase):
+class SortedNeighborhoodBlockingTest(unittest.TestCase):
 
     def test_sorted_neighborhood_blocks(self):
         blocking = SortedNeighborhoodBlocking(ref_attr_index=1, target_attr_index=1,
@@ -229,7 +234,7 @@
             self.assertIn(block, blocks)
 
 
-class MergeBlockingTest(unittest2.TestCase):
+class MergeBlockingTest(unittest.TestCase):
 
 
     def test_merge_blocks(self):
@@ -269,7 +274,7 @@
             self.assertIn(block, blocks)
 
 
-class KmeansBlockingTest(unittest2.TestCase):
+class KmeansBlockingTest(unittest.TestCase):
 
     def test_clustering_blocking_kmeans(self):
         refset = [['V1', 'label1', (6.14194444444, 48.67)],
@@ -301,7 +306,7 @@
             self.assertIn(pair, pairs)
 
 
-class MinHashingBlockingTest(unittest2.TestCase):
+class MinHashingBlockingTest(unittest.TestCase):
 
     def test_minhashing(self):
         refset = [['V1', 'label1', u"Un nuage flotta dans le grand ciel bleu."],
@@ -324,7 +329,7 @@
             self.assertIn(align, blocks)
 
 
-class KdTreeBlockingTest(unittest2.TestCase):
+class KdTreeBlockingTest(unittest.TestCase):
 
     def test_kdtree(self):
         refset = [['V1', 'label1', (6.14194444444, 48.67)],
@@ -345,7 +350,7 @@
                           (['V4'], ['T2'])], blocks)
 
 
-class PipelineBlockingTest(unittest2.TestCase):
+class PipelineBlockingTest(unittest.TestCase):
 
     def test_pipeline_blocking(self):
         refset = [['1', 'aabb', 'ccdd'],
@@ -394,5 +399,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_dataio.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_dataio.py	Thu Mar 20 09:06:41 2014 +0100
@@ -16,7 +16,11 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
-import unittest2
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 import shutil
 from contextlib import contextmanager
 from os import path
@@ -43,7 +47,7 @@
             pass
 
 
-class ValidXHTMLPrettyPrintTest(unittest2.TestCase):
+class ValidXHTMLPrettyPrintTest(unittest.TestCase):
 
     def test_valid(self):
         from lxml import etree
@@ -89,7 +93,7 @@
                                 u'And <a href="http://example.com/me" class="ner">me</a>.'))
 
 
-class DataIOTestCase(unittest2.TestCase):
+class DataIOTestCase(unittest.TestCase):
 
     def test_parser(self):
         data = parsefile(path.join(TESTDIR, 'data', 'file2parse'),
@@ -226,5 +230,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_distances.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_distances.py	Thu Mar 20 09:06:41 2014 +0100
@@ -16,7 +16,11 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
-import unittest2
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 import random
 random.seed(6) ### Make sure tests are repeatable
 from dateutil import parser as dateparser
@@ -26,7 +30,7 @@
                                    LevenshteinProcessing)
 
 
-class DistancesTest(unittest2.TestCase):
+class DistancesTest(unittest.TestCase):
     def test_levenshtein(self):
         self.assertEqual(levenshtein('niche', 'chiens'), 5)
         self.assertEqual(levenshtein('bonjour', 'bonjour !'), 1)
@@ -123,7 +127,7 @@
         self.assertAlmostEqual(dist_parislondon, 341564, 0)
 
 
-class MatrixTestCase(unittest2.TestCase):
+class MatrixTestCase(unittest.TestCase):
 
     def setUp(self):
         self.input1 = [u'Victor Hugo', u'Albert Camus', 'Jean Valjean']
@@ -156,5 +160,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_filters.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_filters.py	Thu Mar 20 09:06:41 2014 +0100
@@ -15,7 +15,12 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
-import unittest2
+
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 
 from nazca.ner import NerProcess
 from nazca.ner.filters import (NerOccurenceFilter,
@@ -25,7 +30,7 @@
 from nazca.utils.tokenizer import Token, Sentence
 
 
-class FilterTest(unittest2.TestCase):
+class FilterTest(unittest.TestCase):
     """ Test of filters """
 
     def test_occurence_filter_min_occ(self):
@@ -115,5 +120,5 @@
                                  sentence=Sentence(indice=1, start=16, end=26)))])
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_minhashing.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_minhashing.py	Thu Mar 20 09:06:41 2014 +0100
@@ -15,8 +15,12 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 from functools import partial
-import unittest2
 from os import path
 import random
 random.seed(6) ### Make sure tests are repeatable
@@ -29,7 +33,7 @@
 
 
 
-class MinLSHTest(unittest2.TestCase):
+class MinLSHTest(unittest.TestCase):
 
     def test_iter_wordgrams(self):
         sentence = 'nom de la rose'
@@ -64,5 +68,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_ner.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_ner.py	Thu Mar 20 09:06:41 2014 +0100
@@ -15,7 +15,12 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
-import unittest2
+
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 
 from nazca.ner.sources import (NerSourceLexicon,
                                           NerSourceSparql,
@@ -25,7 +30,7 @@
 from nazca.ner.preprocessors import NerStopwordsFilterPreprocessor
 
 
-class NerTest(unittest2.TestCase):
+class NerTest(unittest.TestCase):
     """ Test of Ner """
 
     def test_lexicon_source(self):
@@ -226,5 +231,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_normalize.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_normalize.py	Thu Mar 20 09:06:41 2014 +0100
@@ -16,7 +16,11 @@
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
 
-import unittest2
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 from os import path
 
 from nazca.utils.normalize import (BaseNormalizer, UnicodeNormalizer, JoinNormalizer,
@@ -31,7 +35,7 @@
 TESTDIR = path.dirname(__file__)
 
 
-class NormalizerFunctionTestCase(unittest2.TestCase):
+class NormalizerFunctionTestCase(unittest.TestCase):
 
     def test_unormalize(self):
         self.assertEqual(lunormalize(u'bépoèàÀêùï'),
@@ -73,7 +77,7 @@
                          u'42')
 
 
-class NormalizerObjectTestCase(unittest2.TestCase):
+class NormalizerObjectTestCase(unittest.TestCase):
 
     def test_normalizer(self):
         normalizer = BaseNormalizer(lunormalize)
@@ -173,7 +177,7 @@
 
 
 
-class NormalizerPipelineTestCase(unittest2.TestCase):
+class NormalizerPipelineTestCase(unittest.TestCase):
 
     def test_normalizer(self):
         regexp = r'(?P<id>\d+);{["]?(?P<firstname>.+[^"])["]?};{(?P<surname>.*)};{};{};(?P<date>.*)'
@@ -187,5 +191,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_preprocessors.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_preprocessors.py	Thu Mar 20 09:06:41 2014 +0100
@@ -15,13 +15,18 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
-import unittest2
+
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 
 from nazca.utils import tokenizer
 from nazca.ner import preprocessors
 
 
-class PreprocessorTest(unittest2.TestCase):
+class PreprocessorTest(unittest.TestCase):
     """ Test of preprocessors """
 
     def test_lowercasefilter(self):
@@ -94,5 +99,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()
 
--- a/test/test_tokenizer.py	Fri Mar 21 10:07:03 2014 +0000
+++ b/test/test_tokenizer.py	Thu Mar 20 09:06:41 2014 +0100
@@ -15,12 +15,17 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with this program. If not, see <http://www.gnu.org/licenses/>.
-import unittest2
+
+import sys
+if sys.version_info >= (2, 7):
+    import unittest
+else:
+    import unittest2 as unittest
 
 from nazca.utils.tokenizer import RichStringTokenizer, Token, Sentence
 
 
-class TokenizerTest(unittest2.TestCase):
+class TokenizerTest(unittest.TestCase):
     """ Test of tokenizer """
 
     def test_richstringtokenizer(self):
@@ -84,5 +89,5 @@
 
 
 if __name__ == '__main__':
-    unittest2.main()
+    unittest.main()