2011-04-07 10 views
6

मान लीजिए कि मैं testFile.py पायथन मॉड्यूल को निम्नानुसार परिभाषित करता हूं।उत्पन्न नाक परीक्षण

def test_evens(): 
    for i in range(0, 5): 
     yield check_even, i, i*3 

def check_even(n, nn): 
    assert n % 2 == 0 or nn % 2 == 0 

मैं नाक कलेक्ट-ओनली मोड में परीक्षण की पहचान करने मैं

testFile.test_evens(0, 0) ... ok 
testFile.test_evens(1, 3) ... ok 
testFile.test_evens(2, 6) ... ok 
testFile.test_evens(3, 9) ... ok 
testFile.test_evens(4, 12) ... ok 

मैं का उपयोग कर

nosetests -v testfile सभी परीक्षणों चला सकते हैं: test_evens

हालांकि, अगर मैं केवल testFile.test_evens (2, 6) चलाने के लिए चाहता हूं (यानी, सभी परीक्षण नहीं) ?

क्या कमांड लाइन से ऐसा करने का कोई तरीका है?

उत्तर

7

नाक डिफ़ॉल्ट रूप से मेरे ज्ञान के लिए ऐसा नहीं कर सकता है। यहाँ कुछ विकल्प हैं:

1. कमांड लाइन

शायद आप के लिए क्या नहीं देख रहे हैं से यह नकली है, लेकिन मैं यह उल्लेख करने के लिए किया था। तुम भी इस आसान बनाने के लिए एक आवरण स्क्रिप्ट बना सकते हैं:

python -c 'import testFile; testFile.check_even(2, 6)' 

2. एक कस्टम नाक परीक्षण लोडर

यह एक छोटे से अधिक शामिल है बनाएं, लेकिन आप एक कस्टम परीक्षण लोडर बना सकते हैं जो जनरेटर लोड करने के लिए जेनरेटर निर्दिष्ट करने के लिए कमांड लाइन तर्कों का अर्थ है, जेनरेटर से परीक्षण और तर्क खींचता है, और मिलान वाले तर्कों के साथ परीक्षण युक्त एक सूट देता है।

import ast 
import nose 

class CustomLoader(nose.loader.TestLoader): 

    def loadTestsFromName(self, name, module=None): 
     # parse the command line arg 
     parts = name.split('(', 1) 
     mod_name, func_name = parts[0].split('.') 
     args = ast.literal_eval('(' + parts[1]) 

     # resolve the module and function - you'll probably want to 
     # replace this with nose's internal discovery methods. 
     mod = __import__(mod_name) 
     func = getattr(mod, func_name) 

     # call the generator and gather all matching tests 
     tests = [] 
     if nose.util.isgenerator(func): 
      for test in func(): 
       _func, _args = self.parseGeneratedTest(test) 
       if _args == args: 
        tests.append(nose.case.FunctionTestCase(_func, arg=_args)) 
     return self.suiteClass(tests) 

nose.main(testLoader=CustomLoader) 

यह निष्पादित:

% python runner.py 'testFile.test_evens(2, 6)' -v 
testFile.check_even(2, 6) ... ok 

% python runner.py 'testFile.test_evens(2, 6)' 'testFile.test_evens(4, 12)' -v 
testFile.check_even(2, 6) ... ok 
testFile.check_even(4, 12) ... ok 

% python runner.py 'testFile.test_evens(1, 3)' -v 
testFile.check_even(1, 3) ... FAIL 

नीचे कुछ उदाहरण कोड है जो आपके पास पर्याप्त पर (runner.py) के निर्माण के लिए देना चाहिए है

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