Skip to content

Instantly share code, notes, and snippets.

@zzzeek
Last active August 29, 2015 13:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zzzeek/9319091 to your computer and use it in GitHub Desktop.
Save zzzeek/9319091 to your computer and use it in GitHub Desktop.
# run #1 - run with just --db sqlite, one test is generated/runs
classics-MacBook-Pro-2:sqlalchemy classic$ py.test test/dialect/test_suite.py --db sqlite -k HasTableTest
============================================================= test session starts ==============================================================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
plugins: cov
collected 121 items
test/dialect/test_suite.py <- lib/sqlalchemy/testing/suite/test_reflection.py:31: HasTableTest_sqlite_pysqlite.test_has_table PASSED
=================================================== 120 tests deselected by '-kHasTableTest' ===================================================
=================================================== 1 passed, 120 deselected in 0.06 seconds ===================================================
# run #2 - run with --db sqlite --db postgresql --db mysql, three copies are generated, one for each DB
classics-MacBook-Pro-2:sqlalchemy classic$ py.test test/dialect/test_suite.py --db sqlite --db postgresql --db mysql -k HasTableTest
============================================================= test session starts ==============================================================
platform darwin -- Python 2.7.5 -- py-1.4.20 -- pytest-2.5.2 -- /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python
plugins: cov
collected 183 items
test/dialect/test_suite.py <- lib/sqlalchemy/testing/suite/test_reflection.py:31: HasTableTest_mysql_mysqldb.test_has_table PASSED
test/dialect/test_suite.py <- lib/sqlalchemy/testing/suite/test_reflection.py:31: HasTableTest_sqlite_pysqlite.test_has_table PASSED
test/dialect/test_suite.py <- lib/sqlalchemy/testing/suite/test_reflection.py:31: HasTableTest_postgresql_psycopg2.test_has_table PASSED
=================================================== 180 tests deselected by '-kHasTableTest' ===================================================
=================================================== 3 passed, 180 deselected in 0.16 seconds ===================================================
classics-MacBook-Pro-2:sqlalchemy classic$
this is the hook, I'm not sure if I'm allowed to poke into the "unittest" plugin like this but this is how it had to be:
def pytest_configure(config):
# because it feels icky importing from "_pytest"..
global py_unittest
py_unittest = config.pluginmanager.getplugin('unittest')
def pytest_pycollect_makeitem(collector, name, obj):
if inspect.isclass(obj) and plugin_base.want_class(obj):
sub_objs = list(plugin_base.generate_sub_tests(obj))
for sub_obj in sub_objs:
if name != sub_obj.__name__:
setattr(collector.module, sub_obj.__name__, sub_obj)
return [
py_unittest.UnitTestCase(sub_obj.__name__, parent=collector)
for sub_obj in sub_objs
]
# elsewhere....just generate classes for certain test classes
def generate_sub_tests(cls):
if getattr(cls, '__multiple__', False):
for cfg in config.Config.all_configs():
name = "%s_%s_%s" % (cls.__name__, cfg.db.name, cfg.db.driver)
yield type(
name,
(cls, ),
{
"__only_on__": (cfg.db.name, cfg.db.driver),
"__multiple__": False}
)
else:
yield cls
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment