Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7623347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:41:04+00:00 2026-05-31T04:41:04+00:00

I have some code where I’m testing for a wrapped exception, when it failed

  • 0

I have some code where I’m testing for a wrapped exception, when it failed and the exception propagated I thought the error message and back trace wasn’t verbose enough, primarily because it didn’t tell me what was expected vs. the test, I would like details of the exception and the expectation.

I adjusted my test (see example code below). I would like to know if this type of approach is valid and if any of the Python testing or mocking frameworks allow to implement it directly? (currently I’m using unittest and mox)

One of the answers to this question briefly touches on the appropriateness of using self.fail in this scenario, but doesn’t really elaborate. My assumption is that if I try to limit the test to one area I’m okay to fail the test.

Note: The code example should fail if you run it, to demonstrate the behaviour I would like to see. I’m using Python 2.7, Mox 0.5.3

import sys
import urllib2
from contextlib import closing

try:
    import lxml.etree as ET
except ImportError:
    import xml.etree.ElementTree as ET


class Defect(Exception):
    """Wrapped exception, for module error detection"""
    def __init__(self, *args):
        Exception.__init__(self, *args)
        self.wrapped_exc = sys.exc_info()


class StudioResources:
    """Dummy class"""
    def _opener(self, request, html=False):
        with closing(urllib2.urlopen(request)) as response:
            try:
                if html:
                    import lxml.html
                    return lxml.html.parse(response)
                else:
                    return ET.parse(response)
            except urllib2.HTTPError, e:
                if e.code in [400, 500]: # Bad Request, Internal Server Error
                    raise Defect, "report error to the library maintainer"
                else:
                    raise


###
# Tests
###
import unittest
import mox
import traceback
import difflib
import urllib
import httplib


def format_expectation(exc_expected=None, exc_instance=None):
    """Synopsis - For exceptions, inspired by _AssertRaisesContext

    try:
        self.assertRaises(myexc, self.studio._opener, None)
    except Exception, e:
        self.fail(format_expectation(exc_expected=myexc, exc_instance=e))
    """
    if not isinstance(exc_expected, type) or exc_instance is None:
        raise ValueError, "check __init__ args"

    differ = difflib.Differ()
    inst_class = exc_instance.__class__
    def fullname(c): return "%s.%s" % (c.__module__, c.__name__)
    diff = differ.compare(
        (fullname(inst_class),), (fullname(exc_expected),))
    _str = ("Unexpected Exception type.  unexpected:-  expected:+\n%s"
        % ("\n".join(diff),))
    return _str


class StudioTest(mox.MoxTestBase):
    def setUp(self):
        mox.MoxTestBase.setUp(self)
        self.studio = StudioResources()

    def test_opener_defect(self):
        f = urllib.addinfourl(urllib2.StringIO('dummy'), None, None)
        RESP_CODE = 501
        self.mox.StubOutWithMock(f, 'read')
        self.mox.StubOutWithMock(urllib2, 'urlopen')
        urllib2.urlopen(mox.IgnoreArg()).AndReturn(f)
        f.read(mox.IgnoreArg()).AndRaise(urllib2.HTTPError(
            'http://c.com', RESP_CODE, httplib.responses[RESP_CODE], "", None))
        self.mox.ReplayAll()
        try:
            with self.assertRaises(Defect) as exc_info:
                self.studio._opener(None)
        except Exception, e:
            traceback.print_exc()
            self.fail(format_expectation(exc_expected=Defect, exc_instance=e))
        # check the response code
        exc, inst, tb = exc_info.exception.wrapped_exc
        self.assertEquals(inst.code, RESP_CODE)
        self.mox.VerifyAll()


if __name__ == '__main__':
    unittest.main()
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:41:05+00:00Added an answer on May 31, 2026 at 4:41 am

    When writing unit tests its always a good idea to limit the test to one thing. I don’t see anything wrong with your code, but I would wrap the whole lot in a context manager. I use nose rather than unittest, which treats any AssertionError as a fail (this means that there is no need to call self.fail()), and I’ve written my own context manager to handle this case. Here is the code if you’re interested:

    class assert_raises:
    
        def __init__(self, exception):
            self.exception = exception
    
        def __enter__(self):
            return self
    
        def __exit__(self, exc_type, exc_val, exc_tb):
            assert exc_type is self.exception, "Got '{}', expected '{}'"\
                .format('None' if exc_type is None else exc_type.__name__,
                        self.exception.__name__)
            return True
    

    And then use it as in these example:

    >>> with assert_raised(ValueError):
    ...    raise ValueError
    
    >>> with assert_raised(ValueError):
    ...    pass
    Traceback (most recent call last):
        ...
    AssertionError: Got 'None', expected 'ValueError'
    
    >>> with assert_raised(ValueError):
    ...     raise TypeError
    Traceback (most recent call last):
        ...
    AssertionError: Got 'TypeError', expected 'ValueError'
    

    Since an AssertionError is raised, nose sees it as a failure and prints the full traceback anyway. This is designed for nose, but it would be a trivial matter to adjust it for unittest and mox instead. If you’re not too concerned about the exact mode of failure you could even use it as is.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code in a javascript file that needs to send queries back
I have some code that throws an exception as follows. if (jobFinished) { ...
I have some code that throws causes syncdb to throw an error (because it
I have some code for starting a thread on the .NET CF 2.0: ThreadStart
I have some code like this in a winforms app I was writing to
I have some code which collects points (consed integers) from a loop which looks
I have some code like this: If key.Equals(search, StringComparison.OrdinalIgnoreCase) Then DoSomething() End If I
I have some code that gives a user id to a utility that then
I have some code in an IAuthorizationFilter which redirects the user to a login
I have some code which utilizes parameterized queries to prevent against injection, but I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.