Start test using py.test
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Thu, 01 Dec 2016 23:14:53 +0100
changeset 809 3f6fd47874d8
parent 808 1efe574ab302
child 810 38ca753c8491
Start test using py.test This includes turning generative test into subtest, using standard api for doctest and some use of bare unittest instead of lgc.testlib as bonus.
test/unittest_parser.py
test/unittest_rqlgen.py
test/unittest_stcheck.py
tox.ini
--- a/test/unittest_parser.py	Fri Jan 27 16:45:33 2017 +0100
+++ b/test/unittest_parser.py	Thu Dec 01 23:14:53 2016 +0100
@@ -1,5 +1,5 @@
 # -*- coding: iso-8859-1 -*-
-# copyright 2004-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2004-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of rql.
@@ -18,9 +18,7 @@
 # with rql. If not, see <http://www.gnu.org/licenses/>.
 from __future__ import print_function
 
-from six import text_type
-
-from logilab.common.testlib import TestCase, unittest_main
+from six import text_type, PY2
 
 from yapps.runtime import print_error, SyntaxError
 
@@ -28,6 +26,12 @@
 from rql import BadRQLQuery, RQLSyntaxError, nodes, stmts, parse
 from rql import parse
 
+if PY2:
+    import unittest2 as unittest
+else:
+    import unittest
+
+
 BAD_SYNTAX_QUERIES = (
     'ANY X WHERE X name Nulll;',
     'ANY X WHERE (X name NULL or X name "chouette";',
@@ -170,7 +174,7 @@
     'Any U,G WHERE U login UL, G name GL, G is CWGroup HAVING UPPER(UL)?=UPPER(GL);',
     )
 
-class ParserHercule(TestCase):
+class ParserHercule(unittest.TestCase):
     _syntaxerr = SyntaxError
 
     def parse(self, string, print_errors=False):
@@ -325,16 +329,20 @@
         for rql in SPEC_QUERIES:
 #            print("Orig:", rql)
 #            print("Resu:", rqltree)
-            yield self.parse, rql, True
+            with self.subTest(rql=rql):
+                self.parse(rql, True)
 
     def test_raise_badsyntax_error(self):
         for rql in BAD_SYNTAX_QUERIES:
-            yield self.assertRaises, self._syntaxerr, self.parse, rql
+            with self.subTest(rql=rql):
+                self.assertRaises(self._syntaxerr, self.parse, rql)
 
     def test_raise_badrqlquery(self):
         BAD_QUERIES = ('Person Marcou;',)
         for rql in BAD_QUERIES:
-            yield self.assertRaises, BadRQLQuery, self.parse, rql
+            with self.subTest(rql=rql):
+                self.assertRaises(BadRQLQuery, self.parse, rql)
+
 
 class ParserRQLHelper(ParserHercule):
     _syntaxerr = RQLSyntaxError
@@ -347,4 +355,4 @@
 
 
 if __name__ == '__main__':
-    unittest_main()
+    unittest.main()
--- a/test/unittest_rqlgen.py	Fri Jan 27 16:45:33 2017 +0100
+++ b/test/unittest_rqlgen.py	Thu Dec 01 23:14:53 2016 +0100
@@ -1,4 +1,4 @@
-# copyright 2004-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2004-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of rql.
@@ -15,19 +15,19 @@
 #
 # You should have received a copy of the GNU Lesser General Public License along
 # with rql. If not, see <http://www.gnu.org/licenses/>.
-"""
-Copyright (c) 2000-2008 LOGILAB S.A. (Paris, FRANCE).
-http://www.logilab.fr/ -- mailto:contact@logilab.fr
-"""
 
-from logilab.common.testlib import TestCase, DocTest, unittest_main
+import doctest
+import unittest
 
 from rql import rqlgen
 
-class RQLGenDocTest(DocTest):
-    module = rqlgen
 
-class RQLGenTC(TestCase):
+def load_tests(loader, tests, ignore):
+    tests.addTests(doctest.DocTestSuite(rqlgen))
+    return tests
+
+
+class RQLGenTC(unittest.TestCase):
     """tests the rqlgen behaviour
     """
 
@@ -130,4 +130,4 @@
                           'X lastname "Kent"')
         
 if __name__ == '__main__':
-    unittest_main()
+    unittest.main()
--- a/test/unittest_stcheck.py	Fri Jan 27 16:45:33 2017 +0100
+++ b/test/unittest_stcheck.py	Thu Dec 01 23:14:53 2016 +0100
@@ -1,4 +1,4 @@
-# copyright 2004-2012 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
+# copyright 2004-2016 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
 #
 # This file is part of rql.
@@ -17,7 +17,12 @@
 # with rql. If not, see <http://www.gnu.org/licenses/>.
 from __future__ import print_function
 
-from logilab.common.testlib import TestCase, unittest_main
+import six
+
+if six.PY2:
+    import unittest2 as unittest
+else:
+    import unittest
 
 from rql import RQLHelper, BadRQLQuery, stmts, nodes
 
@@ -86,7 +91,8 @@
     'SET X number CAST(Int, Y) WHERE X name Y',
     )
 
-class CheckClassTest(TestCase):
+
+class CheckClassTest(unittest.TestCase):
     """check wrong queries are correctly detected"""
 
     def setUp(self):
@@ -103,11 +109,13 @@
 
     def test_raise(self):
         for rql in BAD_QUERIES:
-            yield self._test, rql
+            with self.subTest(rql=rql):
+                self._test(rql)
 
     def test_ok(self):
         for rql in OK_QUERIES:
-            yield self.parse, rql
+            with self.subTest(rql=rql):
+                self.parse(rql)
 
     def _test_rewrite(self, rql, expected):
         rqlst = self.parse(rql)
@@ -187,7 +195,8 @@
             ('Any X WHERE EXISTS(X work_for Y, Y eid IN (12)) OR X eid IN (12)',
              'Any X WHERE (EXISTS(X work_for 12)) OR (X eid 12)'),
             ):
-            yield self._test_rewrite, rql, expected
+            with self.subTest(rql=rql):
+                self._test_rewrite(rql, expected)
 
     def test_subquery_graphdict(self):
         # test two things:
@@ -212,7 +221,7 @@
 ##         self.annotate(rqlst)
 ##         self.assertEqual(rqlst.as_string(), 'Any X WHERE X eid 12')
 
-class CopyTest(TestCase):
+class CopyTest(unittest.TestCase):
 
     def setUp(self):
         helper = RQLHelper(DummySchema(), None, {'eid': 'uid'})
@@ -246,7 +255,7 @@
         self.assertEqual(copy.defined_vars['U'].valuable_references(), 3)
 
 
-class AnnotateTest(TestCase):
+class AnnotateTest(unittest.TestCase):
 
     def setUp(self):
         helper = RQLHelper(DummySchema(), None, {'eid': 'uid'})
@@ -272,7 +281,6 @@
         self.assertTrue(C.scope is rqlst, C.scope)
         self.assertEqual(len(C.stinfo['relations']), 2)
 
-
     def test_not_rel_normalization_1(self):
         rqlst = self.parse('Any X WHERE C is Company, NOT X work_for C').children[0]
         self.assertEqual(rqlst.as_string(), 'Any X WHERE C is Company, NOT EXISTS(X work_for C)')
@@ -331,4 +339,4 @@
 
 
 if __name__ == '__main__':
-    unittest_main()
+    unittest.main()
--- a/tox.ini	Fri Jan 27 16:45:33 2017 +0100
+++ b/tox.ini	Thu Dec 01 23:14:53 2016 +0100
@@ -2,4 +2,11 @@
 envlist=py27,py34
 
 [testenv]
-commands = {envpython} -m logilab.common.pytest -t test
+deps =
+  pytest
+  py27: unittest2 >= 0.7
+commands = {envpython} -m pytest {posargs:test}
+
+[pytest]
+python_files = *test_*.py
+