मैंने पद्धति से विरासत django.test.runner.DiscoverRunnerमें कुछ जोड़े और बनाने का विकल्प चुना run_tests।
यह देखने के लिए मेरा पहला अतिरिक्त जाँच है कि क्या db सेट करना आवश्यक है और setup_databasesयदि db आवश्यक है तो सामान्य कार्यक्षमता को किक करने की अनुमति देता है। मेरा दूसरा जोड़ सामान्य teardown_databasesचलाने की अनुमति देता है यदि setup_databasesविधि को चलाने की अनुमति दी गई थी।
मेरा कोड मानता है कि किसी भी TestCase से विरासत में मिला django.test.TransactionTestCase(और इस तरह django.test.TestCase) सेटअप करने के लिए डेटाबेस की आवश्यकता होती है। मैंने यह अनुमान लगाया क्योंकि Django डॉक्स का कहना है:
यदि आपको किसी अन्य अधिक जटिल और हैवीवेट Django- विशिष्ट सुविधाओं की आवश्यकता है ... जैसे ORM का परीक्षण करना या उपयोग करना ... तो आपको इसके बजाय TransactionTestCase या TestCase का उपयोग करना चाहिए।
https://docs.djangoproject.com/en/1.6/topics/testing/tools/#django.test.SimpleTestCase
mysite / scripts / settings.py
from django.test import TransactionTestCase
from django.test.runner import DiscoverRunner
class MyDiscoverRunner(DiscoverRunner):
def run_tests(self, test_labels, extra_tests=None, **kwargs):
"""
Run the unit tests for all the test labels in the provided list.
Test labels should be dotted Python paths to test modules, test
classes, or test methods.
A list of 'extra' tests may also be provided; these tests
will be added to the test suite.
If any of the tests in the test suite inherit from
``django.test.TransactionTestCase``, databases will be setup.
Otherwise, databases will not be set up.
Returns the number of tests that failed.
"""
self.setup_test_environment()
suite = self.build_suite(test_labels, extra_tests)
# ----------------- First Addition --------------
need_databases = any(isinstance(test_case, TransactionTestCase)
for test_case in suite)
old_config = None
if need_databases:
# --------------- End First Addition ------------
old_config = self.setup_databases()
result = self.run_suite(suite)
# ----------------- Second Addition -------------
if need_databases:
# --------------- End Second Addition -----------
self.teardown_databases(old_config)
self.teardown_test_environment()
return self.suite_result(suite, result)
अंत में, मैंने अपने प्रोजेक्ट की सेटिंग्स ओडीएफ फ़ाइल में निम्न पंक्ति जोड़ी।
mysite / settings.py
TEST_RUNNER = 'mysite.scripts.settings.MyDiscoverRunner'
अब, जब केवल गैर-डीबी-आश्रित परीक्षण चल रहा है, तो मेरा परीक्षण सूट तेजी से परिमाण का क्रम चलाता है! :)