मैं निम्नलिखित बल्कि अजीब समस्या में चला रहा हूँ:AssertRaises में विफल रहता है, भले ही अपवाद उठाया है
मैं एक Django एप्लिकेशन विकसित करने कर रहा हूँ और अपने मॉडल कक्षा में मैं एक अपवाद है कि जब एक सत्यापन विफल हो जाता उठाया जाना चाहिए परिभाषित करने हूँ:
def validate_required_fields(self):
# Here is the validation code.
if all_fields_present:
return True
else:
raise MissingValueException(errors)
मेरी इकाई परीक्षण में मैं एक मामले में जहां अपवाद उठाया जाना चाहिए बनाने के लिए::
class MissingValueException(Exception):
"""Raise when a required attribute is missing."""
def __init__(self, message):
super(MissingValueException, self).__init__()
self.message = message
def __str__(self):
return repr(self.message)
इस कोड को एक सत्यापन विधि में एक प्रकाशन वर्ग से कहा जाता है
def test_raise_exception_incomplete_publication(self):
publication = Publication(publication_type="book")
self.assertRaises(MissingValueException, publication.validate_required_fields)
यह निम्न उत्पादन का उत्पादन:
======================================================================
ERROR: test_raise_exception_incomplete_publication (core_knowledge_platform.core_web_service.tests.logic_tests.BusinessLogicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/media/data/Dokumente/Code/master_project/core_knowledge_platform/../core_knowledge_platform/core_web_service/tests/logic_tests.py", line 45, in test_raise_exception_incomplete_publication
self.assertRaises(MissingValueException, method,)
File "/usr/lib/python2.7/unittest/case.py", line 465, in assertRaises
callableObj(*args, **kwargs)
File "/media/data/Dokumente/Code/master_project/core_knowledge_platform/../core_knowledge_platform/core_web_service/models.py", line 150, in validate_required_fields
raise MissingValueException(errors)
MissingValueException: 'Publication of type book is missing field publisherPublication of type book is missing field titlePublication of type book is missing field year'
तो ऐसा लगता है कि अपवाद की तरह उठाया है (इस स्थिति है - मैं भी एक इंटरैक्टिव IPython सत्र में यह जाँच), लेकिन यह है कि assertRaises लगता है इसे पकड़ नहीं रहा है
किसी को भी कोई विचार है कि ऐसा क्यों हो सकता है? यदि आपका परीक्षण और अपने उत्पाद कोड दो अलग रास्तों के माध्यम से अपना अपवाद वर्ग आयात कर रहे हैं
धन्यवाद
हाँ यह समस्या थी - उठाया अपवाद था:। .models.Exception जबकि अपेक्षित था केवल: .models.Exception –
BergmannF
एह ... मुझे इस समस्या को 'AssertionError' के साथ समस्या है - मैं 'self.assertRaises (AssertionError, ...) का उपयोग कर रहा हूं ', और यह अभी भी 'AssertionError' के साथ विफल रहा है। चूंकि यह कोर पायथन में है, यह जवाब नहीं हो सकता है। कोई अन्य संभावनाएं? – naught101
कभी नहीं, यह एक अलग त्रुटि थी। सबक: परीक्षण विफलता आउटपुट में लाइन संख्याओं पर बहुत करीब ध्यान दें। – naught101