python - कमांड लाइन के माध्यम से unittest.TestCase से एकल परीक्षण चल रहा है
unit-testing (4)
जैसा कि आप अनुमान लगाते हैं यह अच्छी तरह से काम कर सकता है
python testMyCase.py MyCase.testItIsHot
और परीक्षण का परीक्षण करने का एक और तरीका है testItIsHot
:
suite = unittest.TestSuite()
suite.addTest(MyCase("testItIsHot"))
runner = unittest.TextTestRunner()
runner.run(suite)
हमारी टीम में, हम इस तरह के अधिकांश परीक्षण मामलों को परिभाषित करते हैं:
एक "ढांचा" वर्ग 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
और केवल एकमात्र रन है testItIsHot()
मैं उसे कैसे प्राप्त कर सकता हूं?
मैंने if __name__ == "__main__":
को फिर से लिखने की कोशिश की if __name__ == "__main__":
भाग, लेकिन चूंकि मैं पाइथन के लिए नया हूं, इसलिए मैं खो रहा हूं और विधियों की तुलना में बाकी सब कुछ में if __name__ == "__main__":
हूं।
जैसा कि आप सुझाव देते हैं यह काम करता है - आपको बस कक्षा का नाम भी निर्दिष्ट करना होगा:
python testMyCase.py MyCase.testItIsHot
यदि आप अपने परीक्षण मामलों को व्यवस्थित करते हैं, यानी, उसी कोड का पालन करें जैसे वास्तविक कोड और उसी पैकेज में मॉड्यूल के लिए सापेक्ष आयात का भी उपयोग करें
आप निम्न कमांड प्रारूप का भी उपयोग कर सकते हैं:
python -m unittest mypkg.tests.test_module.TestClass.test_method
# In your case, this would be:
python -m unittest testMyCase.MyCase.testItIsHot
यदि आप अवांछित मॉड्यूल की मदद देखते हैं तो यह आपको कई संयोजनों के बारे में बताता है जो आपको मॉड्यूल से टेस्ट केस क्लास चलाने और टेस्ट केस क्लास से परीक्षण विधियों को चलाने की अनुमति देते हैं।
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()
को परिभाषित करने की आवश्यकता नहीं है।