2

I need to test my python code for an exception but I can't seem to get the right statement to run.
When I test my production code I can see that the exception is thrown but when I test for this exception in my unit test file I get this:

testincorrectparam (unittestearnings.TestEarningsArgs)
should raise error when param not an integer ... "3v" is not a valid argument.  i should be an integer
ERROR

I read through similar questions on SO but I just don't manage to find a satisfactory answer for my problem..

The production code looks like this:

class Earnings():

    def printd(i=0):

        try:
            i = int(i)

        except Exception:
            print('"' + i + '"' + " is not a valid argument.  i should be an integer")
            sys.exit(0)

And the unit test code is this:

from earnings import Earnings
import unittest

class TestEarningsArgs(unittest.TestCase):

   def testincorrectparam(self):
      '''should raise error when param not an integer'''
      e = Earnings()
      value = "3v"
      self.assertRaises(Exception, e.printd(i=value))

if __name__ == "__main__":
      unittest.main()

Really appreciate any help.

2
  • Note that printd() doesn't take a "param" named parameter, so either your test or code is incorrect there as well. Commented Aug 10, 2012 at 15:08
  • @Wooble thank you for spotting this, the code is now consistent. My question is still valid however. Cheers Commented Aug 10, 2012 at 17:25

3 Answers 3

1

The problem is that exception is catched, it's not propagated to unittest. You need to remove try-except statement and leave i = int(i). In this case exception will be raised and assertRaises will catch it itself.

Another issue is line sys.exit(0) in your code. You need to either replace it with raise Exception(...) which isn't good as Exception class is not a specific exception (you should raise more specific exception for the situation you expect to error happen).

Sign up to request clarification or add additional context in comments.

2 Comments

Rostyslav thank you your comment has helped me solve my question. I am going to post a solution now. Unfortunately I am not able to upvote your comment as I do not have the necessary seniority to do so.
Just accept the answer if it helped) Upvote is not necessary.
0

replace sys.exit(0) with raise

2 Comments

... and you should be trying to catch ValueError rather an Exception in your try clause.
thank you. I have decided to raise a custom exception instead based on the great examples I found in "Dive Into Python". Cheers
0

i am trying to learn unittesting in python i think the general form of assertRaise is

TestCase.assertRaises(Exception, callable, parameter1,parameter2,...)

so please try

self.assertRaises(Exception, e.printd,i=value)

hope it will work for you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.