Skip to content

Instantly share code, notes, and snippets.

@charettes
Created May 19, 2015 21:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save charettes/37d3be452deb3232851a to your computer and use it in GitHub Desktop.
Save charettes/37d3be452deb3232851a to your computer and use it in GitHub Desktop.
diff --git a/django/test/runner.py b/django/test/runner.py
index 1106c52..e053bd3 100644
--- a/django/test/runner.py
+++ b/django/test/runner.py
@@ -211,6 +211,27 @@ class DiscoverRunner(object):
result = self.run_suite(suite)
self.teardown_databases(old_config)
self.teardown_test_environment()
+ from itertools import groupby
+ def keyfunc(test):
+ cls = test.__class__
+ return "%s.%s" % (cls.__module__, cls.__name__)
+ ignored = {test for test, _reason in result.skipped}
+ ignored |= {test for test, _traceback in result.errors}
+ ignored |= {test for test, _traceback in result.failures}
+ names = []
+ import six
+ attr = '__func__' if six.PY3 else 'im_func'
+ setUpTestData = getattr(TestCase.setUpTestData, attr)
+ for cls, tests in groupby(sorted(suite._tests, key=keyfunc), keyfunc):
+ tests = list(tests)
+ if (getattr(cls, 'allow_database_queries', False) and
+ hasattr(cls, 'setUpTestData') and
+ getattr(cls.setUpTestData, attr) is setUpTestData and
+ not any(test in ignored for test in tests) and
+ sum(test.queries_count for test in tests) == 0):
+ names.append("%s.%s" % (cls.__module__, cls.__name__))
+ for name in sorted(names):
+ print(name)
return self.suite_result(suite, result)
diff --git a/django/test/testcases.py b/django/test/testcases.py
index 5d2f74e..d1f22e7 100644
--- a/django/test/testcases.py
+++ b/django/test/testcases.py
@@ -80,6 +80,7 @@ class _AssertNumQueriesContext(CaptureQueriesContext):
def __exit__(self, exc_type, exc_value, traceback):
super(_AssertNumQueriesContext, self).__exit__(exc_type, exc_value, traceback)
+ return
if exc_type is not None:
return
executed = len(self)
@@ -203,16 +204,36 @@ class SimpleTestCase(unittest.TestCase):
skipped = (getattr(self.__class__, "__unittest_skip__", False) or
getattr(testMethod, "__unittest_skip__", False))
+ self.queries_count = 0
+
if not skipped:
try:
- self._pre_setup()
+ with CaptureQueriesContext(connections['default']) as c1, CaptureQueriesContext(connections['other']) as c2:
+ self._pre_setup()
+ self.queries_count += len([
+ q for q in c1.captured_queries if 'SAVEPOINT' not in q['sql']
+ ])
+ self.queries_count += len([
+ q for q in c2.captured_queries if 'SAVEPOINT' not in q['sql']
+ ])
except Exception:
result.addError(self, sys.exc_info())
return
- super(SimpleTestCase, self).__call__(result)
+ with CaptureQueriesContext(connections['default']) as c1, CaptureQueriesContext(connections['other']) as c2:
+ super(SimpleTestCase, self).__call__(result)
+ if not skipped:
+ self.queries_count += len(c1.captured_queries)
+ self.queries_count += len(c2.captured_queries)
if not skipped:
try:
- self._post_teardown()
+ with CaptureQueriesContext(connections['default']) as c1, CaptureQueriesContext(connections['other']) as c2:
+ self._post_teardown()
+ self.queries_count += len([
+ q for q in c1.captured_queries if 'SAVEPOINT' not in q['sql']
+ ])
+ self.queries_count += len([
+ q for q in c2.captured_queries if 'SAVEPOINT' not in q['sql']
+ ])
except Exception:
result.addError(self, sys.exc_info())
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment