2010-07-30 16 views
13

मेरे पास django.test.TestCase ऑब्जेक्ट में कुछ विधियां लिखी गई हैं जो मैं अपने वास्तविक डेटाबेस पर manage.py shell से चलाना चाहता हूं।मैं django टेस्टकेस मैन्युअल रूप से/अन्य डेटाबेस के विरुद्ध कैसे चला सकता हूं?

ValueError: no such test method in <class 'track.tests.MentionTests'>: runTest 

वहाँ TestCase वस्तुओं का दृष्टांत का कोई तरीका है: लेकिन जब मैं परीक्षा पद्धति को चलाने के लिए testcase वस्तु का दृष्टांत करने की कोशिश, मैं इस त्रुटि मिलती है? या एक गैर परीक्षण डेटाबेस के खिलाफ एक परीक्षण विधि चलाने का कोई तरीका है?

उत्तर

1

Django testing docs से:

Running tests

Once you've written tests, run them using the test subcommand of your project's manage.py utility:

$ ./manage.py test

By default, this will run every test in every application in INSTALLED_APPS. If you only want to run tests for a particular application, add the application name to the command line. For example, if your INSTALLED_APPS contains 'myproject.polls' and 'myproject.animals', you can run the myproject.animals unit tests alone with this command:

$ ./manage.py test animals

Note that we used animals, not myproject.animals. New in Django 1.0: You can now choose which test to run.

You can be even more specific by naming an individual test case. To run a single test case in an application (for example, the AnimalTestCase described in the "Writing unit tests" section), add the name of the test case to the label on the command line:

$ ./manage.py test animals.AnimalTestCase

And it gets even more granular than that! To run a single test method inside a test case, add the name of the test method to the label:

$ ./manage.py test animals.AnimalTestCase.testFluffyAnimals

पिछले उदाहरण आपके मामले में लागू किया जाना चाहिए।

यदि आप यही कर रहे हैं, तो आपको अपने परीक्षण मामले में नियोजित कोड का अधिक विस्तृत विवरण पोस्ट करना होगा।

+0

निश्चित रूप से, लेकिन यह चलाने के लिए एक नया परीक्षण डेटाबेस बनाता है। मैं इसे मुख्य (गैर-परीक्षण) डेटाबेस के विरुद्ध कैसे चला सकता हूं? – Leopd

11

यहां एक विधि है जिसे मैंने हाल ही में पाया था। मुझे अभी तक कुछ भी बेहतर नहीं मिला है।

from django.test.utils import setup_test_environment 
from unittest import TestResult 
from my_app.tests import TheTestWeWantToRun 

setup_test_environment() 
t = TheTestWeWantToRun('test_function_we_want_to_run') 
r = TestResult() 
t.run(r) 
r.testsRun # prints the number of tests that were run (should be 1) 
r.errors + r.failures # prints a list of the errors and failures 

दस्तावेज़ों के मुताबिक, हमें मैन्युअल रूप से परीक्षण चलाने पर setup_test_environment() पर कॉल करना चाहिए। django.test परीक्षण के लिए unittest का उपयोग करता है ताकि हम परीक्षण चलाते समय परिणामों को कैप्चर करने के लिए unittest से TestResult का उपयोग कर सकें।

Django 1.2, DjangoTestRunner में अधिक संरचित परीक्षण के लिए उपयोग किया जा सकता है। मैंने अभी तक यह कोशिश नहीं की है।

+1

जब मैं ऐसा करता हूं तो मेरा टेस्टकेस self.client तक नहीं पहुंच सकता है। टेस्टकेस के अंदर सभी टेस्ट फ़ंक्शंस को गेटैटर इत्यादि के बिना चलाने के लिए भी संभव है? – pielgrzym

+1

जो मैं वास्तव में चाहता हूं वह पीडीबी के तहत परीक्षण केस चलाने के लिए है। – mcr

+0

यह कमाल है, धन्यवाद! –

1

मैं इस में भाग, और निम्न समाधान बाहर काम किया:

अपने MyApp/tests.py में, इस तरह की बातें की स्थापना:

# get the straight-up Python unittest without the Django machinery                                                     
# NOTE: this is unittest2, a backport of unit testing features from Python 2.7                                                 
# (running 2.6 at the time of writing)                                                
from django.utils import unittest 
# get the Django wraps                                           
from django.test import TestCase as DjangoTestCase 

# [..] 
# your normal Django unit tests, inheriting from DjangoTestCase 
# [..] 

class MyTest(unittest.TestCase): 
    def runTest(self): # NOTE: required name 
     self.failUnless(True is True) 

def runNonDjangoTests(): 
    return MyTest() # or unittest.TestSuite([ MyTest(), .. ]) 

आप

के साथ इस परीक्षण चलाने
~$ unit2 myapp.tests.runNonDjangoTests 

अधिक जानकारी के लिए, http://pypi.python.org/pypi/unittest2

देखना

यह आपको सभी संभावित विनाशकारी साइड इफेक्ट्स के साथ मुख्य डेटाबेस के खिलाफ यूनिट परीक्षण चलाने देता है। ध्यान दें कि यूनिट 2 इस संदर्भ में बहुत खतरनाक है, यदि आप यूनिट 2 myapp.tests पर कॉल करते हैं तो यह आपके सभी सामान्य Django परीक्षणों को परीक्षण डेटाबेस में ले जाने के बिना चलाएगा।

2

"रनटेस्ट" समस्या आम तौर पर आती है क्योंकि लोग इस तथ्य को अनदेखा करते हैं कि unittest.TestCase के अपने कन्स्ट्रक्टर में एक डिफ़ॉल्ट तर्क है। lib/अजगर/unittest/case.py

class TestCase: 
    def __init__(self, methodName='runTest'): 

सूचना है कि baseclass "testcase" "डीईएफ़ runTest" की एक डिफ़ॉल्ट कार्यान्वयन प्रदान नहीं करता है, लेकिन यह nethertheless यह आह्वान करने के लिए कोशिश करता है पर एक नज़र डालें। यही वह जगह है जहां से त्रुटि आती है। वास्तविक भ्रम इस तथ्य से आता है कि "unittest.main()" का उपयोग रनटेस्ट विधि की आवश्यकता नहीं है लेकिन यह अभी भी सभी "def test *" फ़ंक्शंस को कॉल करेगा। यह काम करता है ... लेकिन टेस्टकेस के डिफ़ॉल्ट व्यवहार की वजह से नहीं बल्कि निरीक्षण कोड को अनजान से।मुख्य - कुछ इस तरह कर रही है निम्नलिखित:

class MyTest(unittest.TestCase): 
    def test_001(self): 
     print "ok" 

if __name__ == "__main__": 
    suite = unittest.TestSuite() 
    for method in dir(MyTest): 
     if method.startswith("test"): 
      suite.addTest(MyTest(method)) 
    unittest.TextTestRunner().run(suite) 

मूल प्रश्न "मैं कुछ एक django.test.TestCase में लिखा तरीकों" का जवाब: यदि आप अपने testclass का उपयोग करके एक testsuite पर व्यक्तिगत रूप से प्रत्येक विधि जोड़ने की जरूरत और ऑब्जेक्ट सृजन पर पहला तर्क के रूप में लक्ष्य विधि नाम प्रदान करना।

संबंधित मुद्दे