मैंने पाइथन यूनिट परीक्षण की setUp
विधि में assert
के उपयोग पर कुछ विवादित सलाह पढ़ी है। परीक्षण में असफल होने में मुझे नुकसान दिखाई नहीं दे रहा है यदि परीक्षण की पूर्व शर्त विफल रहता है।सेटअप विधि में पाइथन में एक त्रुटि में त्रुटि की रिपोर्ट करने का सही तरीका क्या है?
उदाहरण के लिए:
import unittest
class MyProcessor():
"""
This is the class under test
"""
def __init__(self):
pass
def ProcessData(self, content):
return ['some','processed','data','from','content'] # Imagine this could actually pass
class Test_test2(unittest.TestCase):
def LoadContentFromTestFile(self):
return None # Imagine this is actually doing something that could pass.
def setUp(self):
self.content = self.LoadContentFromTestFile()
self.assertIsNotNone(self.content, "Failed to load test data")
self.processor = MyProcessor()
def test_ProcessData(self):
results = self.processor.ProcessData(self.content)
self.assertGreater(results, 0, "No results returned")
if __name__ == '__main__':
unittest.main()
यह लगता है एक उचित बात मेरे लिए क्या करने के लिए की तरह अर्थात यकीन है कि परीक्षण चलाने के लिए सक्षम है या नहीं। जब इस स्थापना स्थिति के कारण विफल रहता है हम पाते हैं:
F
======================================================================
FAIL: test_ProcessData (__main__.Test_test2)
----------------------------------------------------------------------
Traceback (most recent call last):
File "C:\Projects\Experiments\test2.py", line 21, in setUp
self.assertIsNotNone(self.content, "Failed to load test data")
AssertionError: unexpectedly None : Failed to load test data
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
तो आप एक स्वचालित निर्माण के माहौल में उस पर प्रकाश डाला जाना चाहिए? –
@ जोनकेज आईएमएचओ यदि आपको वास्तव में पूर्व शर्त का परीक्षण करने की आवश्यकता है तो आपको उनके लिए एक परीक्षा लिखनी चाहिए और यह सुनिश्चित करने के लिए कि आपकी पूर्व शर्त वैध हैं, 'setUp()' का उपयोग न करें। तो 'test_load_fixture (self) जैसे एक टेस्ट जोड़ें: self.assertIsNotNone (self.content, "परीक्षण डेटा लोड करने में विफल")' और आपको इस परीक्षण के लिए एक त्रुटि होगी और प्रत्येक परीक्षण के लिए असफल होगा जो 'self.content' '। –