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

  • SEARCH
  • Home
  • 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 5969837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:17:16+00:00 2026-05-22T20:17:16+00:00

class AppError(Exception): pass class MissingInputError(AppError): em = {1101: Date input is missing. Please verify.,

  • 0
class AppError(Exception): pass

class MissingInputError(AppError):

    em = {1101: "Date input is missing. Please verify.", \
          1102: "Key input is missing. Please verify.", \
          1103: "Stn input is missing. Please verify."}
          # and so on ...

…

def validate(self):
    """ Method of Input class to validate input and save it """

    params = self.__params

    if 'dt' in params:
        self.__validateKey(escape(params['dt'][0]))
    else:
        raise MissingInputError(1101)

    if 'key' in params:
        self.__validateService(escape(params['key'][0]))
    else:
        raise MissingInputError(1102)

    # and so on ...

Unit testing the above, I know that the following tests in the MissingInput test class:

def testMissingKeyInput(self):
    """ Missing key should raise error """
    ip = controller.Input(MissingInput.missInputKey)
    self.assertRaises(errors.MissingInputError, ip.validate)

def testMissingDtInput(self):
    """ Missing dt should raise error """
    ip = controller.Input(MissingInput.missInputDt)
    self.assertRaises(errors.MissingInputError, ip.validate)

# and so on ...

will correctly detect if a MissingInputError exception was raised.

Is there any way to determine in the test what error number was passed to the exception while calling it, so that I can be sure that the error is being raised for that particular missing input, and not for any other missing inputs?

(P.S: Python 2.4.3).


Tip: If you are stuck with 2.4 to 2.6, use the unittest2 library.
In Python 2.7 and 3.2 a whole bunch of improvements to unittest will arrive. unittest2 is a backport of the new features (and tests) to work with Python 2.4, 2.5 & 2.6.

  • 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-22T20:17:17+00:00Added an answer on May 22, 2026 at 8:17 pm

    You can pass a regular expression that runs against the message:

    import unittest
    
    class MyError(Exception):
        pass
    
    def raiseError():
        raise MyError(100)
    
    class TestStuff(unittest.TestCase):
        def testError(self):
            self.assertRaisesRegexp(MyError, '100', raiseError)
    
    unittest.main()    
    

    Does that make sense to you? If you were raising MyError(‘foo’) or MyError(101), the test would fail because those wouldn’t match the regular expression of ‘100’. Fortunately, this method will work against numbers and anything else that you can cast to a string.

    See the unittest documentation for details on assertRaisesRegexp.

    Alternatively, if you’re on Python 2.6 or older, assertRaisesRegexp is not there and you’ll have to do something like this:

    try:
        <code>
    except MyError, message:
        self.failUnlessEqual(message.args, <expected args>)
    else:
        self.fail('MyError not raised')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class AppError(Exception): pass class MissingInputError(AppError): pass class ValidationError(AppError): pass ... def validate(self): """ Validate
class Score { var $score; var $name; var $dept; var $date; function Score($score, $name,
class Ball: a = [] def __init__(self): pass def add(self,thing): self.a.append(thing) def size(self): print
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;
class Foo(models.Model): title = models.CharField(max_length=20) slug = models.SlugField() Is there a built-in way to
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?

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.