2013-04-12 5 views
130

हमारी टीम में, हम इस तरह सबसे परीक्षण मामलों को परिभाषित:कमांड लाइन के माध्यम से unittest.TestCase से ही परीक्षण चल रहा है

एक "रूपरेखा" वर्ग ourtcfw.py:

import unittest 

class OurTcFw(unittest.TestCase): 
    def setUp: 
     # something 

    # other stuff that we want to use everywhere 

और परीक्षण का एक बहुत testMyCase.py तरह के मामलों:

import localweather 

class MyCase(OurTcFw): 

    def testItIsSunny(self): 
     self.assertTrue(localweather.sunny) 

    def testItIsHot(self): 
     self.assertTrue(localweather.temperature > 20) 

if __name__ == "__main__": 
    unittest.main() 

जब मैं नए परीक्षण कोड लिख रहा हूँ और यह अक्सर चलाने के लिए, और बचाने के लिए समय चाहते हैं, मुझे क्या करना है कि मैं डाल "__" अन्य सभी परीक्षण के सामने है। लेकिन यह बोझिल है, मुझे जो कोड लिख रहा है उससे मुझे परेशान करता है और जो बनाता है वह प्रतिबद्ध शोर सादा कष्टप्रद है।

तो उदा। जब testItIsHot() में परिवर्तन करने, मैं यह करने के लिए सक्षम होना चाहते हैं:

$ python testMyCase.py testItIsHot 

और unittest रन है केवलtestItIsHot()

कैसे मुझे लगता है कि प्राप्त कर सकते हैं?

मैंने if __name__ == "__main__": भाग को फिर से लिखने की कोशिश की, लेकिन चूंकि मैं पाइथन के लिए नया हूं, इसलिए मैं खो रहा हूं और विधियों की तुलना में बाकी सब कुछ में झुकाव रखता हूं।

+2

कृपया स्वीकार किए गए [उत्तर] (https://stackoverflow.com/a/26531790/) को अजय द्वारा एक को अपडेट करने पर विचार करें; यह वास्तव में काम करता है। –

उत्तर

161

यह काम करता है आप का सुझाव के रूप में - तुम सिर्फ रूप में अच्छी तरह वर्ग के नाम निर्दिष्ट करने के लिए है:

python testMyCase.py MyCase.testItIsHot 
+1

ओह मेरी! चूंकि परीक्षण Python2.6 पर चलने के लिए हैं (99% समय * मैं परीक्षण पाइथन 2.7 के साथ परीक्षण कर सकता हूं), मैं 2.6.8 डॉक्टर को देख रहा था और बहुत याद किया! :-) –

+1

बस ध्यान दिया गया कि यह केवल तभी काम करता है जब विधि को "test *" कहा जाता है, इसलिए दुर्भाग्य से इसे कभी-कभी परीक्षण चलाने के लिए उपयोग नहीं किया जा सकता है जो –

+2

का नाम बदलकर "अक्षम" है, उपनिर्देशिका में परीक्षणों के लिए काम नहीं करता है - सबसे अधिक एक परिपक्व पायथन कार्यक्रम में आम मामला। –

42

यह अच्छी तरह से काम कर सकते हैं के रूप में आप

python testMyCase.py MyCase.testItIsHot 

लगता है और वहाँ सिर्फ परीक्षण करने के लिए एक और तरीका है testItIsHot:

suite = unittest.TestSuite() 
    suite.addTest(MyCase("testItIsHot")) 
    runner = unittest.TextTestRunner() 
    runner.run(suite) 
+6

मुझे इस उत्तर का दूसरा भाग बेहद सहायक पाया गया: मैं एक्लिप्स + पायडेव में परीक्षण लिख रहा हूं और मैं कमांड लाइन पर स्विच नहीं करना चाहता हूं! –

63

आप अपने परीक्षण मामलों का आयोजन करते हैं, तो वह है, एक ही अंग का पालन करें और वास्तविक कोड की तरह ization भी एक ही पैकेज में मॉड्यूल के लिए रिश्तेदार के आयात का उपयोग

तुम भी निम्न आदेश स्वरूप का उपयोग कर सकते हैं: आप unittest मॉड्यूल इसके बारे में आपको बताता है की मदद की जाँच तो

python -m unittest mypkg.tests.test_module.TestClass.test_method 
# In your case, this would be: 
python -m unittest testMyCase.MyCase.testItIsHot 
+9

यह स्वीकार्य उत्तर होना चाहिए। –

9

कई संयोजन जो आपको एक मॉड्यूल से टेस्ट केस क्लास चलाने और टेस्ट केस क्लास से परीक्षण विधियों को चलाने की अनुमति देते हैं।

python3 -m unittest -h 

[...] 

Examples: 
    python3 -m unittest test_module    - run tests from test_module 
    python3 -m unittest module.TestClass   - run tests from module.TestClass 
    python3 -m unittest module.Class.test_method - run specified test method 

यह अपने मॉड्यूल के डिफ़ॉल्ट व्यवहार के रूप में एक unittest.main() परिभाषित करने के लिए आप की आवश्यकता नहीं है।

+0

+1 और चूंकि शब्दावली भ्रमित हो सकती है यदि किसी भाषा में नया (और 'उपयोग' भी अजीब असंगत है): चल रहा है' python -m unittest module_test.TestClass.test_method' एक फ़ाइल 'module_test.py' मानता है (वर्तमान से चलाएं निर्देशिका; और '__init.py__' _not_ आवश्यक है); और 'module_test.py' में 'क्लास टेस्टक्लास (unittest.TestCase) है ...' जिसमें 'def test_method (self, ...)' है (यह मेरे लिए अजगर 2.7.13 पर भी काम करता है) – michael

-4

यहाँ एक और तरीका

testname = "testItIsHot" 
testcase = MyCase(testname) 
testcase.testItIsHot() 
+4

यह उत्तर नहीं देता है प्रश्न, जो स्पष्ट रूप से "कमांड लाइन के माध्यम से" कहता है। –

1

@yarkee मैं कोड मैं पहले से ही मिल गया से कुछ के साथ यह संयुक्त से प्रेरित है। आप कमांड लाइन का उपयोग किए बिना फंक्शन run_unit_tests() को कॉल करके, इसे किसी अन्य स्क्रिप्ट से भी कॉल कर सकते हैं, या इसे python3 my_test_file.py के साथ कमांड लाइन से कॉल करें।

import my_test_file 
my_test_file.run_unit_tests() 

Python 3.3 या बेहतर के लिए दुर्भाग्य से यह केवल काम करता है:

import unittest 


class LineBalancingUnitTests(unittest.TestCase): 

    @classmethod 
    def setUp(self): 
     self.maxDiff = None 

    def test_it_is_sunny(self): 
     self.assertTrue("a" == "a") 

    def test_it_is_hot(self): 
     self.assertTrue("a" != "b") 


def run_unit_tests(): 
    runner = unittest.TextTestRunner() 

    classes = \ 
    [ 
     LineBalancingUnitTests, 
    ] 

    # Comment all the tests names on this list, to run all Unit Tests 
    unit_tests_to_run = \ 
    [ 
     "test_it_is_sunny", 
     # "test_it_is_hot", 
    ] 

    runner.run(suite(classes, unit_tests_to_run)) 


def suite(classes, unit_tests_to_run): 
    """ 
     Problem with sys.argv[1] when unittest module is in a script 
     https://stackoverflow.com/questions/2812218/problem-with-sys-argv1-when-unittest-module-is-in-a-script 

     Is there a way to loop through and execute all of the functions in a Python class? 
     https://stackoverflow.com/questions/2597827/is-there-a-way-to-loop-through-and-execute-all-of-the-functions 

     looping over all member variables of a class in python 
     https://stackoverflow.com/questions/1398022/looping-over-all-member-variables-of-a-class-in-python 
    """ 
    suite = unittest.TestSuite() 
    unit_tests_to_run_count = len(unit_tests_to_run) 

    for _class in classes: 
     _object = _class() 

     for function_name in dir(_object): 

      if function_name.lower().startswith("test"): 

       if unit_tests_to_run_count > 0 \ 
         and function_name not in unit_tests_to_run: 

        continue 

       suite.addTest(_class(function_name)) 

    return suite 


if __name__ == "__main__": 

    print("\n\n") 
    run_unit_tests() 

एक छोटे से कोड का संपादन, आप सभी इकाई परीक्षण के साथ एक सरणी पारित कर सकते हैं आप चाहते हैं कॉल करने के लिए:

... 
def run_unit_tests(unit_tests_to_run): 
    runner = unittest.TextTestRunner() 

    classes = \ 
    [ 
     LineBalancingUnitTests, 
    ] 

    runner.run(suite(classes, unit_tests_to_run)) 
... 

और एक और फ़ाइल:

import my_test_file 

# Comment all the tests names on this list, to run all Unit Tests 
unit_tests_to_run = \ 
[ 
    "test_it_is_sunny", 
    # "test_it_is_hot", 
] 

my_test_file.run_unit_tests(unit_tests_to_run) 
संबंधित मुद्दे