Permalink
Browse files

Convert tests to use pytest.

 * docs/testing.rst: Document usage of pytest.
 * setup.py: Indicate pytest is required for tests and allow class to
   support running them.
 * tests/{dbobject, functional}/*.py: Remove unnecessary unittest
   references.  Convert self.assertXxx calls to simple assert's.  This
   wasn't required but took the opportunity to eliminate warnings
   raised by flake8.
 * tox.ini: Add pytest dependency and change command to run the tests.
  • Loading branch information...
jmafc committed Mar 5, 2013
1 parent 8f5b108 commit 05d1b150ba482abb6cb77ce28feaca71cf86ef1e
View
@@ -4,26 +4,20 @@ Testing
=======
The majority of Pyrseas' capabilities are exercised and verified via
-unit tests written using Python's `unittest framework
-<http://docs.python.org/library/unittest.html>`_. The tests can be
-run from the command line by most users, e.g.,
+unit tests written using `pytest <http://pytest.org/latest/>`_. The
+tests can be run from the command line by most users, e.g.,
::
- cd tests/dbobject
- python test_table.py
- python test_constraint.py ForeignKeyToMapTestCase
- python test_trigger.py TriggerToSqlTestCase.test_create_trigger
- python __init__.py
- python -m unittest discover
+ py.test tests/dbobject/test_table.py
+ py.test tests/dbobject/test_trigger.py -k test_create_trigger
+ py.test tests/functional
The first ``python`` command above runs all tests related to tables,
-mapping, creating, dropping, etc. The next command runs the subset of
-tests related to mapping tables with foreign keys and the following
-one executes a single test to generate SQL to create a trigger. The
-fourth command runs through all the tests suites in the ``dbobject``
-subdirectory. The final command does the same but using the test
-discovery feature available from Python 2.7.
+mapping, creating, dropping, etc. The second one executes a single
+test to generate SQL to create a trigger. The third runs all the
+functional tests. Please review the `pytest documentation
+<http://pytest.org/latest/usage.html>`_ for further options.
Environment Variables
---------------------
View
@@ -3,8 +3,21 @@
"""
Pyrseas - Framework and utilities to upgrade and maintain databases.
"""
+import sys
from setuptools import setup
+from setuptools.command.test import test as TestCommand
+
+class PyTest(TestCommand):
+ def finalize_options(self):
+ TestCommand.finalize_options(self)
+ self.test_args = []
+ self.test_suite = True
+ def run_tests(self):
+ import pytest
+ errno = pytest.main(self.test_args)
+ sys.exit(errno)
+
setup(
name='Pyrseas',
@@ -19,7 +32,8 @@
'psycopg2 >= 2.2',
'PyYAML >= 3.09'],
- test_suite='tests.dbobject',
+ tests_require=['pytest'],
+ cmdclass = {'test': PyTest},
author='Joe Abbate',
author_email='jma@freedomcircle.com',
View
@@ -1,63 +1 @@
-# -*- coding: utf-8 -*-
-"""Pyrseas unit tests"""
-
-import unittest
-
-from tests.dbobject import test_language
-from tests.dbobject import test_cast
-from tests.dbobject import test_schema
-from tests.dbobject import test_type
-from tests.dbobject import test_domain
-from tests.dbobject import test_sequence
-from tests.dbobject import test_table
-from tests.dbobject import test_column
-from tests.dbobject import test_constraint
-from tests.dbobject import test_index
-from tests.dbobject import test_view
-from tests.dbobject import test_function
-from tests.dbobject import test_operator
-from tests.dbobject import test_operfamily
-from tests.dbobject import test_operclass
-from tests.dbobject import test_trigger
-from tests.dbobject import test_rule
-from tests.dbobject import test_conversion
-from tests.dbobject import test_textsearch
-from tests.dbobject import test_foreign
-from tests.dbobject import test_extension
-from tests.dbobject import test_tablespace
-from tests.dbobject import test_collation
-from tests.dbobject import test_owner
-from tests.dbobject import test_privs
-
-
-def suite():
- tests = unittest.TestSuite()
- tests.addTest(test_language.suite())
- tests.addTest(test_cast.suite())
- tests.addTest(test_schema.suite())
- tests.addTest(test_type.suite())
- tests.addTest(test_domain.suite())
- tests.addTest(test_sequence.suite())
- tests.addTest(test_table.suite())
- tests.addTest(test_column.suite())
- tests.addTest(test_constraint.suite())
- tests.addTest(test_index.suite())
- tests.addTest(test_view.suite())
- tests.addTest(test_function.suite())
- tests.addTest(test_operator.suite())
- tests.addTest(test_operfamily.suite())
- tests.addTest(test_operclass.suite())
- tests.addTest(test_trigger.suite())
- tests.addTest(test_rule.suite())
- tests.addTest(test_conversion.suite())
- tests.addTest(test_textsearch.suite())
- tests.addTest(test_foreign.suite())
- tests.addTest(test_extension.suite())
- tests.addTest(test_tablespace.suite())
- tests.addTest(test_collation.suite())
- tests.addTest(test_owner.suite())
- tests.addTest(test_privs.suite())
- return tests
-
-if __name__ == '__main__':
- unittest.main(defaultTest='suite')
+"""Pyrseas dbobject unit tests"""
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
"""Test casts"""
-import unittest
+import pytest
from pyrseas.testutils import DatabaseToMapTestCase
from pyrseas.testutils import InputMapToSqlTestCase, fix_indent
@@ -25,20 +25,20 @@ def test_map_cast_function(self):
dbmap = self.to_map([CREATE_FUNC, CREATE_STMT1], superuser=True)
expmap = {'function': 'int2_bool(smallint)', 'context': 'explicit',
'method': 'function'}
- self.assertEqual(dbmap['cast (smallint as boolean)'], expmap)
+ assert dbmap['cast (smallint as boolean)'] == expmap
def test_map_cast_inout(self):
"Map a cast with INOUT"
dbmap = self.to_map([CREATE_DOMAIN, CREATE_STMT3])
expmap = {'context': 'implicit', 'method': 'inout'}
- self.assertEqual(dbmap['cast (d1 as integer)'], expmap)
+ assert dbmap['cast (d1 as integer)'] == expmap
def test_map_cast_comment(self):
"Map a cast comment"
dbmap = self.to_map([CREATE_FUNC, CREATE_STMT1, COMMENT_STMT],
superuser=True)
- self.assertEqual(dbmap['cast (smallint as boolean)']['description'],
- 'Test cast 1')
+ assert dbmap['cast (smallint as boolean)']['description'] == \
+ 'Test cast 1'
class CastToSqlTestCase(InputMapToSqlTestCase):
@@ -49,112 +49,97 @@ def test_create_cast_function(self):
stmts = [DROP_STMT, CREATE_FUNC]
inmap = self.std_map()
inmap.update({'cast (smallint as boolean)': {
- 'function': 'int2_bool(smallint)', 'context': 'explicit',
- 'method': 'function'}})
+ 'function': 'int2_bool(smallint)', 'context': 'explicit',
+ 'method': 'function'}})
sql = self.to_sql(inmap, stmts)
- self.assertEqual(fix_indent(sql[0]), CREATE_STMT1)
+ assert fix_indent(sql[0]) == CREATE_STMT1
def test_create_cast_inout(self):
"Create a cast with INOUT"
stmts = [CREATE_DOMAIN, "DROP CAST IF EXISTS (d1 AS integer)"]
inmap = self.std_map()
inmap.update({'cast (d1 as integer)': {
- 'context': 'implicit', 'method': 'inout'}})
+ 'context': 'implicit', 'method': 'inout'}})
inmap['schema public'].update({'domain d1': {'type': 'integer'}})
sql = self.to_sql(inmap, stmts)
- self.assertEqual(fix_indent(sql[0]), CREATE_STMT3)
+ assert fix_indent(sql[0]) == CREATE_STMT3
def test_create_cast_schema(self):
"Create a cast using a type/domain in a non-public schema"
stmts = ["CREATE SCHEMA s1", "CREATE DOMAIN s1.d1 AS integer",
"DROP CAST IF EXISTS (integer AS s1.d1)"]
inmap = self.std_map()
inmap.update({'cast (integer as s1.d1)': {
- 'context': 'assignment', 'method': 'binary coercible'}})
+ 'context': 'assignment', 'method': 'binary coercible'}})
inmap.update({'schema s1': {'domain d1': {'type': 'integer'}}})
sql = self.to_sql(inmap, stmts)
- self.assertEqual(fix_indent(sql[0]),
- "CREATE CAST (integer AS s1.d1) WITHOUT FUNCTION "
- "AS ASSIGNMENT")
+ assert fix_indent(sql[0]) == "CREATE CAST (integer AS s1.d1) " \
+ "WITHOUT FUNCTION AS ASSIGNMENT"
def test_bad_cast_map(self):
"Error creating a cast with a bad map"
inmap = self.std_map()
inmap.update({'(smallint as boolean)': {
- 'function': 'int2_bool(smallint)', 'context': 'explicit',
- 'method': 'function'}})
- self.assertRaises(KeyError, self.to_sql, inmap)
+ 'function': 'int2_bool(smallint)', 'context': 'explicit',
+ 'method': 'function'}})
+ with pytest.raises(KeyError):
+ self.to_sql(inmap)
def test_drop_cast(self):
"Drop an existing cast"
stmts = [DROP_STMT, CREATE_FUNC, CREATE_STMT1]
sql = self.to_sql(self.std_map(), stmts, superuser=True)
- self.assertEqual(sql[0], "DROP CAST (smallint AS boolean)")
+ assert sql[0] == "DROP CAST (smallint AS boolean)"
def test_cast_with_comment(self):
"Create a cast with a comment"
inmap = self.std_map()
inmap.update({'cast (smallint as boolean)': {
- 'description': 'Test cast 1',
- 'function': 'int2_bool(smallint)', 'context': 'explicit',
- 'method': 'function'}})
+ 'description': 'Test cast 1', 'function': 'int2_bool(smallint)',
+ 'context': 'explicit', 'method': 'function'}})
inmap['schema public'].update({'function int2_bool(smallint)': {
- 'returns': 'boolean', 'language': 'sql',
- 'immutable': True, 'source': SOURCE}})
+ 'returns': 'boolean', 'language': 'sql', 'immutable': True,
+ 'source': SOURCE}})
sql = self.to_sql(inmap, [DROP_STMT])
# sql[0] -> SET, sql[1] -> CREATE FUNCTION
- self.assertEqual(fix_indent(sql[2]), CREATE_STMT1)
- self.assertEqual(sql[3], COMMENT_STMT)
+ assert fix_indent(sql[2]) == CREATE_STMT1
+ assert sql[3] == COMMENT_STMT
def test_comment_on_cast(self):
"Create a comment for an existing cast"
stmts = [DROP_STMT, CREATE_FUNC, CREATE_STMT1]
inmap = self.std_map()
inmap.update({'cast (smallint as boolean)': {
- 'description': 'Test cast 1',
- 'function': 'int2_bool(smallint)', 'context': 'explicit',
- 'method': 'function'}})
+ 'description': 'Test cast 1', 'function': 'int2_bool(smallint)',
+ 'context': 'explicit', 'method': 'function'}})
inmap['schema public'].update({'function int2_bool(smallint)': {
- 'returns': 'boolean', 'language': 'sql',
- 'immutable': True, 'source': SOURCE}})
+ 'returns': 'boolean', 'language': 'sql', 'immutable': True,
+ 'source': SOURCE}})
sql = self.to_sql(inmap, stmts, superuser=True)
- self.assertEqual(sql, [COMMENT_STMT])
+ assert sql == [COMMENT_STMT]
def test_drop_cast_comment(self):
"Drop a comment on an existing cast"
stmts = [DROP_STMT, CREATE_FUNC, CREATE_STMT1, COMMENT_STMT]
inmap = self.std_map()
inmap.update({'cast (smallint as boolean)': {
- 'function': 'int2_bool(smallint)', 'context': 'explicit',
- 'method': 'function'}})
+ 'function': 'int2_bool(smallint)', 'context': 'explicit',
+ 'method': 'function'}})
inmap['schema public'].update({'function int2_bool(smallint)': {
- 'returns': 'boolean', 'language': 'sql',
- 'immutable': True, 'source': SOURCE}})
- sql = self.to_sql(inmap, stmts, superuser=True)
- self.assertEqual(sql, [
- "COMMENT ON CAST (smallint AS boolean) IS NULL"])
+ 'returns': 'boolean', 'language': 'sql', 'immutable': True,
+ 'source': SOURCE}})
+ assert self.to_sql(inmap, stmts, superuser=True) == \
+ ["COMMENT ON CAST (smallint AS boolean) IS NULL"]
def test_change_cast_comment(self):
"Change existing comment on a cast"
stmts = [DROP_STMT, CREATE_FUNC, CREATE_STMT1, COMMENT_STMT]
inmap = self.std_map()
inmap.update({'cast (smallint as boolean)': {
- 'description': 'Changed cast 1',
- 'function': 'int2_bool(smallint)', 'context': 'explicit',
- 'method': 'function'}})
+ 'description': 'Changed cast 1', 'function': 'int2_bool(smallint)',
+ 'context': 'explicit', 'method': 'function'}})
inmap['schema public'].update({'function int2_bool(smallint)': {
- 'returns': 'boolean', 'language': 'sql',
- 'immutable': True, 'source': SOURCE}})
- sql = self.to_sql(inmap, stmts, superuser=True)
- self.assertEqual(sql, [
- "COMMENT ON CAST (smallint AS boolean) IS 'Changed cast 1'"])
-
-
-def suite():
- tests = unittest.TestLoader().loadTestsFromTestCase(CastToMapTestCase)
- tests.addTest(unittest.TestLoader().loadTestsFromTestCase(
- CastToSqlTestCase))
- return tests
-
-if __name__ == '__main__':
- unittest.main(defaultTest='suite')
+ 'returns': 'boolean', 'language': 'sql', 'immutable': True,
+ 'source': SOURCE}})
+ assert self.to_sql(inmap, stmts, superuser=True) == \
+ ["COMMENT ON CAST (smallint AS boolean) IS 'Changed cast 1'"]
Oops, something went wrong.

0 comments on commit 05d1b15

Please sign in to comment.